forked from sass/node-sass
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
358 lines (298 loc) · 10.9 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
var assert = require('assert'),
fs = require('fs'),
path = require('path'),
read = require('fs').readFileSync,
spawn = require('cross-spawn'),
cli = path.join(__dirname, '..', 'bin', 'node-sass'),
fixture = path.join.bind(null, __dirname, 'fixtures');
describe('cli', function() {
this.timeout(0);
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);
});
});
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();
});
bin.once('close', function() {console.log('entered close');
assert(false);
fs.unlinkSync(src);
done();
});
bin.once('exit', function() {console.log('entered exit');
assert(false);
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);
});
});
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();
});
});
});
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 return error on 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();
});
});
});
});