fix(CLI): always omit first two elements of process.argv (#891)

This is a great simplification to the logic previously included in the
Yargs `.parse()` function on the Etcher CLI.

There is no scenario where the CLI script will be executed in a
standalone fashion (e.g: `etcher.js <options>`), so we can always safely
drop the first two elements from `process.argv`.

Change-Type: patch
Changelog-Entry: Fix command line arguments not interpreted correctly when running the CLI with a custom named NodeJS binary.
Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
This commit is contained in:
Juan Cruz Viotti 2016-11-25 12:56:54 -04:00 committed by GitHub
parent 3eac1762b1
commit 7d51dea35d

View File

@ -18,7 +18,6 @@
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const yargs = require('yargs');
const utils = require('./utils');
const EXIT_CODES = require('../src/exit-codes');
@ -123,38 +122,4 @@ module.exports = yargs
default: true
}
})
// Make sure arguments are parsed correctly when running
// in development mode (or NPM) and in a final package.
//
// We rely on the following heuristics to determine if the
// Etcher CLI is being run as a final packaged executable
// or by a tool passing the script as an argument:
.parse(_.attempt((argv) => {
// Excluding the extension makes sure we cover cases
// like *.exe and *.cmd in Windows systems.
const executable = path.basename(argv[0], path.extname(argv[0])).toLowerCase();
if (_.includes([
'node',
'electron',
'electron helper'
], executable)) {
// In this case, the second argument (e.g: index 1)
// equals `lib/cli/etcher.js`, so the real arguments
// start from the third one.
return argv.slice(2);
}
// Handle the case where the CLI is executed by pointing
// the packaged electron `Etcher` binary to the application
// asar archive by using `ELECTRON_RUN_AS_NODE`.
if (argv[1] && path.basename(argv[1]) === 'app.asar') {
return argv.slice(2);
}
return argv.slice(1);
}, process.argv));
.parse(process.argv.slice(2));