Skip to content

Commit

Permalink
fix(core): keep track of application restarts and close stdin pipe co…
Browse files Browse the repository at this point in the history
…rrectly

previously we weren't tracking application restarts in the CLI module or closing our stdin pipe once
the child app had quit

ISSUES CLOSED: #545
  • Loading branch information
MarshallOfSound committed Sep 10, 2018
1 parent ee61540 commit a6d5b86
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
29 changes: 22 additions & 7 deletions packages/api/cli/src/electron-forge-start.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { api, StartOptions } from '@electron-forge/core';

import { ChildProcess } from 'child_process';
import fs from 'fs-extra';
import path from 'path';
import program from 'commander';
Expand Down Expand Up @@ -64,12 +65,26 @@ import './util/terminate';
const spawned = await api.start(opts);

await new Promise((resolve) => {
spawned.on('exit', (code: number) => {
if ((spawned as any).restarted) return;
if (code !== 0) {
process.exit(code);
}
resolve();
});
const listenForExit = (child: ChildProcess) => {
const removeListeners = () => {
child.removeListener('exit', onExit);
child.removeListener('restarted', onRestart);
};
const onExit = (code: number) => {
removeListeners();
if ((spawned as any).restarted) return;
if (code !== 0) {
process.exit(code);
}
resolve();
};
const onRestart = (newChild: ChildProcess) => {
removeListeners();
listenForExit(newChild);
};
child.on('exit', onExit);
child.on('restarted', onRestart);
};
listenForExit(spawned);
});
})();
19 changes: 17 additions & 2 deletions packages/api/core/src/api/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ export default async ({

const forgeSpawnWrapper = async () => {
lastSpawned = await forgeSpawn();
// When the child app is closed we should stop listening for stdin
if (lastSpawned) {
if (interactive && process.stdin.isPaused()) {
process.stdin.resume();
}
lastSpawned.on('exit', () => {
if ((lastSpawned as any).restarted) return;

if (!process.stdin.isPaused()) process.stdin.pause();
});
} else {
if (interactive && !process.stdin.isPaused()) {
process.stdin.pause();
}
}
return lastSpawned;
};

Expand Down Expand Up @@ -114,12 +129,12 @@ export default async ({
};

if (interactive) {
process.stdin.on('data', (data) => {
process.stdin.on('data', async (data) => {
if (data.toString().trim() === 'rs' && lastSpawned) {
console.info('\nRestarting App\n'.cyan);
(lastSpawned as any).restarted = true;
lastSpawned.kill('SIGTERM');
forgeSpawnWrapper();
lastSpawned.emit('restarted', await forgeSpawnWrapper());
}
});
}
Expand Down
9 changes: 5 additions & 4 deletions packages/api/core/test/fast/start_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('start', () => {
let packageJSON: any;
let resolveStub: SinonStub;
let spawnStub: SinonStub;
let shouldOverride: boolean;
let shouldOverride: any;
let processOn: SinonStub;

beforeEach(() => {
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('start', () => {

it('should not spawn if a plugin overrides the start command', async () => {
resolveStub.returnsArg(0);
shouldOverride = true;
shouldOverride = { on: () => {} };
await start({
dir: __dirname,
interactive: false,
Expand Down Expand Up @@ -172,12 +172,13 @@ describe('start', () => {

it('should resolve with a handle to the spawned instance', async () => {
resolveStub.returnsArg(0);
spawnStub.returns('child');
const fakeChild = { on: () => {} };
spawnStub.returns(fakeChild);

await expect(start({
dir: __dirname,
interactive: false,
enableLogging: true,
})).to.eventually.equal('child');
})).to.eventually.equal(fakeChild);
});
});

0 comments on commit a6d5b86

Please sign in to comment.