#!/usr/bin/env node
var util = require("util");
- fs = require('fs'),
- path = require('path'),
- argv = require('optimist').argv,
- CleanCSS = require('../index');
+var fs = require('fs');
+var path = require('path');
+var CleanCSS = require('../index');
+
+var commands = require('commander');
+
+var packageConfig = fs.readFileSync(path.join(path.dirname(fs.realpathSync(process.argv[1])), '../package.json'));
+var buildVersion = JSON.parse(packageConfig).version;
+
+// Specify commander options to parse command line params correctly
+commands
+ .version(buildVersion, '-v, --version')
+ .usage('[options] <source-file>')
+ .option('-e, --remove-empty', 'Remove empty declarations (e.g. a{})')
+ .option('-b, --keep-line-breaks', 'Keep line breaks')
+ .option('--s0', 'Remove all special comments (i.e. /*! special comment */)')
+ .option('--s1', 'Remove all special comments but the first one')
+ .option('-o, --output [output-file]', 'Use [output-file] as output instead of stdout')
+ .parse(process.argv);
var options = {
source: null,
target: null
};
var cleanOptions = {};
-var fromStdin = !process.env['__DIRECT__'] && process.stdin.readable;
+var fromStdin = !process.env['__DIRECT__'] && process.stdin.writable;
+
+// If no sensible data passed in just print help and exit
+if (!fromStdin && commands.args.length == 0) {
+ commands.outputHelp();
+ return 0;
+}
-if (argv.o)
- options.target = argv.o;
-if (argv._)
- options.source = argv._[0];
-if (argv.e || argv.empty || argv.removeempty)
+// Now coerce commands into CleanCSS configuration...
+if (commands.output)
+ options.target = commands.output;
+if (commands.args.length > 0)
+ options.source = commands.args[0];
+if (commands.removeEmpty)
cleanOptions.removeEmpty = true;
-if (argv.b || argv.keepbreaks || argv.keeplinebreaks)
+if (commands.keepLineBreaks)
cleanOptions.keepBreaks = true;
-if (argv.s1)
+if (commands.s1)
cleanOptions.keepSpecialComments = 1;
-if (argv.s0)
+if (commands.s0)
cleanOptions.keepSpecialComments = 0;
-if (argv.v) {
- var packageConfig = fs.readFileSync(path.join(path.dirname(fs.realpathSync(process.argv[1])), '../package.json'));
- util.puts(JSON.parse(packageConfig).version);
- return 0;
-}
-
-if (argv.h || argv.help || (!fromStdin && argv._.length == 0)) {
- util.puts("usage: cleancss [options] -o <output-file> <input-file>\n");
- util.puts("options:");
- util.puts(" -e\tRemove empty declarations (e.g. a{})");
- util.puts(" -b\tKeep line breaks");
- util.puts(" --s0\tRemove all special comments (i.e. /*! special comment */)");
- util.puts(" --s1\tRemove all special comments but the first one");
- return 0;
-}
-
+// ... and do the magic!
if (options.source) {
fs.readFile(options.source, 'utf8', function(error, text) {
if (error) throw error;
var lineBreak = isWindows ? /\r\n/g : /\n/g;
var binaryContext = function(options, context) {
+ if (isWindows)
+ return {};
+
context.topic = function() {
- // We add __DIRECT__=1 to switch binary into 'non-piped' mode
- if (isWindows)
- exec("set __DIRECT__=1 & node .\\bin\\cleancss " + options, this.callback);
- else
- exec("__DIRECT__=1 ./bin/cleancss " + options, this.callback);
+ // We add __DIRECT__=1 to force binary into 'non-piped' mode
+ exec("__DIRECT__=1 ./bin/cleancss " + options, this.callback);
};
return context;
};
exports.commandsSuite = vows.describe('binary commands').addBatch({
'no options': binaryContext('', {
'should output help': function(stdout) {
- assert.equal(/usage:/.test(stdout), true);
+ assert.equal(/Usage:/.test(stdout), true);
}
}),
'help': binaryContext('-h', {
'should output help': function(error, stdout) {
- assert.equal(/usage:/.test(stdout), true);
+ assert.equal(/Usage:/.test(stdout), true);
}
}),
'version': binaryContext('-v', {