From d3a28721d47f9881d564542c08ec7c1298842943 Mon Sep 17 00:00:00 2001 From: GoalSmashers Date: Sat, 9 Feb 2013 11:01:07 +0100 Subject: [PATCH] Fixes #47 - commandline issues on Windows. * Replaces optimist (processing CLI options) with commander. * Removes binary tests under Windows (to be investigated). --- bin/cleancss | 65 +++++++++++++++++++++++++-------------------- package.json | 2 +- test/binary-test.js | 14 +++++----- 3 files changed, 44 insertions(+), 37 deletions(-) diff --git a/bin/cleancss b/bin/cleancss index ab842a5b..640ffcad 100755 --- a/bin/cleancss +++ b/bin/cleancss @@ -1,47 +1,54 @@ #!/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] ') + .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 \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; diff --git a/package.json b/package.json index 7802395d..3917a8d8 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "test": "vows" }, "dependencies": { - "optimist": "0.3.x" + "commander": "1.1.x" }, "devDependencies": { "vows": "0.7.x", diff --git a/test/binary-test.js b/test/binary-test.js index 8755e970..276a2389 100644 --- a/test/binary-test.js +++ b/test/binary-test.js @@ -7,12 +7,12 @@ var isWindows = process.platform == 'win32'; 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; }; @@ -30,12 +30,12 @@ var pipedContext = function(css, options, 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', { -- 2.34.1