Commit ae752d24 by Kevin Martensson

Use meow instead of yargs

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