Skip to content

Commit

Permalink
Work around broken command line parsing of NW.js
Browse files Browse the repository at this point in the history
See comment in #613
  • Loading branch information
bastimeyer committed Jan 16, 2019
1 parent c24058e commit 0ecb4fd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
39 changes: 38 additions & 1 deletion src/app/nwjs/argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,65 @@ const argFilters = [
...( filteredArgv || [] ),
/^--user-data-dir=/,
/^--no-sandbox/,
/^--no-zygote/,
/^--flag-switches-(begin|end)/
];

/** @type {Function[]} */
const argTests = [
...minimistOptions.boolean,
...minimistOptions.string,
...Object.keys( minimistOptions.alias ).reduce( ( arr, key ) => {
arr.push( ...minimistOptions.alias[ key ] );
return arr;
}, [] )
].map( name => name.length === 1
// shorthand parameters
? ( arg => arg.length > 1 && arg.substr( 1 ) === name )
// named parameters
: ( arg => arg.length > 2 && arg[1] === "-" && arg.substr( 2 ) === name )
);


/**
* Turns a command line string into a parameter object
*
* NW.js doesn't correctly parse the passed command line from second application starts
* and the positions of parameters and their values will get shifted partially. There is nothing
* which can be done on the application code side to restore the original parameter order.
*
* Examples:
* - When executing `/path/to/executable --launch foo`:
* /path/to/executable --launch --chromium --specific --stuff foo
* - When executing `/path/to/executable --launch foo --goto bar --minimize`:
* /path/to/executable --launch --goto --minimize --chromium --specific --stuff foo bar
*
* The Chromium specific parameters are a mix of the custom NW.js chromium-args field and internal
* Chromium parameters. Those have to be removed in order to be able to at least restore
* single-parameter-and-value usage. Since internal Chromium parameters are unpredictable/unknown,
* we're simply removing any parameter with a leading dash that is not defined in the list above.
*
* @param {String} command
* @returns {Object}
*/
export function parseCommand( command ) {
// remove chromium args from the command line
/* istanbul ignore else */
if ( manifest && hasOwnProperty.call( manifest, "chromium-args" ) ) {
const chromiumArgs = manifest[ "chromium-args" ];
const pos = command.indexOf( chromiumArgs );
/* istanbul ignore else */
if ( pos !== -1 ) {
command = command.substr( 0, pos ) + command.substr( pos + chromiumArgs.length );
}
}

const argv = getParameters( { command }, parameters )
.slice( 1 )
.filter( arg => !argFilters.some( regex => regex.test( arg ) ) );
// remove known Chromium parameters
.filter( arg => !argFilters.some( regex => regex.test( arg ) ) )
// workaround (for now): only accept known application parameters
.filter( arg => arg[0] !== "-" || argTests.some( test => test( arg ) ) );

return minimist( argv, minimistOptions );
}
Expand Down
12 changes: 7 additions & 5 deletions src/test/tests/nwjs/argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,20 @@ test( "Parse command", assert => {
});

assert.propEqual(
// this is unfortunately how NW.js passes through the command line string from second
// application starts: parameters with leading dashes get moved to the beginning
parseCommand([
"/path/to/executable",
"--goto",
"--unrecognized-parameter-name",
"--foo",
"--bar",
"--user-data-dir=baz",
"--no-sandbox",
"--no-zygote",
"--flag-switches-begin",
"--flag-switches-end",
"--goto",
"foo",
"--launch",
"bar"
"foo"
].join( " " ) ),
{
"_": [],
Expand All @@ -200,7 +202,7 @@ test( "Parse command", assert => {
"loglevel": "",
"l": "",
"goto": "foo",
"launch": "bar"
"launch": ""
},
"Correctly parses parameters"
);
Expand Down

0 comments on commit 0ecb4fd

Please sign in to comment.