-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
186 lines (157 loc) · 5.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const beautify = require('js-beautify');
const colors = require('ansi-colors');
const cosmiconfig = require('cosmiconfig');
const log = require('fancy-log');
const mergeWith = require('lodash.mergewith');
const path = require('path');
const PluginError = require('plugin-error');
const through = require('through2');
const PLUGIN_NAME = 'gulp-jsbeautifier';
/**
* Merge options from different sources
* @param {Object} pluginOptions The gulp-jsbeautifier options
* @return {Object} The options
*/
function mergeOptions(pluginOptions) {
const defaultOptions = {
config: null,
debug: false,
css: {
file_types: ['.css', '.less', '.sass', '.scss'],
},
html: {
file_types: ['.html'],
},
js: {
file_types: ['.js', '.json'],
},
};
// Load 'file options'
const explorer = cosmiconfig('jsbeautify');
let explorerResult;
if (pluginOptions && pluginOptions.config) {
explorerResult = explorer.loadSync(path.resolve(pluginOptions.config));
} else {
explorerResult = explorer.searchSync();
}
let fileOptions;
if (explorerResult) {
fileOptions = explorerResult.config;
}
// Merge options
const finalOptions = mergeWith({}, defaultOptions, fileOptions, pluginOptions, (objValue, srcValue) => {
if (Array.isArray(objValue)) {
return objValue.concat(srcValue);
}
return undefined;
});
// Show debug messages
if (finalOptions.debug) {
if (fileOptions) {
log(`File options:\n${JSON.stringify(fileOptions, null, 2)}`);
}
log(`Final options:\n${JSON.stringify(finalOptions, null, 2)}`);
}
// Delete properties not used
delete finalOptions.config;
delete finalOptions.debug;
return finalOptions;
}
/**
* Beautify files or perform validation
* @param {Object} pluginOptions The gulp-jsbeautifier parameter options
* @param {boolean} doValidation Specifies whether perform validation only
* @return {Object} The object stream
*/
function helper(pluginOptions, doValidation) {
const options = mergeOptions(pluginOptions);
return through.obj((file, encoding, callback) => {
let oldContent;
let newContent;
let type = null;
if (file.isNull()) {
callback(null, file);
return;
}
if (file.isStream()) {
callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
return;
}
// Check if current file should be treated as JavaScript, HTML, CSS or if it should be ignored
['js', 'css', 'html'].some((value) => {
// Check if at least one element in 'file_types' is suffix of file basename
if (options[value].file_types.some(suffix => path.basename(file.path).endsWith(suffix))) {
type = value;
return true;
}
return false;
});
// Initialize properties for reporter
file.jsbeautify = {};
file.jsbeautify.type = type;
file.jsbeautify.beautified = false;
file.jsbeautify.canBeautify = false;
if (type) {
oldContent = file.contents.toString('utf8');
newContent = beautify[type](oldContent, options);
if (oldContent.toString() !== newContent.toString()) {
if (doValidation) {
file.jsbeautify.canBeautify = true;
} else {
file.contents = Buffer.from(newContent);
file.jsbeautify.beautified = true;
}
}
}
callback(null, file);
});
}
/**
* Beautify files
* @param {Object} options The gulp-jsbeautifier parameter options
* @return {Object} The object stream with beautified files
*/
const plugin = options => helper(options, false);
/**
* Perform the validation of files without changing their content
* @param {Object} options The gulp-jsbeautifier parameter options
* @return {Object} The object stream
*/
plugin.validate = options => helper(options, true);
/**
* Show results of beautification or validation
* @param {Object} options The gulp-jsbeautifier reporter options
* @return {Object} The object stream
*/
plugin.reporter = (options) => {
let verbosity = 0;
let errorCount = 0;
if (typeof options === 'object' && Object.prototype.hasOwnProperty.call(options, 'verbosity')) {
({ verbosity } = options);
}
return through.obj((file, encoding, callback) => {
if (file.jsbeautify) {
if (verbosity >= 1 && file.jsbeautify.type === null) {
log(`Can not beautify ${colors.cyan(file.relative)}`);
} else if (verbosity >= 0 && file.jsbeautify.beautified) {
log(`Beautified ${colors.cyan(file.relative)} [${file.jsbeautify.type}]`);
} else if (verbosity >= 0 && file.jsbeautify.canBeautify) {
errorCount += 1;
log(`Can beautify ${colors.cyan(file.relative)} [${file.jsbeautify.type}]`);
} else if (verbosity >= 1) {
log(`Already beautified ${colors.cyan(file.relative)} [${file.jsbeautify.type}]`);
}
}
callback(null, file);
}, function flush(callback) {
if (errorCount > 0) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Validation not passed. Please beautify.'));
}
callback();
});
};
plugin.report = {
BEAUTIFIED: 0,
ALL: 1,
};
module.exports = plugin;