forked from postcss/autoprefixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary.js
394 lines (328 loc) · 12.4 KB
/
binary.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
var CssSyntaxError = require('postcss/lib/css-syntax-error');
var autoprefixer = require('autoprefixer-core');
var path = require('path');
var fs = require('fs-extra');
class Binary {
constructor(process) {
this.arguments = process.argv.slice(2);
this.stdin = process.stdin;
this.stderr = process.stderr;
this.stdout = process.stdout;
this.status = 0;
this.command = 'compile';
this.inputFiles = [];
this.processOptions = { };
this.processorOptions = { };
this.parseArguments();
}
// Quick help message
help() {
return (
`Usage: autoprefixer [OPTION...] FILES
Parse CSS files and add prefixed properties and values.
Options:
-b, --browsers BROWSERS add prefixes for selected browsers
-c, --clean remove all known prefixes
-o, --output FILE set output file
-d, --dir DIR set output dir
-m, --map generate source map
--no-map skip source map even if previous map exists
--no-inline-map do not inline maps to data:uri
--inline-map force inline map
--annotation PATH change map location relative from CSS file
--no-map-annotation do not add source map annotation comment in CSS
--no-sources-content remove origin CSS from maps
--sources-content force include origin CSS into map
--no-cascade do not create nice visual cascade of prefixes
--no-remove do not remove outdated prefixes
--safe try to fix CSS syntax errors
-i, --info show selected browsers and properties
-h, --help show help text
-v, --version print program version`);
}
// Options description
desc() {
return (
`Files:
If you didn't set input files, autoprefixer will read from stdin stream.
By default, prefixed CSS will rewrite original files.
You can specify output file or directory by '-o' argument.
For several input files you can specify only output directory by '-d'.
Output CSS will be written to stdout stream on '-o -' argument or stdin input.
Source maps:
On '-m' argument Autoprefixer will generate source map for changes near
output CSS (for out/main.css it generates out/main.css.map).
If previous source map will be near input files (for example, in/main.css
and in/main.css.map) Autoprefixer will apply previous map to output
source map.
Browsers:
Separate browsers by comma. For example, '-b "> 1%, opera 12"'.
You can set browsers by global usage statictics: '-b \"> 1%\"'.
or last version: '-b "last 2 versions"'.`);
}
// Print to stdout
print(str) {
str = str.replace(/\n$/, '');
this.stdout.write(str + "\n");
}
// Print to stdout
error(str) {
this.status = 1;
this.stderr.write(str + "\n");
}
// Get current version
version() {
return require('./package.json').version;
}
// Parse arguments
parseArguments() {
var args = this.arguments.slice();
while ( args.length > 0 ) {
var arg = args.shift();
if ( arg == '-h' || arg == '--help' ) {
this.command = 'showHelp';
} else if ( arg == '-v' || arg == '--version' ) {
this.command = 'showVersion';
} else if ( arg == '-i' || arg == '--info' ) {
this.command = 'info';
} else if ( arg == '-m' || arg == '--map' ) {
this.processOptions.map = { };
} else if ( arg == '--no-map' ) {
this.processOptions.map = false;
} else if ( arg == '-I' || arg == '--inline-map' ) {
if ( typeof( this.processOptions.map ) == 'undefined' ) {
this.processOptions.map = { };
}
this.processOptions.map.inline = true;
} else if ( arg == '--no-inline-map' ) {
if ( typeof( this.processOptions.map ) == 'undefined' ) {
this.processOptions.map = { };
}
this.processOptions.map.inline = false;
} else if ( arg == '--annotation' ) {
if ( typeof( this.processOptions.map ) == 'undefined' ) {
this.processOptions.map = { };
}
this.processOptions.map.annotation = args.shift();
} else if ( arg == '--no-map-annotation' ) {
if ( typeof( this.processOptions.map ) == 'undefined' ) {
this.processOptions.map = { };
}
this.processOptions.map.annotation = false;
} else if ( arg == '--sources-content' ) {
if ( typeof( this.processOptions.map ) == 'undefined' ) {
this.processOptions.map = { };
}
this.processOptions.map.sourcesContent = true;
} else if ( arg == '--no-sources-content' ) {
if ( typeof( this.processOptions.map ) == 'undefined' ) {
this.processOptions.map = { };
}
this.processOptions.map.sourcesContent = false;
} else if ( arg == '--no-cascade' ) {
this.processorOptions.cascade = false;
} else if ( arg == '--no-remove' ) {
this.processorOptions.remove = false;
} else if ( arg == '--safe' ) {
this.processOptions.safe = true;
} else if ( arg == '-b' || arg == '--browsers' ) {
this.processorOptions.browsers = args.shift().split(',')
.map( (i) => i.trim() );
} else if ( arg == '-c' || arg == '--clean' ) {
this.processorOptions.browsers = [];
} else if ( arg == '-o' || arg == '--output' ) {
this.outputFile = args.shift();
} else if ( arg == '-d' || arg == '--dir' ) {
this.outputDir = args.shift();
} else {
if ( arg.match(/^-\w$/) || arg.match(/^--\w[\w-]+$/) ) {
this.command = undefined;
this.error('autoprefixer: Unknown argument ' + arg);
this.error('');
this.error(this.help());
} else {
this.inputFiles.push(arg);
}
}
}
}
// Print help
showHelp(done) {
this.print(this.help());
this.print('');
this.print(this.desc());
done();
}
// Print version
showVersion(done) {
this.print('autoprefixer ' + this.version());
done();
}
// Print inspect
info(done) {
this.print(this.compiler().info());
done();
}
// Mark that there is another async work
startWork() {
this.waiting += 1;
}
// Execute done callback if there is no works
endWork() {
this.waiting -= 1;
if ( this.waiting <= 0 ) this.doneCallback();
}
// Write error to stderr and finish work
workError(str) {
this.error(str);
this.endWork();
}
// Lazy loading for Autoprefixer instance
compiler() {
if ( !this.compilerCache ) {
this.compilerCache = autoprefixer(this.processorOptions);
}
return this.compilerCache;
}
// Compile loaded CSS
compileCSS(css, output, input) {
var opts = { };
for ( var name in this.processOptions ) {
opts[name] = this.processOptions[name];
}
if ( input ) opts.from = input;
if ( output != '-' ) opts.to = output;
var result;
try {
result = this.compiler().process(css, opts);
} catch (error) {
if ( error.indexOf && error.indexOf('Unknown browser') != -1 ) {
this.error('autoprefixer: ' + error);
} else if ( error.autoprefixer ) {
this.error('autoprefixer: ' + error.message);
} else if ( error instanceof CssSyntaxError ) {
var text = error.message;
if ( error.source ) text += '\n' + error.highlight();
this.error('autoprefixer:' + text);
} else {
this.error('autoprefixer: Internal error');
if ( error.stack ) {
this.error('');
this.error(error.stack);
}
}
}
if ( !result ) return this.endWork();
if ( output == '-' ) {
this.print(result.css);
this.endWork();
} else {
fs.outputFile(output, result.css, (error) => {
if (error) this.error('autoprefixer: ' + error);
if ( result.map ) {
var map;
if ( opts.map && opts.map.annotation ) {
map = path.resolve(path.dirname(output),
opts.map.annotation);
} else {
map = output + '.map';
}
fs.writeFile(map, result.map, (error) => {
if (error) this.error('autoprefixer: ' + error);
this.endWork();
});
} else {
this.endWork();
}
});
}
}
// Return input and output files array
files() {
if ( this.inputFiles.length === 0 && !this.outputFile ) {
this.outputFile = '-';
}
var i, file, list = [];
if ( this.outputDir ) {
if ( this.inputFiles.length === 0 ) {
this.error('autoprefixer: For STDIN input you need ' +
'to specify output file (by -o FILE),\n ' +
'not output dir');
return;
}
var dir = this.outputDir;
if ( fs.existsSync(dir) && !fs.statSync(dir).isDirectory() ) {
this.error('autoprefixer: Path ' + dir +
' is a file, not directory');
return;
}
var output;
for ( file of this.inputFiles ) {
output = path.join(this.outputDir, path.basename(file));
list.push([file, output]);
}
} else if ( this.outputFile ) {
if ( this.inputFiles.length > 1 ) {
this.error('autoprefixer: For several files you can ' +
'specify only output dir (by -d DIR`),\n' +
'not one output file');
return;
}
for ( file of this.inputFiles ) {
list.push([file, this.outputFile]);
}
} else {
for ( file of this.inputFiles ) {
list.push([file, file]);
}
}
return list;
}
// Compile selected files
compile(done) {
this.waiting = 0;
this.doneCallback = done;
var files = this.files();
if ( !files ) return done();
if ( files.length === 0 ) {
this.startWork();
var css = '';
this.stdin.resume();
this.stdin.on('data', (chunk) => css += chunk);
this.stdin.on('end', () => {
this.compileCSS(css, this.outputFile);
});
} else {
var i, input, output;
for ( i = 0; i < files.length; i++ ) {
this.startWork();
}
for ( i = 0; i < files.length; i++ ) {
[input, output] = files[i];
if ( !fs.existsSync(input) ) {
this.workError('autoprefixer: File ' + input +
'doesn\'t exists');
continue;
}
((input, output) => {
fs.readFile(input, (error, css) => {
if ( error ) {
this.workError('autoprefixer: ' + error.message);
} else {
this.compileCSS(css, output, input);
}
});
})(input, output);
}
}
}
// Execute command selected by arguments
run(done) {
if ( this.command ) {
this[this.command](done);
} else {
done();
}
}
}
module.exports = Binary;