diff --git a/src/api/start.js b/src/api/start.js index 3c3da06359..24147c25d7 100644 --- a/src/api/start.js +++ b/src/api/start.js @@ -24,12 +24,13 @@ import resolveDir from '../util/resolve-dir'; */ export default async (providedOptions = {}) => { // eslint-disable-next-line prefer-const, no-unused-vars - let { dir, interactive, enableLogging, appPath, args } = Object.assign({ + let { dir, interactive, enableLogging, appPath, args, runAsNode } = Object.assign({ dir: process.cwd(), appPath: '.', interactive: false, enableLogging: false, args: [], + runAsNode: false, }, providedOptions); asyncOra.interactive = interactive; @@ -53,6 +54,12 @@ export default async (providedOptions = {}) => { } : {}), }; + if (runAsNode) { + spawnOpts.env.ELECTRON_RUN_AS_NODE = true; + } else { + delete spawnOpts.env.ELECTRON_RUN_AS_NODE; + } + let spawned; await asyncOra('Launching Application', async () => { diff --git a/src/electron-forge-start.js b/src/electron-forge-start.js index bcfaafaefa..8de6f61799 100644 --- a/src/electron-forge-start.js +++ b/src/electron-forge-start.js @@ -6,12 +6,11 @@ import './util/terminate'; import { start } from './api'; (async () => { - let commandArgs; + let commandArgs = process.argv; let appArgs; + const tripleDashIndex = process.argv.indexOf('---'); - if (tripleDashIndex === -1) { - commandArgs = process.argv; - } else { + if (tripleDashIndex !== -1) { commandArgs = process.argv.slice(0, tripleDashIndex); appArgs = process.argv.slice(tripleDashIndex + 1); } @@ -22,6 +21,7 @@ import { start } from './api'; .arguments('[cwd]') .option('-p, --app-path ', "Override the path to the Electron app to launch (defaults to '.')") .option('-l, --enable-logging', 'Enable advanced logging. This will log internal Electron things') + .option('-n, --run-as-node', 'Run the Electron app as a Node.JS script') .action((cwd) => { if (!cwd) return; if (path.isAbsolute(cwd) && fs.existsSync(cwd)) { @@ -43,17 +43,12 @@ import { start } from './api'; const opts = { dir, interactive: true, + enableLogging: !!program.enableLogging, + runAsNode: !!program.runAsNode, }; - if (program.appPath) { - opts.appPath = program.appPath; - } - if (program.enableLogging) { - opts.enableLogging = program.enableLogging; - } - if (appArgs) { - opts.args = appArgs; - } + if (program.appPath) opts.appPath = program.appPath; + if (appArgs) opts.args = appArgs; await start(opts); })(); diff --git a/test/fast/electron_forge_start_spec.js b/test/fast/electron_forge_start_spec.js index 11359bfa6b..ba28dc43b3 100644 --- a/test/fast/electron_forge_start_spec.js +++ b/test/fast/electron_forge_start_spec.js @@ -35,6 +35,8 @@ describe('electron-forge start', () => { expect(startStub.firstCall.args[0]).to.deep.equal({ dir: process.cwd(), interactive: true, + enableLogging: false, + runAsNode: false, }); }); @@ -44,6 +46,8 @@ describe('electron-forge start', () => { expect(startStub.firstCall.args[0]).to.deep.equal({ dir: path.join(process.cwd(), 'test', 'fixture', 'dummy_app'), interactive: true, + enableLogging: false, + runAsNode: false, }); }); @@ -53,6 +57,8 @@ describe('electron-forge start', () => { expect(startStub.firstCall.args[0]).to.deep.equal({ dir: path.join(process.cwd(), 'test', 'fixture', 'dummy_app'), interactive: true, + enableLogging: false, + runAsNode: false, }); }); @@ -63,6 +69,8 @@ describe('electron-forge start', () => { dir: process.cwd(), appPath: path.join('foo', 'electron.js'), interactive: true, + enableLogging: false, + runAsNode: false, }); }); @@ -73,6 +81,7 @@ describe('electron-forge start', () => { dir: process.cwd(), enableLogging: true, interactive: true, + runAsNode: false, }); }); @@ -84,6 +93,18 @@ describe('electron-forge start', () => { enableLogging: true, interactive: true, args: ['-a', 'foo', '-l'], + runAsNode: false, + }); + }); + + it('should handle run-as-node', async () => { + await runCommand(['--run-as-node']); + expect(startStub.callCount).to.equal(1); + expect(startStub.firstCall.args[0]).to.deep.equal({ + dir: process.cwd(), + enableLogging: false, + interactive: true, + runAsNode: true, }); }); }); diff --git a/test/fast/start_spec.js b/test/fast/start_spec.js index 2baceb4345..69102f40d3 100644 --- a/test/fast/start_spec.js +++ b/test/fast/start_spec.js @@ -68,6 +68,28 @@ describe('start', () => { expect(spawnStub.firstCall.args[2].env).to.have.property('ELECTRON_ENABLE_LOGGING', true); }); + it('should enable RUN_AS_NODE if runAsNode=true', async () => { + resolveStub.returnsArg(0); + await start({ + dir: __dirname, + interactive: false, + runAsNode: true, + }); + expect(spawnStub.callCount).to.equal(1); + expect(spawnStub.firstCall.args[2].env).to.have.property('ELECTRON_RUN_AS_NODE', true); + }); + + it('should disable RUN_AS_NODE if runAsNode=false', async () => { + resolveStub.returnsArg(0); + await start({ + dir: __dirname, + interactive: false, + runAsNode: false, + }); + expect(spawnStub.callCount).to.equal(1); + expect(spawnStub.firstCall.args[2].env).to.not.have.property('ELECTRON_RUN_AS_NODE'); + }); + it('should throw if no dir could be found', async () => { resolveStub.returns(null);