Commit 7a9238dd by Adeel

CLI: Do not require input file on watch.

* Removes input file parameter from watch test.
* Uses `once` instead of `on` in CLI tests.
  * Removes `bin.kill` in favour of `once`.

Issue URL: #755.
PR URL: #761.
parent 2c88e9cd
...@@ -54,7 +54,8 @@ var cli = meow({ ...@@ -54,7 +54,8 @@ var cli = meow({
'include-path', 'include-path',
'output', 'output',
'output-style', 'output-style',
'precision' 'precision',
'watch'
], ],
alias: { alias: {
c: 'source-comments', c: 'source-comments',
...@@ -125,7 +126,7 @@ function getEmitter() { ...@@ -125,7 +126,7 @@ function getEmitter() {
*/ */
function getOptions(args, options) { function getOptions(args, options) {
options.src = args[0]; options.src = options.watch ? options.watch : args[0];
if (args[1]) { if (args[1]) {
options.dest = path.resolve(process.cwd(), args[1]); options.dest = path.resolve(process.cwd(), args[1]);
...@@ -227,15 +228,14 @@ function run(options, emitter) { ...@@ -227,15 +228,14 @@ function run(options, emitter) {
* Arguments and options * Arguments and options
*/ */
var input = cli.input; var options = getOptions(cli.input, cli.flags);
var options = getOptions(input, cli.flags);
var emitter = getEmitter(); var emitter = getEmitter();
/** /**
* Show usage if no arguments are supplied * Show usage if no arguments are supplied
*/ */
if (!input.length && process.stdin.isTTY) { if (!options.src && process.stdin.isTTY) {
emitter.emit('error', [ emitter.emit('error', [
'Provide a Sass file to render', 'Provide a Sass file to render',
'', '',
......
...@@ -73,7 +73,7 @@ describe('cli', function() { ...@@ -73,7 +73,7 @@ describe('cli', function() {
var dest = fixture('simple/index.css'); var dest = fixture('simple/index.css');
var bin = spawn(cli, [src, dest]); var bin = spawn(cli, [src, dest]);
bin.on('close', function() { bin.once('close', function() {
assert(fs.existsSync(dest)); assert(fs.existsSync(dest));
fs.unlinkSync(dest); fs.unlinkSync(dest);
process.chdir(__dirname); process.chdir(__dirname);
...@@ -88,7 +88,7 @@ describe('cli', function() { ...@@ -88,7 +88,7 @@ describe('cli', function() {
var dest = fixture('simple/index-custom.css'); var dest = fixture('simple/index-custom.css');
var bin = spawn(cli, [src, dest]); var bin = spawn(cli, [src, dest]);
bin.on('close', function() { bin.once('close', function() {
assert(fs.existsSync(dest)); assert(fs.existsSync(dest));
fs.unlinkSync(dest); fs.unlinkSync(dest);
process.chdir(__dirname); process.chdir(__dirname);
...@@ -118,7 +118,7 @@ describe('cli', function() { ...@@ -118,7 +118,7 @@ describe('cli', function() {
var bin = spawn(cli, [src, '--watch']); var bin = spawn(cli, [src, '--watch']);
var exited; var exited;
bin.on('close', function() { bin.once('close', function() {
exited = true; exited = true;
}); });
...@@ -133,15 +133,15 @@ describe('cli', function() { ...@@ -133,15 +133,15 @@ describe('cli', function() {
}); });
it('should emit `warn` on file change when using --watch option', function(done) { it('should emit `warn` on file change when using --watch option', function(done) {
fs.writeFileSync(fixture('simple/tmp.scss'), '');
var src = fixture('simple/tmp.scss'); var src = fixture('simple/tmp.scss');
var bin = spawn(cli, [src, '--watch']);
fs.writeFileSync(src, '');
var bin = spawn(cli, ['--watch', src]);
bin.stderr.setEncoding('utf8'); bin.stderr.setEncoding('utf8');
bin.stderr.on('data', function(data) { bin.stderr.once('data', function(data) {
assert(data.trim() === '=> changed: ' + src); assert(data.trim() === '=> changed: ' + src);
bin.kill();
fs.unlinkSync(src); fs.unlinkSync(src);
done(); done();
}); });
...@@ -152,27 +152,24 @@ describe('cli', function() { ...@@ -152,27 +152,24 @@ describe('cli', function() {
}); });
it('should render all watched files', function(done) { it('should render all watched files', function(done) {
fs.writeFileSync(fixture('simple/foo.scss'), ''); var src = fixture('simple/bar.scss');
fs.writeFileSync(fixture('simple/bar.scss'), '');
fs.writeFileSync(src, '');
var src = fixture('simple/foo.scss');
var watched = fixture('simple/bar.scss');
var bin = spawn(cli, [ var bin = spawn(cli, [
src, '--watch', watched, '--output-style', 'compressed',
'--output-style', 'compressed' '--watch', src
]); ]);
bin.stdout.setEncoding('utf8'); bin.stdout.setEncoding('utf8');
bin.stdout.on('data', function(data) { bin.stdout.once('data', function(data) {
assert(data.trim() === 'body{background:white}'); assert(data.trim() === 'body{background:white}');
bin.kill();
fs.unlinkSync(src); fs.unlinkSync(src);
fs.unlinkSync(watched);
done(); done();
}); });
setTimeout(function() { setTimeout(function() {
fs.appendFileSync(watched, 'body{background:white}'); fs.appendFileSync(src, 'body{background:white}');
}, 500); }, 500);
}); });
}); });
...@@ -183,7 +180,7 @@ describe('cli', function() { ...@@ -183,7 +180,7 @@ describe('cli', function() {
var dest = fixture('simple/index.css'); var dest = fixture('simple/index.css');
var bin = spawn(cli, [src, '--output', path.dirname(dest)]); var bin = spawn(cli, [src, '--output', path.dirname(dest)]);
bin.on('close', function() { bin.once('close', function() {
assert(fs.existsSync(dest)); assert(fs.existsSync(dest));
fs.unlinkSync(dest); fs.unlinkSync(dest);
done(); done();
...@@ -198,7 +195,7 @@ describe('cli', function() { ...@@ -198,7 +195,7 @@ describe('cli', function() {
var expectedMap = read(fixture('source-map/expected.map'), '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]); var bin = spawn(cli, [src, '--output', path.dirname(destCss), '--source-map', destMap]);
bin.on('close', function() { bin.once('close', function() {
assert.equal(read(destCss, 'utf8').trim(), expectedCss); assert.equal(read(destCss, 'utf8').trim(), expectedCss);
assert.equal(read(destMap, 'utf8').trim(), expectedMap); assert.equal(read(destMap, 'utf8').trim(), expectedMap);
fs.unlinkSync(destCss); fs.unlinkSync(destCss);
...@@ -216,7 +213,7 @@ describe('cli', function() { ...@@ -216,7 +213,7 @@ describe('cli', function() {
'--source-map', map, '--omit-source-map-url' '--source-map', map, '--omit-source-map-url'
]); ]);
bin.on('close', function() { bin.once('close', function() {
assert(read(dest, 'utf8').indexOf('sourceMappingURL') === -1); assert(read(dest, 'utf8').indexOf('sourceMappingURL') === -1);
assert(fs.existsSync(map)); assert(fs.existsSync(map));
fs.unlinkSync(map); fs.unlinkSync(map);
...@@ -232,7 +229,7 @@ describe('cli', function() { ...@@ -232,7 +229,7 @@ describe('cli', function() {
var dest = fixture('output-directory/path/to/file/index.css'); var dest = fixture('output-directory/path/to/file/index.css');
var bin = spawn(cli, [src, '--output', path.dirname(dest)]); var bin = spawn(cli, [src, '--output', path.dirname(dest)]);
bin.on('close', function() { bin.once('close', function() {
assert(fs.existsSync(path.dirname(dest))); assert(fs.existsSync(path.dirname(dest)));
fs.unlinkSync(dest); fs.unlinkSync(dest);
fs.rmdirSync(path.dirname(dest)); fs.rmdirSync(path.dirname(dest));
...@@ -257,7 +254,7 @@ describe('cli', function() { ...@@ -257,7 +254,7 @@ describe('cli', function() {
'--importer', fixture('extras/my_custom_importer_file_and_data_cb.js') '--importer', fixture('extras/my_custom_importer_file_and_data_cb.js')
]); ]);
bin.on('close', function() { bin.once('close', function() {
assert.equal(read(dest, 'utf8').trim(), expected); assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest); fs.unlinkSync(dest);
done(); done();
...@@ -270,7 +267,7 @@ describe('cli', function() { ...@@ -270,7 +267,7 @@ describe('cli', function() {
'--importer', fixture('extras/my_custom_importer_file_cb.js') '--importer', fixture('extras/my_custom_importer_file_cb.js')
]); ]);
bin.on('close', function() { bin.once('close', function() {
if (fs.existsSync(dest)) { if (fs.existsSync(dest)) {
assert.equal(read(dest, 'utf8').trim(), ''); assert.equal(read(dest, 'utf8').trim(), '');
fs.unlinkSync(dest); fs.unlinkSync(dest);
...@@ -286,7 +283,7 @@ describe('cli', function() { ...@@ -286,7 +283,7 @@ describe('cli', function() {
'--importer', fixture('extras/my_custom_importer_data_cb.js') '--importer', fixture('extras/my_custom_importer_data_cb.js')
]); ]);
bin.on('close', function() { bin.once('close', function() {
assert.equal(read(dest, 'utf8').trim(), expected); assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest); fs.unlinkSync(dest);
done(); done();
...@@ -299,7 +296,7 @@ describe('cli', function() { ...@@ -299,7 +296,7 @@ describe('cli', function() {
'--importer', fixture('extras/my_custom_importer_file_and_data.js') '--importer', fixture('extras/my_custom_importer_file_and_data.js')
]); ]);
bin.on('close', function() { bin.once('close', function() {
assert.equal(read(dest, 'utf8').trim(), expected); assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest); fs.unlinkSync(dest);
done(); done();
...@@ -312,7 +309,7 @@ describe('cli', function() { ...@@ -312,7 +309,7 @@ describe('cli', function() {
'--importer', fixture('extras/my_custom_importer_file.js') '--importer', fixture('extras/my_custom_importer_file.js')
]); ]);
bin.on('close', function() { bin.once('close', function() {
if (fs.existsSync(dest)) { if (fs.existsSync(dest)) {
assert.equal(read(dest, 'utf8').trim(), ''); assert.equal(read(dest, 'utf8').trim(), '');
fs.unlinkSync(dest); fs.unlinkSync(dest);
...@@ -328,7 +325,7 @@ describe('cli', function() { ...@@ -328,7 +325,7 @@ describe('cli', function() {
'--importer', fixture('extras/my_custom_importer_data.js') '--importer', fixture('extras/my_custom_importer_data.js')
]); ]);
bin.on('close', function() { bin.once('close', function() {
assert.equal(read(dest, 'utf8').trim(), expected); assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest); fs.unlinkSync(dest);
done(); done();
...@@ -341,7 +338,7 @@ describe('cli', function() { ...@@ -341,7 +338,7 @@ describe('cli', function() {
'--importer', fixture('non/existing/path') '--importer', fixture('non/existing/path')
]); ]);
bin.on('close', function(code) { bin.once('close', function(code) {
assert(code !== 0); assert(code !== 0);
done(); done();
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment