forked from kangax/html-minifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·238 lines (211 loc) · 8.25 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
#!/usr/bin/env node
/**
* html-minifier CLI tool
*
* The MIT License (MIT)
*
* Copyright (c) 2014 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.
*
*/
'use strict';
var cli = require('cli');
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 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.']],
useShortDoctype: [[false, 'Replaces the doctype with the short (HTML5) doctype']],
removeEmptyAttributes: [[false, 'Remove all attributes with whitespace-only values']],
removeOptionalTags: [[false, 'Remove unrequired tags']],
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'],
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'],
maxLineLength: [[false, 'Max line length', 'number'], true]
};
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']
};
var mainOptionKeys = Object.keys(mainOptions);
mainOptionKeys.forEach(function(key) {
cliOptions[changeCase.paramCase(key)] = mainOptions[key][0];
});
cli.parse(cliOptions);
cli.main(function(args, options) {
if (options.version) {
process.stderr.write(appName + ' v' + appVersion);
cli.exit(0);
}
if (options['config-file']) {
try {
var fileOptions = JSON.parse(fs.readFileSync(path.resolve(options['config-file']), 'utf8'));
if ((fileOptions !== null) && (typeof fileOptions === 'object')) {
minifyOptions = fileOptions;
}
}
catch (e) {
process.stderr.write('Error: Cannot read the specified config file');
cli.exit(1);
}
}
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 true:
minifyOptions[key] = value;
break;
default:
minifyOptions[key] = true;
}
}
});
if (args.length) {
input = args;
}
if (options.output) {
output = options.output;
}
var original = '';
var status = 0;
if (input !== null) { // Minifying one or more files specified on the CMD line
input.forEach(function(afile) {
try {
original += fs.readFileSync(afile, 'utf8');
}
catch (e) {
status = 2;
process.stderr.write('Error: Cannot read file ' + afile);
}
});
}
else { // Minifying input coming from STDIN
var BUFSIZE = 4096;
var buf = new Buffer(BUFSIZE);
var bytesRead;
while (true) { // Loop as long as stdin input is available.
bytesRead = 0;
try {
bytesRead = fs.readSync(process.stdin.fd, buf, 0, BUFSIZE);
}
catch (e) {
if (e.code === 'EAGAIN') { // 'resource temporarily unavailable'
// Happens on OS X 10.8.3 (not Windows 7!), if there's no
// stdin input - typically when invoking a script without any
// input (for interactive stdin input).
// If you were to just continue, you'd create a tight loop.
process.stderr.write('ERROR: interactive stdin input not supported');
cli.exit(2);
}
else if (e.code === 'EOF') {
// Happens on Windows 7, but not OS X 10.8.3:
// simply signals the end of *piped* stdin input.
break;
}
throw e; // unexpected exception
}
if (bytesRead === 0) {
// No more stdin input available.
// OS X 10.8.3: regardless of input method, this is how the end
// of input is signaled.
// Windows 7: this is how the end of input is signaled for
// *interactive* stdin input.
break;
}
original += buf.toString('utf8', 0, bytesRead);
}
}
function parseJSONOption(value) {
if (value !== null) {
var jsonArray;
try {
jsonArray = JSON.parse(value);
}
catch (e) {}
if (jsonArray instanceof Array) {
return jsonArray;
}
else {
return [value];
}
}
}
// Run minify
var minified = null;
try {
minified = minify(original, minifyOptions);
}
catch (e) {
status = 3;
process.stderr.write('Error: Minification error');
}
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;
process.stderr.write('Error: Cannot write to output');
}
}
cli.exit(status);
});