Skip to content

Commit

Permalink
fixed #297 - streamline not passing options to script
Browse files Browse the repository at this point in the history
  • Loading branch information
bjouhier committed Nov 5, 2015
1 parent 7286e43 commit 66df474
Showing 1 changed file with 57 additions and 33 deletions.
90 changes: 57 additions & 33 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,72 @@ 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 <dir>', "save compiled .js files in the given directory")
.option('--no-cache', "turns caching off", true)
.option('--cache-dir <dir>', "sets the cache directory")
.option('-f, --force', "force transformation (even if found in cache)")
.option('--runtime <name>', "target runtime")
.option('-s, --source-maps [true|false|inline|both]', "(see babel)", /^(true|false|inline|both)$/)
.option('-s, --source-maps <true|false|inline|both>', "(see babel)", /^(true|false|inline|both)$/)
.option('--source-map-target <file>', "output file for source map")
.option('-q, --quiet', "don't log")
.option('--preload <modules>', "hook to preload a specified list of modules (comma-separated)")
.parse(argv); // commander skips 2 first args, not just first one.
.option('--preload <modules>', "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);
Expand Down

0 comments on commit 66df474

Please sign in to comment.