diff --git a/packages/api/core/src/api/start.ts b/packages/api/core/src/api/start.ts index 50614bf024..1881c11740 100644 --- a/packages/api/core/src/api/start.ts +++ b/packages/api/core/src/api/start.ts @@ -69,8 +69,12 @@ export default async ({ runAsNode, inspect, }); + let prefixArgs: string[] = []; if (typeof spawnedPluginChild === 'string') { electronExecPath = spawnedPluginChild; + } else if (Array.isArray(spawnedPluginChild)) { + electronExecPath = spawnedPluginChild[0]; + prefixArgs = spawnedPluginChild.slice(1); } else if (spawnedPluginChild) { await runHook(forgeConfig, 'postStart', spawnedPluginChild); return spawnedPluginChild; @@ -98,7 +102,7 @@ export default async ({ let spawned!: ChildProcess; await asyncOra('Launching Application', async () => { - spawned = spawn(electronExecPath, [appPath].concat(args as string[]), spawnOpts); + spawned = spawn(electronExecPath, prefixArgs.concat([appPath]).concat(args as string[]), spawnOpts); }); await runHook(forgeConfig, 'postStart', spawned); diff --git a/packages/plugin/base/src/Plugin.ts b/packages/plugin/base/src/Plugin.ts index 91c9f77362..afab284caf 100644 --- a/packages/plugin/base/src/Plugin.ts +++ b/packages/plugin/base/src/Plugin.ts @@ -21,7 +21,7 @@ export default abstract class Plugin { return null; } - async startLogic(startOpts: StartOptions): Promise { + async startLogic(startOpts: StartOptions): Promise { return false; } } diff --git a/packages/plugin/compile/package.json b/packages/plugin/compile/package.json new file mode 100644 index 0000000000..6997f661d7 --- /dev/null +++ b/packages/plugin/compile/package.json @@ -0,0 +1,25 @@ +{ + "name": "@electron-forge/plugin-compile", + "version": "6.0.0-beta.21", + "description": "Electron Compile plugin for Electron Forge", + "repository": "https://github.com/electron-userland/electron-forge", + "author": "Samuel Attard", + "license": "MIT", + "main": "dist/CompilePlugin.js", + "typings": "dist/CompilePlugin.d.ts", + "scripts": { + "test": "exit 0" + }, + "devDependencies": { + "chai": "^4.0.0", + "mocha": "^5.0.0" + }, + "engines": { + "node": ">= 6.0" + }, + "dependencies": { + "@electron-forge/async-ora": "6.0.0-beta.21", + "@electron-forge/plugin-base": "6.0.0-beta.21", + "fs-extra": "^5.0.0" + } +} diff --git a/packages/plugin/compile/src/CompilePlugin.ts b/packages/plugin/compile/src/CompilePlugin.ts new file mode 100644 index 0000000000..5af9eb40f9 --- /dev/null +++ b/packages/plugin/compile/src/CompilePlugin.ts @@ -0,0 +1,25 @@ +import PluginBase, { StartOptions } from '@electron-forge/plugin-base'; +import * as path from 'path'; + +import { CompilePluginConfig } from './Config'; +import { createCompileHook } from './lib/compile-hook'; + +export default class LocalElectronPlugin extends PluginBase { + name = 'electron-compile'; + private dir!: string; + + init(dir: string) { + this.dir = dir; + } + + getHook(hookName: string) { + if (hookName === 'packageAfterCopy') { + return createCompileHook(this.dir); + } + return null; + } + + async startLogic(opts: StartOptions) { + return [process.execPath, path.resolve(this.dir, 'node_modules/electron-prebuilt-compile/lib/cli')]; + } +} diff --git a/packages/plugin/compile/src/Config.ts b/packages/plugin/compile/src/Config.ts new file mode 100644 index 0000000000..4d8419bad3 --- /dev/null +++ b/packages/plugin/compile/src/Config.ts @@ -0,0 +1,2 @@ +export interface CompilePluginConfig { +} diff --git a/packages/plugin/compile/src/lib/compile-hook.ts b/packages/plugin/compile/src/lib/compile-hook.ts new file mode 100644 index 0000000000..2adf667f95 --- /dev/null +++ b/packages/plugin/compile/src/lib/compile-hook.ts @@ -0,0 +1,38 @@ +import { asyncOra } from '@electron-forge/async-ora'; +import fs from 'fs-extra'; +import path from 'path'; + +export const createCompileHook = (originalDir: string) => + async (_: any, buildPath: string) => { + await asyncOra('Compiling Application', async () => { + const compileCLI = require(path.resolve(originalDir, 'node_modules/electron-compile/lib/cli.js')); + + async function compileAndShim(appDir: string) { + for (const entry of await fs.readdir(appDir)) { + if (!entry.match(/^(node_modules|bower_components)$/)) { + const fullPath = path.join(appDir, entry); + + if ((await fs.stat(fullPath)).isDirectory()) { + const log = console.log; + console.log = () => {}; + await compileCLI.main(appDir, [fullPath]); + console.log = log; + } + } + } + + const packageJSON = await fs.readJson(path.resolve(appDir, 'package.json')); + + const index = packageJSON.main || 'index.js'; + packageJSON.originalMain = index; + packageJSON.main = 'es6-shim.js'; + + await fs.writeFile(path.join(appDir, 'es6-shim.js'), + await fs.readFile(path.join(path.resolve(originalDir, 'node_modules/electron-compile/lib/es6-shim.js')), 'utf8')); + + await fs.writeJson(path.join(appDir, 'package.json'), packageJSON, { spaces: 2 }); + } + + await compileAndShim(buildPath); + }); + }; diff --git a/packages/utils/types/src/index.ts b/packages/utils/types/src/index.ts index 186393fa78..9b00cf11dc 100644 --- a/packages/utils/types/src/index.ts +++ b/packages/utils/types/src/index.ts @@ -11,7 +11,7 @@ import { RebuildOptions } from 'electron-rebuild/lib/src/rebuild'; export interface IForgePluginInterface { triggerHook(hookName: string, hookArgs: any[]): Promise; triggerMutatingHook(hookName: string, item: T): Promise; - overrideStartLogic(opts: any): Promise; + overrideStartLogic(opts: any): Promise; } export interface ForgeConfig { /**