Skip to content

Commit

Permalink
feat(plugin-compile): add the electron compile plugin into the monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound committed Jul 4, 2018
1 parent 37a1e07 commit 5907de5
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/api/core/src/api/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/base/src/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default abstract class Plugin<C> {
return null;
}

async startLogic(startOpts: StartOptions): Promise<ChildProcess | string | false> {
async startLogic(startOpts: StartOptions): Promise<ChildProcess | string | string[] | false> {
return false;
}
}
25 changes: 25 additions & 0 deletions packages/plugin/compile/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
25 changes: 25 additions & 0 deletions packages/plugin/compile/src/CompilePlugin.ts
Original file line number Diff line number Diff line change
@@ -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<CompilePluginConfig> {
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')];
}
}
2 changes: 2 additions & 0 deletions packages/plugin/compile/src/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export interface CompilePluginConfig {
}
38 changes: 38 additions & 0 deletions packages/plugin/compile/src/lib/compile-hook.ts
Original file line number Diff line number Diff line change
@@ -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);
});
};
2 changes: 1 addition & 1 deletion packages/utils/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { RebuildOptions } from 'electron-rebuild/lib/src/rebuild';
export interface IForgePluginInterface {
triggerHook(hookName: string, hookArgs: any[]): Promise<void>;
triggerMutatingHook<T>(hookName: string, item: T): Promise<any>;
overrideStartLogic(opts: any): Promise<ChildProcess | false>;
overrideStartLogic(opts: any): Promise<ChildProcess | string | string[] | false>;
}
export interface ForgeConfig {
/**
Expand Down

0 comments on commit 5907de5

Please sign in to comment.