Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: create the ElectronProcess type (ChildProcess + restarted property) #1346

Merged
merged 1 commit into from
Dec 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/api/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"@electron-forge/async-ora": "6.0.0-beta.46",
"@electron-forge/core": "6.0.0-beta.46",
"@electron-forge/shared-types": "6.0.0-beta.46",
"@electron/get": "^1.6.0",
"colors": "^1.4.0",
"commander": "^4.0.1",
Expand Down
12 changes: 6 additions & 6 deletions packages/api/cli/src/electron-forge-start.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { api, StartOptions } from '@electron-forge/core';
import { ElectronProcess } from '@electron-forge/shared-types';

import { ChildProcess } from 'child_process';
import fs from 'fs-extra';
import program from 'commander';
import path from 'path';
Expand Down Expand Up @@ -59,28 +59,28 @@ import workingDir from './util/working-dir';
const spawned = await api.start(opts);

await new Promise((resolve) => {
const listenForExit = (child: ChildProcess) => {
const listenForExit = (child: ElectronProcess) => {
let onExit: NodeJS.ExitListener;
let onRestart: (newChild: ChildProcess) => void;
let onRestart: (newChild: ElectronProcess) => void;
const removeListeners = () => {
child.removeListener('exit', onExit);
child.removeListener('restarted', onRestart);
};
onExit = (code: number) => {
removeListeners();
if ((spawned as any).restarted) return;
if (spawned.restarted) return;
if (code !== 0) {
process.exit(code);
}
resolve();
};
onRestart = (newChild: ChildProcess) => {
onRestart = (newChild: ElectronProcess) => {
removeListeners();
listenForExit(newChild);
};
child.on('exit', onExit);
child.on('restarted', onRestart);
};
listenForExit(spawned as ChildProcess);
listenForExit(spawned as ElectronProcess);
});
})();
16 changes: 9 additions & 7 deletions packages/api/core/src/api/start.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'colors';
import { asyncOra } from '@electron-forge/async-ora';
import { StartOptions, ForgePlatform, ForgeArch } from '@electron-forge/shared-types';
import { spawn, ChildProcess, SpawnOptions } from 'child_process';
import {
ElectronProcess, ForgeArch, ForgePlatform, StartOptions,
} from '@electron-forge/shared-types';
import { spawn, SpawnOptions } from 'child_process';
import path from 'path';

import { readMutatedPackageJson } from '../util/read-package-json';
Expand Down Expand Up @@ -49,7 +51,7 @@ export default async ({

await runHook(forgeConfig, 'generateAssets');

let lastSpawned: ChildProcess | null = null;
let lastSpawned: ElectronProcess | null = null;

const forgeSpawn = async () => {
let electronExecPath: string | null = null;
Expand Down Expand Up @@ -101,14 +103,14 @@ export default async ({
args = ['--inspect' as (string|number)].concat(args);
}

let spawned!: ChildProcess;
let spawned!: ElectronProcess;

await asyncOra('Launching Application', async () => {
spawned = spawn(
electronExecPath!,
prefixArgs.concat([appPath]).concat(args as string[]),
spawnOpts as SpawnOptions,
);
) as ElectronProcess;
});

await runHook(forgeConfig, 'postStart', spawned);
Expand All @@ -123,7 +125,7 @@ export default async ({
process.stdin.resume();
}
spawned.on('exit', () => {
if ((spawned as any).restarted) return;
if (spawned.restarted) return;

if (!process.stdin.isPaused()) process.stdin.pause();
});
Expand All @@ -140,7 +142,7 @@ export default async ({
if (data.toString().trim() === 'rs' && lastSpawned) {
// eslint-disable-next-line no-console
console.info('\nRestarting App\n'.cyan);
(lastSpawned as any).restarted = true;
lastSpawned.restarted = true;
lastSpawned.kill('SIGTERM');
lastSpawned.emit('restarted', await forgeSpawnWrapper());
}
Expand Down
4 changes: 2 additions & 2 deletions packages/api/core/test/fast/start_spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ElectronProcess } from '@electron-forge/shared-types';
import { expect } from 'chai';
import { ChildProcess } from 'child_process';
import path from 'path';
import proxyquire from 'proxyquire';
import sinon, { SinonStub } from 'sinon';

import { StartOptions } from '../../src/api';

describe('start', () => {
let start: (opts: StartOptions) => Promise<ChildProcess>;
let start: (opts: StartOptions) => Promise<ElectronProcess>;
let packageJSON: any;
let resolveStub: SinonStub;
let spawnStub: SinonStub;
Expand Down
5 changes: 2 additions & 3 deletions packages/plugin/base/src/Plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
ForgeConfig, ForgeHookFn, IForgePlugin, StartOptions,
ElectronProcess, ForgeConfig, ForgeHookFn, IForgePlugin, StartOptions,
} from '@electron-forge/shared-types';
import { ChildProcess } from 'child_process';

export { StartOptions };

Expand All @@ -25,7 +24,7 @@ export default abstract class Plugin<C> implements IForgePlugin {
return null;
}

async startLogic(_startOpts: StartOptions): Promise<ChildProcess | string | string[] | false> {
async startLogic(_startOpts: StartOptions): Promise<ElectronProcess | string | string[] | false> {
return false;
}
}
7 changes: 3 additions & 4 deletions packages/plugin/webpack/src/WebpackPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint "no-console": "off" */
import { asyncOra } from '@electron-forge/async-ora';
import PluginBase from '@electron-forge/plugin-base';
import { ForgeConfig } from '@electron-forge/shared-types';
import { ElectronProcess, ForgeConfig } from '@electron-forge/shared-types';
import Logger, { Tab } from '@electron-forge/web-multi-logger';
import { ChildProcess } from 'child_process';
import debug from 'debug';
import fs from 'fs-extra';
import merge from 'webpack-merge';
Expand Down Expand Up @@ -137,14 +136,14 @@ export default class WebpackPlugin extends PluginBase<WebpackPluginConfig> {
await this.compileRenderers();
};
case 'postStart':
return async (_: any, child: ChildProcess) => {
return async (_: any, child: ElectronProcess) => {
if (!this.loggedOutputUrl) {
console.info(`\n\nWebpack Output Available: ${(`http://localhost:${this.loggerPort}`).cyan}\n`);
this.loggedOutputUrl = true;
}
d('hooking electron process exit');
child.on('exit', () => {
if ((child as any).restarted) return;
if (child.restarted) return;
this.exitHandler({ cleanup: true, exit: true });
});
};
Expand Down
6 changes: 4 additions & 2 deletions packages/utils/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { ChildProcess } from 'child_process';
import { Options } from 'electron-packager';
import { RebuildOptions } from 'electron-rebuild/lib/src/rebuild';

export type ElectronProcess = ChildProcess & { restarted: boolean };

export type ForgePlatform = 'darwin' | 'mas' | 'win32' | 'linux';
export type ForgeArch = 'ia32' | 'x64' | 'armv7l' | 'arm64' | 'arm' | 'all';
export type ForgeHookFn = (forgeConfig: ForgeConfig, ...args: any[]) => Promise<any>;
export type ForgeConfigPublisher = IForgeResolvablePublisher | IForgePublisher | string;
export interface IForgePluginInterface {
triggerHook(hookName: string, hookArgs: any[]): Promise<void>;
triggerMutatingHook<T>(hookName: string, item: T): Promise<any>;
overrideStartLogic(opts: any): Promise<ChildProcess | string | string[] | false>;
overrideStartLogic(opts: any): Promise<ElectronProcess | string | string[] | false>;
}
export interface ForgeConfig {
/**
Expand Down Expand Up @@ -61,7 +63,7 @@ export interface IForgePlugin {

init(dir: string, forgeConfig: ForgeConfig): void;
getHook?(hookName: string): ForgeHookFn | null;
startLogic?(opts: StartOptions): Promise<ChildProcess | string | string[] | false>;
startLogic?(opts: StartOptions): Promise<ElectronProcess | string | string[] | false>;
}

export interface IForgeResolvableMaker {
Expand Down