Skip to content

Commit

Permalink
feat(starter): automatically wipe the ELECTRON_RUN_AS_NODE variable u…
Browse files Browse the repository at this point in the history
…nless specified
  • Loading branch information
MarshallOfSound authored and malept committed Mar 5, 2017
1 parent 47c5572 commit c702fe4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
9 changes: 8 additions & 1 deletion src/api/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 () => {
Expand Down
21 changes: 8 additions & 13 deletions src/electron-forge-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -22,6 +21,7 @@ import { start } from './api';
.arguments('[cwd]')
.option('-p, --app-path <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)) {
Expand All @@ -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);
})();
21 changes: 21 additions & 0 deletions test/fast/electron_forge_start_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});

Expand All @@ -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,
});
});

Expand All @@ -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,
});
});

Expand All @@ -63,6 +69,8 @@ describe('electron-forge start', () => {
dir: process.cwd(),
appPath: path.join('foo', 'electron.js'),
interactive: true,
enableLogging: false,
runAsNode: false,
});
});

Expand All @@ -73,6 +81,7 @@ describe('electron-forge start', () => {
dir: process.cwd(),
enableLogging: true,
interactive: true,
runAsNode: false,
});
});

Expand All @@ -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,
});
});
});
22 changes: 22 additions & 0 deletions test/fast/start_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit c702fe4

Please sign in to comment.