Commit 9cb67840 by Andrew Nesbitt

Merge pull request #117 from msokk/cli

CLI Cleanup
parents 1d397cce 59107b82
......@@ -129,8 +129,17 @@ Replace the prebuild binary with your newly generated one
The interface for command-line usage is fairly simplistic at this stage, as seen in the following usage section.
Output will be saved with the same name as input SASS file into the current working directory if it's omitted.
### Usage
`node-sass [input.scss] [output.css]`
`node-sass [options] <input.scss> [<output.css>]`
**Options:**
--output-style CSS output style (nested|expanded|compact|compressed) [default: "nested"]
--source-comments Include debug info in output (none|normal|map) [default: "none"]
--include-path Path to look for @import-ed files [default: cwd]
--help, -h Print usage info
## Contributors
Special thanks to the following people for submitting patches:
......
#!/usr/bin/env node
var colors = require('colors');
var fs = require('fs');
var sass = require('../sass');
var path = require('path');
var argv = require('optimist').argv;
var fileName = argv._[0];
var cssFileName = argv._[1];
var outputStyle = argv['output-style'];
var cwd = argv.cwd || process.cwd();
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);
if (fileName) {
fs.readFile(path.join(cwd, fileName), "utf8", function(err, data) {
if (err) {
console.log("** Error Opening File **".red);
console.log(JSON.stringify(err, null, 4).yellow);
} else {
console.log('File data read successfully, rendering css'.green);
renderSASS(data);
}
});
} else {
console.log("** Please Pass a filename to compile **".red);
}
function renderSASS(data) {
sass.render(data, function(err, compiled) {
if (err) {
console.log("** Error Rendering SASS **".red);
console.log(JSON.stringify(err, null, 4).yellow);
} else {
console.log('Rendering Complete, saving .css file...'.green);
var outFile;
if (cssFileName) {
outFile = cssFileName;
} else {
outFile = fileName.slice(fileName.lastIndexOf('/') + 1,
fileName.lastIndexOf('.')) + '.css';
}
writeCssFile(outFile || 'nodesass.css', compiled);
}
}, {include_paths: [cwd], output_style: outputStyle});
}
function writeCssFile(filename, data) {
fs.writeFile(path.join(cwd, filename), data, function(err){
if(err) {
console.log('Error: ' + err);
} else {
console.log('File saved! New .css file: ' + filename);
}
});
}
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);
}
});
var path = require('path'),
assert = require('assert'),
fs = require('fs'),
exec = require('child_process').exec,
sass = require(path.join(__dirname, '..', 'sass')),
cliPath = path.resolve(__dirname, '..', 'bin', 'node-sass'),
sampleFilename = path.resolve(__dirname, 'sample.scss');
describe('cli', function() {
it('should print help when run with no arguments', function(done) {
exec(cliPath, function(err, stdout, stderr) {
done(assert(stderr.indexOf('Compile .scss files with node-sass') === 0));
});
});
it('should compile sample.scss as sample.css', function(done) {
var resultPath = path.join(__dirname, 'sample.css');
exec(cliPath + ' ' + sampleFilename, {
cwd: __dirname
}, function(err, stdout, stderr) {
fs.exists(resultPath, function(exists) {
done(assert(exists));
fs.unlink(resultPath, function() {});
});
});
});
it('should compile sample.scss to ../out.css', function(done) {
var resultPath = path.resolve(__dirname, '..', 'out.css');
exec(cliPath + ' ' + sampleFilename + ' ../out.css', {
cwd: __dirname
}, function(err, stdout, stderr) {
fs.exists(resultPath, function(exists) {
done(assert(exists));
fs.unlink(resultPath, function() {});
});
});
});
});
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