From 66df474ca5100b9881e56f2bcfec858506abc925 Mon Sep 17 00:00:00 2001 From: Bruno Jouhier Date: Thu, 5 Nov 2015 10:53:08 +0100 Subject: [PATCH] fixed #297 - streamline not passing options to script --- lib/command.js | 90 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 33 deletions(-) diff --git a/lib/command.js b/lib/command.js index e71a6bc8..d6f9babc 100644 --- a/lib/command.js +++ b/lib/command.js @@ -18,36 +18,7 @@ var Module = require('module'); function parseOptions(argv) { var program = require('commander'); - // process obsolete options first, we don't want them in the help - var args = argv.slice(0, 2); - argv.slice(2).forEach(function(arg) { - if (arg === '--cache') { - // ignore silently - cache is now on by default. - } else if (/^(-l(m|i|p)|--(lines-(mark|ignore|preserve)|standalone|fast|old-style-future|promise|cb|aggressive))$/.test(arg)) { - util.warn('obsolete option ignored: ' + arg); - return; - } else if (arg === '--map') { - util.warn('obsolete option: --map, use -s or --source-maps instead'); - args.push('--source-maps'); - return; - } else if (arg === '--source-map') { - util.warn('obsolete option: --source-map, use --source-map-target instead'); - args.push('--source-map-target'); - } else if (/^--(fibers|generators)$/.test(arg)) { - util.warn('obsolete option: ' + arg + ', use --runtime ' + arg.substring(2) + ' instead'); - args.push('--runtime'); - args.push(arg.substring(2)); - } else if (/^(-o|--output-dir)$/.test(arg)) { - util.warn('obsolete option: ' + arg + ', use -d or --out-dir instead'); - args.push('-d'); - } else if (arg === '-v') { - util.warn('obsolete option: -v, verbose is on by default, use -q to turn it off'); - } else { - args.push(arg); - } - }); - - var options = program.version(require('../package').version, '-v, --version') + var prog = program.version(require('../package').version, '-v, --version') .usage('[options] [script] [arguments]') .option('-c, --compile', "compile [script] files and save as *.js files") .option('-d, --out-dir ', "save compiled .js files in the given directory") @@ -55,11 +26,64 @@ function parseOptions(argv) { .option('--cache-dir ', "sets the cache directory") .option('-f, --force', "force transformation (even if found in cache)") .option('--runtime ', "target runtime") - .option('-s, --source-maps [true|false|inline|both]', "(see babel)", /^(true|false|inline|both)$/) + .option('-s, --source-maps ', "(see babel)", /^(true|false|inline|both)$/) .option('--source-map-target ', "output file for source map") .option('-q, --quiet', "don't log") - .option('--preload ', "hook to preload a specified list of modules (comma-separated)") - .parse(argv); // commander skips 2 first args, not just first one. + .option('--preload ', "hook to preload a specified list of modules (comma-separated)"); + + // Arguments that follow the script or filename should not be intercepted by commander but passed + // verbatim to the script (https://github.com/Sage/streamlinejs/issues/297) + // There may be a clever way to do this with commander but the following hack should do for now + // I'm handling compat in the same loop to cut correctly if script accepts obsolete streamline options (-o for ex). + // commander skips 2 first args, not just first one. + var args = argv.slice(0, 2); + var cut = 2; + while (cut < argv.length) { + var arg = argv[cut]; + if (arg[0] !== '-') break; + cut++; + var opt = prog.options.filter(function(o) { + return o.short === arg || o.long === arg; + })[0]; + if (opt) { + args.push(arg); + if (opt.flags.indexOf('<') >= 0 && cut < argv.length) { + args.push(argv[cut++]); + } + } else { + // handle compat options + if (arg === '--cache') { + // ignore silently - cache is now on by default. + } else if (/^(-l(m|i|p)|--(lines-(mark|ignore|preserve)|standalone|fast|old-style-future|promise|cb|aggressive))$/.test(arg)) { + util.warn('obsolete option ignored: ' + arg); + return; + } else if (arg === '--map') { + util.warn('obsolete option: --map, use -s or --source-maps instead'); + args.push('--source-maps'); + args.push('true'); + return; + } else if (arg === '--source-map') { + util.warn('obsolete option: --source-map, use --source-map-target instead'); + args.push('--source-map-target'); + } else if (/^--(fibers|generators)$/.test(arg)) { + util.warn('obsolete option: ' + arg + ', use --runtime ' + arg.substring(2) + ' instead'); + args.push('--runtime'); + args.push(arg.substring(2)); + } else if (/^(-o|--output-dir)$/.test(arg)) { + util.warn('obsolete option: ' + arg + ', use -d or --out-dir instead'); + args.push('-d'); + if (cut < argv.length) args.push(argv[cut++]); + } else if (arg === '-v') { + util.warn('obsolete option: -v, verbose is on by default, use -q to turn it off'); + } else { + // push invalid option - commander will deal with it + args.push(arg); + } + } + } + + var options = prog.parse(args); + options.args = options.args.concat(argv.slice(cut)); options = Object.keys(options).filter(function(opt) { return !/^([A-Z]|_)/.test(opt);