Commit ae752d24 by Kevin Martensson

Use meow instead of yargs

parent 550bb158
......@@ -8,6 +8,7 @@ cli.on('log', function(msg){
cli.on('error', function(msg){
console.error(msg);
process.exit(1);
});
cli.on('warn', function(msg){
......
var watch = require('node-watch'),
var watch = require('node-watch'),
render = require('./render'),
path = require('path'),
Emitter = require('events').EventEmitter,
stdin = require('get-stdin'),
meow = require('meow'),
cwd = process.cwd();
var yargs = require('yargs')
.usage('Compile .scss files with node-sass.\nUsage: $0 [options] <input.scss> [<output.css>]')
.version(require('../package.json').version, 'version').alias('version', 'V')
.options('output-style', {
describe: 'CSS output style (nested|expanded|compact|compressed)',
'default': 'nested'
})
.options('source-comments', {
describe: 'Include debug info in output (none|normal|map)',
'default': 'none'
})
.options('source-map', {
describe: 'Emit source map'
})
.options('include-path', {
describe: 'Path to look for @import-ed files',
'default': cwd
})
.options('image-path', {
describe: 'Path to prepend when using the image-url(…) helper',
'default': ''
})
.options('precision', {
describe: 'The amount of precision allowed in decimal numbers',
'default': 5
})
.options('watch', {
describe: 'Watch a directory or file',
alias: 'w',
type: 'boolean'
})
.options('output', {
describe: 'Output css file',
alias: 'o'
})
.options('stdout', {
describe: 'Print the resulting CSS to stdout',
type: 'boolean'
})
.options('omit-source-map-url', {
describe: 'Omit source map URL comment from output',
type: 'boolean',
alias: 'x'
})
.options('indented-syntax', {
describe: 'Treat data from stdin as sass code (versus scss)',
type: 'boolean',
alias: 'i'
})
.options('help', {
describe: 'Print usage info',
type: 'string',
alias: 'help'
})
.check(function(argv) {
if (argv.help) { return true; }
if (!argv._.length && (process.stdin.isTTY || process.env.isTTY)) {
return false;
function init(args) {
return meow({
argv: args,
pkg: '../package.json',
help: [
' Usage',
' node-sass [options] <input.scss> [output.css]',
' cat <input.scss> | node-sass > output.css',
'',
' Example',
' node-sass --output-style compressed foobar.scss foobar.css',
' cat foobar.scss | node-sass --output-style compressed > foobar.css',
'',
' Options',
' -w, --watch Watch a directory or file',
' -o, --output Output CSS file',
' -x, --omit-source-map-url Omit source map URL comment from output',
' -i, --indented-syntax Treat data from stdin as sass code (versus scss)',
' --output-style CSS output style (nested|expanded|compact|compressed)',
' --source-comments Include debug info in output (none|normal|map)',
' --source-map Emit source map',
' --include-path Path to look for imported files',
' --image-path Path to prepend when using the `image-url()` helper',
' --precision The amount of precision allowed in decimal numbers',
' --stdout Print the resulting CSS to stdout',
' --help Print usage info'
].join('\n')
}, {
boolean: [
'indented-syntax',
'omit-source-map-url',
'stdout',
'watch'
],
string: [
'image-path',
'include-path',
'output',
'output-style',
'precision',
'source-comments'
],
alias: {
i: 'indented-syntax',
o: 'output',
w: 'watch',
x: 'omit-source-map-url'
},
default: {
'image-path': '',
'include-path': cwd,
'output-style': 'nested',
precision: 5,
'source-comments': 'none'
}
});
}
// throttle function, used so when multiple files change at the same time
// (e.g. git pull) the files are only compiled once.
function throttle(fn) {
var timer;
var args = Array.prototype.slice.call(arguments, 1);
var args = [].slice.call(arguments, 1);
return function() {
var self = this;
......@@ -86,16 +82,8 @@ function isSassFile(file) {
}
function run(options, emitter) {
if (!Array.isArray(options.includePaths)) {
options.includePaths = [options.includePaths];
}
if (Array.isArray(options.outputStyle)) {
options.outputStyle = options.outputStyle[0];
}
if (Array.isArray(options.sourceComments)) {
options.sourceComments = options.sourceComments[0];
if (!Array.isArray(options.includePath)) {
options.includePath = [options.includePath];
}
if (options.sourceMap) {
......@@ -133,34 +121,18 @@ function run(options, emitter) {
}
module.exports = function(args) {
var argv = yargs.parse(args);
var cli = init(args);
var emitter = new Emitter();
var options = {
imagePath: argv['image-path'],
includePaths: argv['include-path'],
omitSourceMapUrl: argv['omit-source-map-url'],
indentedSyntax: argv['indented-syntax'],
outputStyle: argv['output-style'],
precision: argv.precision,
sourceComments: argv['source-comments'],
sourceMap: argv['source-map'],
stdout: argv.stdout,
watch: argv.w
};
if (argv.help) {
yargs.showHelp();
process.exit();
return;
}
var input = cli.input;
var options = cli.flags;
emitter.on('error', function(err) {
console.error(err);
process.exit(1);
});
options.src = argv._[0];
options.dest = argv.o || argv._[1];
options.src = input[0] || null;
options.dest = options.output || input[1] || null;
if (!options.dest && (process.stdout.isTTY || process.env.isTTY)) {
var suffix = '.css';
......@@ -171,6 +143,16 @@ module.exports = function(args) {
}
if (process.stdin.isTTY || process.env.isTTY) {
if (!input.length) {
console.error([
'Provide a sass file to render',
'',
' Example',
' node-sass --output-style compressed foobar.scss foobar.css',
' cat foobar.scss | node-sass --output-style compressed > foobar.css'
].join('\n'));
process.exit(1);
}
run(options, emitter);
} else {
stdin(function(data) {
......@@ -181,5 +163,3 @@ module.exports = function(args) {
return emitter;
};
module.exports.yargs = yargs;
......@@ -5,10 +5,10 @@ var sass = require('../sass'),
function render(options, emitter) {
var renderOptions = {
imagePath: options.imagePath,
includePaths: options.includePaths,
includePaths: options.includePath,
omitSourceMapUrl: options.omitSourceMapUrl,
indentedSyntax: options.indentedSyntax,
outFile: options.outFile,
outFile: options.dest,
outputStyle: options.outputStyle,
precision: options.precision,
sourceComments: options.sourceComments,
......
......@@ -42,15 +42,15 @@
},
"dependencies": {
"chalk": "~0.5.1",
"get-stdin": "~3.0.0",
"meow": "^2.0.0",
"mkdirp": "~0.5.0",
"mocha": "~1.21.5",
"nan": "~1.3.0",
"node-watch": "~0.3.4",
"object-assign": "~1.0.0",
"shelljs": "~0.3.0",
"sinon": "~1.10.3",
"yargs": "~1.3.2",
"get-stdin": "~3.0.0"
"sinon": "~1.10.3"
},
"devDependencies": {
"coveralls": "^2.11.1",
......
......@@ -71,16 +71,16 @@ describe('cli', function() {
// when hit the callback in the following,
// it means that data is recieved, so we are ok to go.
emitter.stdout.on('data', function() { done(); });
emitter.stdout.on('data', function() { done(); });
src.pipe(emitter.stdin);
});
it('should print help when run with no arguments', function(done) {
it('should print usage when run with no arguments', function(done) {
var env = assign(process.env, { isTTY: true });
exec('node ' + cliPath, {
env: env
}, function(err, stdout, stderr) {
done(assert(stderr.trim().indexOf('Compile .scss files with node-sass') === 0));
done(assert(stderr.trim().indexOf('Provide a sass file to render') === 0));
});
});
......
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