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

feat(plugin-local-electron): add plugin-local-electron #501

Merged
merged 4 commits into from
May 3, 2018
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"packages/installer/*",
"packages/maker/*",
"packages/publisher/*",
"packages/utils/*"
"packages/utils/*",
"packages/plugin/*"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plugin or plugins?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plugin, it's consistent with installer and maker

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

util perhaps 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup will follow up on that

]
},
"config": {
Expand Down
9 changes: 8 additions & 1 deletion packages/api/core/src/api/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import requireSearch from '../util/require-search';
import resolveDir from '../util/resolve-dir';
import getCurrentOutDir from '../util/out-dir';
import getElectronVersion from '../util/electron-version';
import electronVersion from '../util/electron-version';

const { hostArch }: { hostArch: () => ForgeArch | 'all'} = require('electron-packager/targets');

Expand Down Expand Up @@ -154,12 +155,18 @@ export default async ({
done();
}) as ElectronPackagerAfterCopyHook);

const afterExtractHooks = [(async (buildPath, electronVersion, pPlatform, pArch, done) => {
await runHook(forgeConfig, 'packageAfterExtract', buildPath, electronVersion, pPlatform, pArch);
done();
}) as ElectronPackagerAfterCopyHook];
afterExtractHooks.push(...resolveHooks(forgeConfig.packagerConfig.afterExtract, dir));

const packageOpts: packager.Options = Object.assign({
asar: false,
overwrite: true,
}, forgeConfig.packagerConfig, {
afterCopy: sequentialHooks(afterCopyHooks),
afterExtract: sequentialHooks(resolveHooks(forgeConfig.packagerConfig.afterExtract, dir)),
afterExtract: sequentialHooks(afterExtractHooks),
afterPrune: sequentialHooks(afterPruneHooks),
dir,
arch,
Expand Down
42 changes: 10 additions & 32 deletions packages/api/core/src/api/start.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'colors';
import { asyncOra } from '@electron-forge/async-ora';
import { StartOptions } from '@electron-forge/shared-types';
import { spawn, ChildProcess } from 'child_process';
import path from 'path';

Expand All @@ -11,36 +12,7 @@ import runHook from '../util/hook';
import getElectronVersion from '../util/electron-version';
import { ForgePlatform, ForgeArch } from '@electron-forge/shared-types';

export interface StartOptions {
/**
* The path to the electron forge project to run
*/
dir?: string;
/**
* The path (relative to dir) to the electron app to run relative to the project directory
*/
appPath?: string;
/**
* Whether to use sensible defaults or prompt the user visually
*/
interactive?: boolean;
/**
* Enables advanced internal Electron debug calls
*/
enableLogging?: boolean;
/**
* Arguments to pass through to the launched Electron application
*/
args?: (string | number)[];
/**
* Runs the Electron process as if it were node, disables all Electron API's
*/
runAsNode?: boolean;
/**
* Enables the node inspector, you can connect to this from chrome://inspect
*/
inspect?: boolean;
}
export { StartOptions };

export default async ({
dir = process.cwd(),
Expand Down Expand Up @@ -79,6 +51,8 @@ export default async ({

await runHook(forgeConfig, 'generateAssets');

let electronExecPath = path.resolve(dir, 'node_modules/electron/cli');

// If a plugin has taken over the start command let's stop here
const spawnedPluginChild = await forgeConfig.pluginInterface.overrideStartLogic({
dir,
Expand All @@ -89,7 +63,11 @@ export default async ({
runAsNode,
inspect,
});
if (spawnedPluginChild) return spawnedPluginChild;
if (typeof spawnedPluginChild === 'string') {
electronExecPath = spawnedPluginChild;
} else if (spawnedPluginChild) {
return spawnedPluginChild;
}

const spawnOpts = {
cwd: dir,
Expand All @@ -113,7 +91,7 @@ export default async ({
let spawned!: ChildProcess;

await asyncOra('Launching Application', async () => {
spawned = spawn(process.execPath, [path.resolve(dir, 'node_modules/electron/cli'), appPath].concat(args as string[]), spawnOpts);
spawned = spawn(process.execPath, [electronExecPath, appPath].concat(args as string[]), spawnOpts);
});

return spawned;
Expand Down
1 change: 0 additions & 1 deletion packages/api/core/src/util/plugin-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export default class PluginInterface implements IForgePluginInterface {
}
}

// FIXME: any
async overrideStartLogic(opts: StartOptions) {
let newStartFn;
const claimed = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { fromBuildIdentifier } = require('../../../src/api');
const { utils: { fromBuildIdentifier } } = require('../../../src/api');

module.exports = {
buildIdentifier: 'beta',
Expand Down
23 changes: 23 additions & 0 deletions packages/plugin/base/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@electron-forge/plugin-base",
"version": "6.0.0-beta.5",
"description": "Base plugin for Electron Forge",
"repository": "https://github.com/electron-userland/electron-forge",
"author": "Samuel Attard",
"license": "MIT",
"main": "dist/Plugin.js",
"typings": "dist/Plugin.d.ts",
"scripts": {
"test": "exit 0"
},
"devDependencies": {
"chai": "^4.0.0",
"mocha": "^5.0.0"
},
"engines": {
"node": ">= 6.0"
},
"dependencies": {
"@electron-forge/shared-types": "6.0.0-beta.5"
}
}
25 changes: 25 additions & 0 deletions packages/plugin/base/src/Plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ForgeHookFn, StartOptions } from '@electron-forge/shared-types';
import { ChildProcess } from 'child_process';

export { StartOptions };

export default abstract class Plugin<C> {
public abstract name: string;
__isElectronForgePlugin!: true;

constructor(public config: C) {
Object.defineProperty(this, '__isElectronForgePlugin', {
value: true,
enumerable: false,
configurable: false,
});
}

getHook(hookName: string): ForgeHookFn | null {
return null;
}

async startLogic(startOpts: StartOptions): Promise<ChildProcess | string | false> {
return false;
}
}
25 changes: 25 additions & 0 deletions packages/plugin/local-electron/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@electron-forge/plugin-local-electron",
"version": "6.0.0-beta.5",
"description": "Local Electron plugin for Electron Forge, let's you use a local build of Electron",
"repository": "https://github.com/electron-userland/electron-forge",
"author": "Samuel Attard",
"license": "MIT",
"main": "dist/LocalElectronPlugin.js",
"typings": "dist/LocalElectronPlugin.d.ts",
"scripts": {
"test": "mocha --require ts-node/register test/**/*_spec.ts test/**/**/*_spec.ts --opts ../../../mocha.opts"
},
"devDependencies": {
"chai": "^4.0.0",
"fs-extra": "^5.0.0",
"mocha": "^5.0.0"
},
"engines": {
"node": ">= 6.0"
},
"dependencies": {
"@electron-forge/plugin-base": "6.0.0-beta.5",
"fs-extra": "^5.0.0"
}
}
32 changes: 32 additions & 0 deletions packages/plugin/local-electron/src/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export interface LocalElectronPluginConfig {
/**
* Whether or not the plugin is enabled.
*
* Can be handy to set this to an environment variable for quick personal
* toggling of this plugin.
*
* Default: `true`
*/
enabled?: boolean;
/**
* An absolute path to the folder containing your built version of Electron.
*
* Normally this looks like `/path/to/electron/out/D`
*/
electronPath: string;
/**
* The platform your local build of Electron is for. You only need to set
* this if you have a local build for a platform that isn't your system's
* platform.
*
* Default: process.platform
*/
electronPlatform?: string;
/**
* The arch your local build of Electron is for. You only need to set this if
* you have a local build for an arch that isn't your system's arch.
*
* Default: process.arch
*/
electronArch?: string;
}
57 changes: 57 additions & 0 deletions packages/plugin/local-electron/src/LocalElectronPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import PluginBase from '@electron-forge/plugin-base';
import { spawn } from 'child_process';
import fs from 'fs-extra';

import { LocalElectronPluginConfig } from './Config';

export default class LocalElectronPlugin extends PluginBase<LocalElectronPluginConfig> {
name = 'local-electron';

constructor(config: LocalElectronPluginConfig) {
super(config);
if (typeof this.config.enabled === 'undefined') {
this.config = {
...this.config,
enabled: true,
};
}
}

async startLogic() {
if (this.config.enabled) {
this.checkPlatform(process.platform);
process.env.ELECTRON_OVERRIDE_DIST_PATH = this.config.electronPath;
}
return false as any;
}

getHook(hookName: string) {
if (hookName === 'packageAfterExtract') {
return this.afterExtract;
}
return null;
}

private checkPlatform = (platform: string) => {
if ((this.config.electronPlatform || process.platform) !== platform) {
throw `Can not use local Electron version, required platform "${platform}" but local platform is "${this.config.electronPlatform || process.platform}"`
}
}

private checkArch = (arch: string) => {
if ((this.config.electronArch || process.arch) !== arch) {
throw `Can not use local Electron version, required arch "${arch}" but local arch is "${this.config.electronArch || process.arch}"`
}
}

private afterExtract = async (_: any, buildPath: string, __: any, platform: string, arch: string) => {
if (!this.config.enabled) return;

this.checkPlatform(platform);
this.checkArch(arch);

await fs.remove(buildPath);

await fs.copy(this.config.electronPath, buildPath);
}
}
Loading