/
cli.js
484 lines (401 loc) · 15.1 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
var assert = require('assert'),
fs = require('fs'),
path = require('path'),
read = require('fs').readFileSync,
stream = require('stream'),
spawn = require('cross-spawn'),
cli = path.join(__dirname, '..', 'bin', 'node-sass'),
fixture = path.join.bind(null, __dirname, 'fixtures');
describe('cli', function() {
describe('node-sass < in.scss', function() {
it('should read data from stdin', function(done) {
var src = fs.createReadStream(fixture('simple/index.scss'));
var expected = read(fixture('simple/expected.css'), 'utf8').trim();
var bin = spawn(cli);
bin.stdout.setEncoding('utf8');
bin.stdout.once('data', function(data) {
assert.equal(data.trim(), expected.replace(/\r\n/g, '\n'));
done();
});
src.pipe(bin.stdin);
});
it('should compile sass using the --indented-syntax option', function(done) {
var src = fs.createReadStream(fixture('indent/index.sass'));
var expected = read(fixture('indent/expected.css'), 'utf8').trim();
var bin = spawn(cli, ['--indented-syntax']);
bin.stdout.setEncoding('utf8');
bin.stdout.once('data', function(data) {
assert.equal(data.trim(), expected.replace(/\r\n/g, '\n'));
done();
});
src.pipe(bin.stdin);
});
it('should compile with the --output-style option', function(done) {
var src = fs.createReadStream(fixture('compressed/index.scss'));
var expected = read(fixture('compressed/expected.css'), 'utf8').trim();
var bin = spawn(cli, ['--output-style', 'compressed']);
bin.stdout.setEncoding('utf8');
bin.stdout.once('data', function(data) {
assert.equal(data.trim(), expected.replace(/\r\n/g, '\n'));
done();
});
src.pipe(bin.stdin);
});
it('should compile with the --source-comments option', function(done) {
var src = fs.createReadStream(fixture('source-comments/index.scss'));
var expected = read(fixture('source-comments/expected.css'), 'utf8').trim();
var bin = spawn(cli, ['--source-comments']);
bin.stdout.setEncoding('utf8');
bin.stdout.once('data', function(data) {
assert.equal(data.trim(), expected.replace(/\r\n/g, '\n'));
done();
});
src.pipe(bin.stdin);
});
it('should render with indentWidth and indentType options', function(done) {
var src = new stream.Readable();
var bin = spawn(cli, ['--indent-width', 7, '--indent-type', 'tab']);
src._read = function() { };
src.push('div { color: transparent; }');
src.push(null);
bin.stdout.setEncoding('utf8');
bin.stdout.once('data', function(data) {
assert.equal(data.trim(), 'div {\n\t\t\t\t\t\t\tcolor: transparent; }');
done();
});
src.pipe(bin.stdin);
});
it('should render with linefeed option', function(done) {
var src = new stream.Readable();
var bin = spawn(cli, ['--linefeed', 'lfcr']);
src._read = function() { };
src.push('div { color: transparent; }');
src.push(null);
bin.stdout.setEncoding('utf8');
bin.stdout.once('data', function(data) {
assert.equal(data.trim(), 'div {\n\r color: transparent; }');
done();
});
src.pipe(bin.stdin);
});
});
describe('node-sass in.scss', function() {
it('should compile a scss file', function(done) {
process.chdir(fixture('simple'));
var src = fixture('simple/index.scss');
var dest = fixture('simple/index.css');
var bin = spawn(cli, [src, dest]);
bin.once('close', function() {
assert(fs.existsSync(dest));
fs.unlinkSync(dest);
process.chdir(__dirname);
done();
});
});
it('should compile a scss file to custom destination', function(done) {
process.chdir(fixture('simple'));
var src = fixture('simple/index.scss');
var dest = fixture('simple/index-custom.css');
var bin = spawn(cli, [src, dest]);
bin.once('close', function() {
assert(fs.existsSync(dest));
fs.unlinkSync(dest);
process.chdir(__dirname);
done();
});
});
it('should compile with the --include-path option', function(done) {
var includePaths = [
'--include-path', fixture('include-path/functions'),
'--include-path', fixture('include-path/lib')
];
var src = fixture('include-path/index.scss');
var expected = read(fixture('include-path/expected.css'), 'utf8').trim();
var bin = spawn(cli, [src].concat(includePaths));
bin.stdout.setEncoding('utf8');
bin.stdout.once('data', function(data) {
assert.equal(data.trim(), expected.replace(/\r\n/g, '\n'));
done();
});
});
it('should not exit with the --watch option', function(done) {
var src = fixture('simple/index.scss');
var bin = spawn(cli, [src, '--watch']);
var exited;
bin.once('close', function() {
exited = true;
});
setTimeout(function() {
if (exited) {
throw new Error('Watch ended too early!');
} else {
bin.kill();
done();
}
}, 100);
});
it('should emit `warn` on file change when using --watch option', function(done) {
var src = fixture('simple/tmp.scss');
fs.writeFileSync(src, '');
var bin = spawn(cli, ['--watch', src]);
bin.stderr.setEncoding('utf8');
bin.stderr.once('data', function(data) {
assert(data.trim() === '=> changed: ' + src);
fs.unlinkSync(src);
done();
});
setTimeout(function() {
fs.appendFileSync(src, 'body {}');
}, 500);
});
it('should render all watched files', function(done) {
var src = fixture('simple/bar.scss');
fs.writeFileSync(src, '');
var bin = spawn(cli, [
'--output-style', 'compressed',
'--watch', src
]);
bin.stdout.setEncoding('utf8');
bin.stdout.once('data', function(data) {
assert(data.trim() === 'body{background:white}');
fs.unlinkSync(src);
done();
});
setTimeout(function() {
fs.appendFileSync(src, 'body{background:white}');
}, 500);
});
it('should watch the full sass dep tree for a single file', function(done) {
var src = fixture('watching/index.scss');
var foo = fixture('watching/foo.scss');
fs.writeFileSync(foo, '');
var bin = spawn(cli, [
'--output-style', 'compressed',
'--watch', src
]);
bin.stdout.setEncoding('utf8');
bin.stdout.once('data', function(data) {
assert(data.trim() === 'body{background:white}');
done();
});
setTimeout(function() {
fs.appendFileSync(foo, 'body{background:white}\n');
}, 500);
});
});
describe('node-sass in.scss --output out.css', function() {
it('should compile a scss file to build.css', function(done) {
var src = fixture('simple/index.scss');
var dest = fixture('simple/index.css');
var bin = spawn(cli, [src, '--output', path.dirname(dest)]);
bin.once('close', function() {
assert(fs.existsSync(dest));
fs.unlinkSync(dest);
done();
});
});
it('should compile with the --source-map option', function(done) {
var src = fixture('source-map/index.scss');
var destCss = fixture('source-map/index.css');
var destMap = fixture('source-map/index.map');
var expectedCss = read(fixture('source-map/expected.css'), 'utf8').trim().replace(/\r\n/g, '\n');
var expectedMap = read(fixture('source-map/expected.map'), 'utf8').trim().replace(/\r\n/g, '\n');
var bin = spawn(cli, [src, '--output', path.dirname(destCss), '--source-map', destMap]);
bin.once('close', function() {
assert.equal(read(destCss, 'utf8').trim(), expectedCss);
assert.equal(read(destMap, 'utf8').trim(), expectedMap);
fs.unlinkSync(destCss);
fs.unlinkSync(destMap);
done();
});
});
it('should omit sourceMappingURL if --omit-source-map-url flag is used', function(done) {
var src = fixture('source-map/index.scss');
var dest = fixture('source-map/index.css');
var map = fixture('source-map/index.map');
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--source-map', map, '--omit-source-map-url'
]);
bin.once('close', function() {
assert(read(dest, 'utf8').indexOf('sourceMappingURL') === -1);
assert(fs.existsSync(map));
fs.unlinkSync(map);
fs.unlinkSync(dest);
done();
});
});
it('should compile with the --source-root option', function(done) {
var src = fixture('source-map/index.scss');
var destCss = fixture('source-map/index.css');
var destMap = fixture('source-map/index.map');
var expectedCss = read(fixture('source-map/expected.css'), 'utf8').trim().replace(/\r\n/g, '\n');
var expectedUrl = 'http://test/';
var bin = spawn(cli, [
src, '--output', path.dirname(destCss),
'--source-map-root', expectedUrl,
'--source-map', destMap
]);
bin.once('close', function() {
assert.equal(read(destCss, 'utf8').trim(), expectedCss);
assert.equal(JSON.parse(read(destMap, 'utf8')).sourceRoot, expectedUrl);
fs.unlinkSync(destCss);
fs.unlinkSync(destMap);
done();
});
});
});
describe('node-sass in.scss --output path/to/file/out.css', function() {
it('should create the output directory', function(done) {
var src = fixture('output-directory/index.scss');
var dest = fixture('output-directory/path/to/file/index.css');
var bin = spawn(cli, [src, '--output', path.dirname(dest)]);
bin.once('close', function() {
assert(fs.existsSync(path.dirname(dest)));
fs.unlinkSync(dest);
fs.rmdirSync(path.dirname(dest));
dest = path.dirname(dest);
fs.rmdirSync(path.dirname(dest));
dest = path.dirname(dest);
fs.rmdirSync(path.dirname(dest));
done();
});
});
});
describe('importer', function() {
var dest = fixture('include-files/index.css');
var src = fixture('include-files/index.scss');
var expected = read(fixture('include-files/expected-importer.css'), 'utf8').trim().replace(/\r\n/g, '\n');
it('should override imports and fire callback with file and contents', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_importer_file_and_data_cb.js')
]);
bin.once('close', function() {
assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest);
done();
});
});
it('should override imports and fire callback with file', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_importer_file_cb.js')
]);
bin.once('close', function() {
if (fs.existsSync(dest)) {
assert.equal(read(dest, 'utf8').trim(), '');
fs.unlinkSync(dest);
}
done();
});
});
it('should override imports and fire callback with data', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_importer_data_cb.js')
]);
bin.once('close', function() {
assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest);
done();
});
});
it('should override imports and return file and contents', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_importer_file_and_data.js')
]);
bin.once('close', function() {
assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest);
done();
});
});
it('should override imports and return file', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_importer_file.js')
]);
bin.once('close', function() {
if (fs.existsSync(dest)) {
assert.equal(read(dest, 'utf8').trim(), '');
fs.unlinkSync(dest);
}
done();
});
});
it('should override imports and return data', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_importer_data.js')
]);
bin.once('close', function() {
assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest);
done();
});
});
it('should accept arrays of importers and return respect the order', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_arrays_of_importers.js')
]);
bin.once('close', function() {
assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest);
done();
});
});
it('should return error for invalid importer file path', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('non/existing/path')
]);
bin.once('close', function(code) {
assert(code !== 0);
done();
});
});
it('should reflect user-defined Error', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_importer_error.js')
]);
bin.stderr.once('data', function(code) {
assert.equal(JSON.parse(code).message, 'doesn\'t exist!');
done();
});
});
});
describe('functions', function() {
it('should let custom functions call setter methods on wrapped sass values (number)', function(done) {
var dest = fixture('custom-functions/setter.css');
var src = fixture('custom-functions/setter.scss');
var expected = read(fixture('custom-functions/setter-expected.css'), 'utf8').trim().replace(/\r\n/g, '\n');
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--functions', fixture('extras/my_custom_functions_setter.js')
]);
bin.once('close', function() {
assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest);
done();
});
});
it('should properly convert strings when calling custom functions', function(done) {
var dest = fixture('custom-functions/string-conversion.css');
var src = fixture('custom-functions/string-conversion.scss');
var expected = read(fixture('custom-functions/string-conversion-expected.css'), 'utf8').trim().replace(/\r\n/g, '\n');
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--functions', fixture('extras/my_custom_functions_string_conversion.js')
]);
bin.once('close', function() {
assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest);
done();
});
});
});
});