Commit 2c21fd7b by Arian Stolwijk

Merge node-sass-cli into node-sass, for a better cli with watch.

- Using node-watch to watch directories or files, exposed with the
  --watch option
- Updated optimist dependency
- Split bin/node-sass into a lib/cli.js and bin/node-sass file, so the
  cli can be required (especially good for testing or extending)
parent 65ee8fdd
#!/usr/bin/env node
var path = require('path'),
fs = require('fs'),
colors = require('colors'),
sass = require(path.join(__dirname, '..', 'sass')),
cwd = process.cwd(),
optimist = require('optimist')
.usage('Compile .scss files with node-sass\nUsage: node-sass [options] <input.scss> [<output.css>]')
.describe('output-style', 'CSS output style (nested|expanded|compact|compressed)')
.default('output-style', 'nested')
.describe('source-comments', 'Include debug info in output (none|normal|map)')
.default('source-comments', 'none')
.describe('include-path', 'Path to look for @import-ed files')
.default('include-path', cwd)
.string('h').alias('h', 'help')
.describe('help', 'Print usage info'),
argv = optimist.argv,
inFile = argv._[0],
outFile = argv._[1];
if(argv.help || !inFile) return optimist.showHelp();
console.log('Starting Render Process...'.green);
sass.render({
file: inFile,
includePaths: [argv['include-path']],
outputStyle: argv['output-style'],
sourceComments: argv['source-comments'],
success: function(css) {
console.log('Rendering Complete, saving .css file...'.green);
if(!outFile) outFile = path.join(cwd, path.basename(inFile, '.scss') + '.css');
fs.writeFile(outFile, css, function(err) {
if(err) return console.log(('Error: ' + err).red);
console.log(('Wrote CSS to ' + outFile).green);
});
},
error: function(err) {
console.log('** Error Rendering SASS **'.red);
console.log(err.yellow);
}
});
require('../lib/cli')(process.argv.slice(2));
var watch = require('node-watch'),
render = require('./render'),
path = require('path'),
cwd = process.cwd();
var optimist = require('optimist')
.usage('Compile .scss files with node-sass.\nUsage: $0 [options] <input.scss> [<output.css>]')
.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('include-path', {
describe: 'Path to look for @import-ed files',
'default': cwd
})
.options('watch', {
describe: 'Watch a directory or file',
alias: 'w'
})
.options('output', {
describe: 'Output css file',
alias: 'o'
})
.options('stdout', {
describe: 'Print the resulting CSS to stdout'
})
.options('help', {
describe: 'Print usage info',
type: 'string',
alias: 'help'
})
.check(function(argv){
if (argv.help) return true;
if (argv._.length < 1) 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, args) {
var timer;
return function() {
var self = this;
clearTimeout(timer);
timer = setTimeout(function() {
fn.call(self, args);
}, 20);
};
}
function isSassFile(file) {
return file.match(/\.(sass|scss)/);
}
exports = module.exports = function(args) {
var argv = optimist.parse(args);
if (argv.help) {
optimist.showHelp();
process.exit(0);
return;
}
var options = {
stdout: argv.stdout
};
var inFile = options.inFile = argv._[0];
var outFile = options.outFile = argv.o || argv._[1];
if (!outFile) {
outFile = options.outFile = path.join(cwd, path.basename(inFile, '.scss') + '.css');
}
// make sure it's an array
if (!Array.isArray(options['include-path'])) {
options['include-paths'] = options.includePaths = [options['include-path']];
}
options.includePaths = options['include-paths'];
// if it's an array, make it a string
if (Array.isArray(options['output-style'])) {
options['output-style'] = options['output-style'][0];
}
options.outputStyle = options['output-style'];
// if it's an array, make it a string
if (Array.isArray(options['source-comments'])) {
options['source-comments'] = options['source-comments'][0];
}
options.sourceComments = options['source-comments'];
if (argv.w) {
var watchDir = argv.w;
if (watchDir === true) {
watchDir = []
} else if (!Array.isArray(watchDir)) {
watchDir = [watchDir];
}
watchDir.push(inFile)
var throttledRender = throttle(render, options);
watch(watchDir, function(file){
console.warn('=> changed: '.grey + file.blue);
if (isSassFile(file)) {
throttledRender();
}
});
throttledRender();
} else {
render(options);
}
};
exports.optimist = optimist;
var sass = require('../sass'),
colors = require('colors'),
fs = require('fs');
var cwd = process.cwd();
function render(options) {
sass.render({
file: options.inFile,
includePaths: options.includePaths,
outputStyle: options.outputStyle,
sourceComments: options.sourceComments,
success: function(css) {
console.warn('Rendering Complete, saving .css file...'.green);
fs.writeFile(options.outFile, css, function(err) {
if (err) return console.error(('Error: ' + err).red);
console.warn(('Wrote CSS to ' + options.outFile).green);
});
if (options.stdout) {
console.log(css);
}
},
error: function(error) {
console.error(error);
}
});
}
module.exports = render;
......@@ -37,7 +37,8 @@
"dependencies": {
"mkdirp": "0.3.x",
"colors": "0.6.0-1",
"optimist": "0.4.x"
"optimist": "0.6.x",
"node-watch": "0.3.x"
},
"devDependencies": {
"mocha": "1.7.x"
......
......@@ -88,3 +88,4 @@ exports.renderSync = function(options) {
};
exports.middleware = require('./lib/middleware');
exports.cli = require('./lib/cli');
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