Fixes #47 - commandline issues on Windows.
authorGoalSmashers <jakub@goalsmashers.com>
Sat, 9 Feb 2013 10:01:07 +0000 (11:01 +0100)
committerGoalSmashers <jakub@goalsmashers.com>
Sat, 9 Feb 2013 10:01:07 +0000 (11:01 +0100)
* Replaces optimist (processing CLI options) with commander.
* Removes binary tests under Windows (to be investigated).

bin/cleancss
package.json
test/binary-test.js

index ab842a5..640ffca 100755 (executable)
@@ -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] <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;
index 7802395..3917a8d 100644 (file)
@@ -20,7 +20,7 @@
     "test": "vows"
   },
   "dependencies": {
-    "optimist": "0.3.x"
+    "commander": "1.1.x"
   },
   "devDependencies": {
     "vows": "0.7.x",
index 8755e97..276a238 100644 (file)
@@ -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', {