Skip to content

Commit

Permalink
fix argv handling and add fix for #8
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdesl committed Jan 20, 2016
1 parent e05b5dd commit d959bac
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
27 changes: 20 additions & 7 deletions lib/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
var _process = remote.process;
var cwd = _process.cwd();

// get an absolute path to our entry point
var entry = remote.getGlobal('__electronEntryFile');
if (entry) {
entry = path.isAbsolute(entry) ? entry : path.resolve(cwd, entry);
}

// setup process to be more like Node.js
hookProcess();

Expand All @@ -33,12 +39,6 @@
};
}

// get an absolute path to our entry point
var entry = remote.getGlobal('__electronEntryFile');
if (entry) {
entry = path.isAbsolute(entry) ? entry : path.resolve(cwd, entry);
}

// hook into the internal require for a few features:
// - better error reporting on syntax errors and missing modules
// - require.main acts like node.js CLI
Expand All @@ -65,9 +65,22 @@
function hookProcess () {
// setup renderer process to look a bit more like node
process.chdir(cwd);
process.argv = _process.argv;
process.argv = _process.argv.slice();
process.exit = _process.exit.bind(_process);

// Remove the Electron argument to make it more
// like Node.js argv handling. User can still
// grab remote.process.argv for original.
process.argv.shift();

// if -- is passed, all options after it will be the
// new user arguments
var stopIdx = process.argv.indexOf('--');
if (stopIdx >= 0) {
var start = process.argv.slice(0, entry ? 2 : 1);
process.argv = start.concat(process.argv.slice(stopIdx + 1));
}

var isTTY = remote.getGlobal('__electronProcessTTY');
process.stdin.isTTY = isTTY.stdin;
process.stdout.isTTY = isTTY.stdout;
Expand Down
1 change: 1 addition & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var BrowserWindow = electron.BrowserWindow;
var ipc = electron.ipcMain;

var argv = require('minimist')(process.argv.slice(2), {
'--': true,
boolean: [
'console', 'quit', 'poll', 'show', 'headless',
'browser-field'
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/argv.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
process.stdout.write(JSON.stringify(process.argv.slice(2)));
window.close();
window.close();
5 changes: 4 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ var cmd = path.resolve(__dirname, '..', 'bin', 'index.js');
var test = require('tape');

setup('process.argv', 'argv.js', JSON.stringify([
path.resolve(__dirname, 'fixtures', 'argv.js'),
'--foobar'
]), [ '--foobar' ]);

setup('process.argv with full stop', 'argv.js', JSON.stringify([
'some', '--arg'
]), [ '--foobar', '--bar', '--', 'some', '--arg' ]);

test('process.stdin', function (t) {
t.plan(1);
t.timeoutAfter(4000);
Expand Down

0 comments on commit d959bac

Please sign in to comment.