forked from kangax/html-minifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·347 lines (308 loc) · 12 KB
/
cli.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env node
/**
* html-minifier CLI tool
*
* The MIT License (MIT)
*
* Copyright (c) 2014-2015 Zoltan Frombach
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
/* globals JSON:true */
'use strict';
var cli = require('cli');
var concat = require('concat-stream');
var changeCase = require('change-case');
var path = require('path');
var fs = require('fs');
var appName = require('./package.json').name;
var appVersion = require('./package.json').version;
var minify = require('./dist/htmlminifier.min.js').minify;
var HTMLLint = require('./dist/htmlminifier.min.js').HTMLLint;
var minifyOptions = {};
var input = null;
var output = null;
cli.width = 100;
cli.option_width = 40;
cli.setApp(appName, appVersion);
var usage = appName + ' [OPTIONS] [FILE(s)]\n\n';
usage += ' If no input file(s) specified then STDIN will be used for input.\n';
usage += ' If more than one input file specified those will be concatenated and minified together.\n\n';
usage += ' When you specify a config file with the --config-file option (see sample-cli-config-file.conf for format)\n';
usage += ' you can still override some of its contents by providing individual command line options, too.\n\n';
usage += ' When you want to provide an array of strings for --ignore-custom-comments or --process-scripts options\n';
usage += ' on the command line you must escape those such as --ignore-custom-comments "[\\"string1\\",\\"string1\\"]"\n';
cli.setUsage(usage);
var mainOptions = {
removeComments: [[false, 'Strip HTML comments']],
removeCommentsFromCDATA: [[false, 'Strip HTML comments from scripts and styles']],
removeCDATASectionsFromCDATA: [[false, 'Remove CDATA sections from script and style elements']],
collapseWhitespace: [[false, 'Collapse white space that contributes to text nodes in a document tree.']],
conservativeCollapse: [[false, 'Always collapse to 1 space (never remove it entirely)']],
preserveLineBreaks: [[false, 'Always collapse to 1 line break (never remove it entirely) when whitespace between tags include a line break.']],
collapseBooleanAttributes: [[false, 'Omit attribute values from boolean attributes']],
removeAttributeQuotes: [[false, 'Remove quotes around attributes when possible.']],
removeRedundantAttributes: [[false, 'Remove attributes when value matches default.']],
preventAttributesEscaping: [[false, 'Prevents the escaping of the values of attributes.']],
useShortDoctype: [[false, 'Replaces the doctype with the short (HTML5) doctype']],
removeEmptyAttributes: [[false, 'Remove all attributes with whitespace-only values']],
removeScriptTypeAttributes: [[false, 'Remove type="text/javascript" from script tags. Other type attribute values are left intact.']],
removeStyleLinkTypeAttributes: [[false, 'Remove type="text/css" from style and link tags. Other type attribute values are left intact.']],
removeOptionalTags: [[false, 'Remove unrequired tags']],
removeIgnored: [[false, 'Remove all tags starting and ending with <%, %>, <?, ?>']],
removeEmptyElements: [[false, 'Remove all elements with empty contents']],
lint: [[false, 'Toggle linting']],
keepClosingSlash: [[false, 'Keep the trailing slash on singleton elements']],
caseSensitive: [[false, 'Treat attributes in case sensitive manner (useful for SVG; e.g. viewBox)']],
minifyJS: [[false, 'Minify Javascript in script elements and on* attributes (uses UglifyJS)']],
minifyCSS: [[false, 'Minify CSS in style elements and style attributes (uses clean-css)']],
minifyURLs: [[false, 'Minify URLs in various attributes (uses relateurl)']],
ignoreCustomComments: [[false, 'Array of regex\'es that allow to ignore certain comments, when matched', 'string'], 'json-regex'],
processScripts: [[false, 'Array of strings corresponding to types of script elements to process through minifier (e.g. "text/ng-template", "text/x-handlebars-template", etc.)', 'string'], 'json-regex'],
maxLineLength: [[false, 'Max line length', 'number'], true],
customAttrAssign: [[false, 'Arrays of regex\'es that allow to support custom attribute assign expressions (e.g. \'<div flex?="{{mode != cover}}"></div>\')', 'string'], 'json-regex'],
customAttrSurround: [[false, 'Arrays of regex\'es that allow to support custom attribute surround expressions (e.g. <input {{#if value}}checked="checked"{{/if}}>)', 'string'], 'json-regex'],
customAttrCollapse: [[false, 'Regex that specifies custom attribute to strip newlines from (e.g. /ng\-class/)', 'string'], 'string-regex']
};
var cliOptions = {
version: ['v', 'Version information'],
output: ['o', 'Specify output file (if not specified STDOUT will be used for output)', 'file'],
'config-file': ['c', 'Use config file', 'file'],
'input-dir': [false, 'Specify an input directory', 'dir'],
'output-dir': [false, 'Specify an output directory', 'dir']
};
var mainOptionKeys = Object.keys(mainOptions);
mainOptionKeys.forEach(function(key) {
cliOptions[changeCase.paramCase(key)] = mainOptions[key][0];
});
cli.parse(cliOptions);
cli.main(function(args, options) {
function stringToRegExp(value) {
// JSON does not support regexes, so, e.g., JSON.parse() will not create
// a RegExp from the JSON value `[ "/matchString/" ]`, which is
// technically just an array containing a string that begins and end with
// a forward slash. To get a RegExp from a JSON string, it must be
// constructed explicitly in JavaScript.
//
// The likelihood of actually wanting to match text that is enclosed in
// forward slashes is probably quite rare, so if forward slashes were
// included in an argument that requires a regex, the user most likely
// thought they were part of the syntax for specifying a regex.
//
// In the unlikely case that forward slashes are indeed desired in the
// search string, the user would need to enclose the expression in a
// second set of slashes:
//
// --customAttrSrround "[\"//matchString//\"]"
//
if (value) {
var stripSlashes = /^\/(.*)\/$/.exec(value);
if (stripSlashes) {
value = stripSlashes[1];
}
return new RegExp(value);
}
}
function parseJSONOption(value, options) {
var opts = options || {};
if (value !== null) {
var jsonArray;
try {
jsonArray = JSON.parse(value);
if (opts.regexArray) {
jsonArray = jsonArray.map(function (regexString) {
return stringToRegExp(regexString);
});
}
}
catch (e) {
cli.fatal('Could not parse JSON value \'' + value + '\'');
}
if (jsonArray instanceof Array) {
return jsonArray;
}
else {
return [value];
}
}
}
function runMinify(original, output) {
var status = 0;
var minified = null;
try {
minified = minify(original, minifyOptions);
}
catch (e) {
status = 3;
cli.error('Minification error');
process.stderr.write(e);
}
if (minifyOptions.lint) {
minifyOptions.lint.populate();
}
if (minified !== null) {
// Write the output
try {
if (output !== null) {
fs.writeFileSync(path.resolve(output), minified);
}
else {
process.stdout.write(minified);
}
}
catch (e) {
status = 4;
console.log(output);
cli.error('Cannot write to output');
}
}
}
function createDirectory(path) {
try {
fs.mkdirSync(path);
}
catch (e) {
if (e.code === 'EEXIST') {
return;
}
cli.fatal('Can not create directory ' + path);
cli.exit(3);
}
}
function processDirectory(inputDir, outputDir) {
createDirectory(outputDir);
var fileList = fs.readdirSync(inputDir);
fileList.forEach(function(fileName) {
var inputFilePath = inputDir + '/' + fileName;
var outputFilePath = outputDir + '/' + fileName;
var stat = fs.statSync(inputFilePath);
if (stat.isDirectory()) {
processDirectory(inputFilePath, outputFilePath);
return;
}
var originalContent = fs.readFileSync(inputFilePath, 'utf8');
runMinify(originalContent, outputFilePath);
});
}
if (options.version) {
cli.output(appName + ' v' + appVersion);
cli.exit(0);
}
if (options['config-file']) {
var fileOptions;
var fileOptionsPath = path.resolve(options['config-file']);
try {
fs.accessSync(fileOptionsPath, fs.R_OK);
}
catch (e) {
cli.fatal('The specified config file doesn’t exist or is unreadable:\n' + fileOptionsPath);
}
try {
fileOptions = JSON.parse(fs.readFileSync(fileOptionsPath), 'utf8');
}
catch (je) {
try {
fileOptions = require(fileOptionsPath);
}
catch (ne) {
cli.fatal('Cannot read the specified config file. \nAs JSON: ' + je.message + '\nAs module: ' + ne.message);
}
}
if (fileOptions && typeof fileOptions === 'object') {
minifyOptions = fileOptions;
}
}
mainOptionKeys.forEach(function(key) {
var paramKey = changeCase.paramCase(key);
var value = options[paramKey];
if (options[paramKey] !== null) {
switch (mainOptions[key][1]) {
case 'json':
minifyOptions[key] = parseJSONOption(value);
break;
case 'json-regex':
minifyOptions[key] = parseJSONOption(value, { regexArray: true });
break;
case 'string-regex':
minifyOptions[key] = stringToRegExp(value);
break;
case true:
minifyOptions[key] = value;
break;
default:
minifyOptions[key] = true;
}
}
});
if (minifyOptions.lint === true) {
minifyOptions.lint = new HTMLLint();
}
if (args.length) {
input = args;
}
if (options['input-dir'] || options['output-dir']) {
var inputDir = options['input-dir'];
var outputDir = options['output-dir'];
if (!inputDir) {
cli.error('The option output-dir need to be use with the option include-dir. If you are working with only 1 file, use -o.');
cli.exit(2);
}
try {
fs.statSync(inputDir);
}
catch (e) {
cli.error('The input directory does not exits');
cli.exit(2);
}
if (!outputDir) {
cli.error('You need to specify where to write the output files with the option --output-dir');
cli.exit(2);
}
try {
processDirectory(inputDir, outputDir);
}
catch (e) {
cli.error('Something wrong happened');
cli.error(e);
cli.exit(3);
}
cli.exit(0);
return;
}
if (options.output) {
output = options.output;
}
if (input !== null) { // Minifying one or more files specified on the CMD line
var original = '';
input.forEach(function(afile) {
try {
original += fs.readFileSync(afile, 'utf8');
}
catch (e) {
cli.error('Cannot read file ' + afile);
cli.exit(2);
}
});
runMinify(original, output);
}
else { // Minifying input coming from STDIN
process.stdin.pipe(concat({ encoding: 'string' }, runMinify));
}
cli.exit(status);
});