Skip to content
Permalink
Browse files

feat(maker): basic hooks for preMake, postMake, generateAssets, prePa…

…ckage and postPackage
  • Loading branch information
MarshallOfSound committed Apr 20, 2017
1 parent 1b10fd5 commit 1a17189bbfbd4e9fcdfdc5523ca45198279a760e
Showing with 57 additions and 1 deletion.
  1. +11 −1 src/api/make.js
  2. +6 −0 src/api/package.js
  3. +5 −0 src/api/start.js
  4. +13 −0 src/util/hook.js
  5. +21 −0 test/fast/hook_spec.js
  6. +1 −0 test/fast/start_spec.js
@@ -5,6 +5,7 @@ import path from 'path';
import asyncOra from '../util/ora-handler';
import electronHostArch from '../util/electron-host-arch';
import getForgeConfig from '../util/forge-config';
import runHook from '../util/hook';
import { info, warn } from '../util/messages';
import readPackageJSON from '../util/read-package-json';
import requireSearch from '../util/require-search';
@@ -97,7 +98,9 @@ export default async (providedOptions = {}) => {

const packageJSON = await readPackageJSON(dir);
const appName = forgeConfig.electronPackagerConfig.name || packageJSON.productName || packageJSON.name;
const outputs = [];
let outputs = [];

await runHook(forgeConfig, 'preMake');

for (const targetArch of targetArchs) {
const packageDir = path.resolve(outDir, `${appName}-${declaredPlatform}-${targetArch}`);
@@ -135,5 +138,12 @@ export default async (providedOptions = {}) => {
}
}

const result = await runHook(forgeConfig, 'postMake', outputs);
// If the postMake hooks modifies the locations / names of the outputs it must return
// the new locations so that the publish step knows where to look
if (Array.isArray(result)) {
outputs = result;
}

return outputs;
};
@@ -8,6 +8,7 @@ import packager from 'electron-packager';

import electronHostArch from '../util/electron-host-arch';
import getForgeConfig from '../util/forge-config';
import runHook from '../util/hook';
import ora from '../util/ora';
import packagerCompileHook from '../util/compile-hook';
import readPackageJSON from '../util/read-package-json';
@@ -108,9 +109,14 @@ export default async (providedOptions = {}) => {
throw new Error('electron-compile does not support asar.unpack yet. Please use asar.unpackDir');
}

await runHook(forgeConfig, 'generateAssets');
await runHook(forgeConfig, 'prePackage');

d('packaging with options', packageOpts);

await pify(packager)(packageOpts);

await runHook(forgeConfig, 'postPackage');

packagerSpinner.succeed();
};
@@ -6,6 +6,8 @@ import asyncOra from '../util/ora-handler';
import readPackageJSON from '../util/read-package-json';
import rebuild from '../util/rebuild';
import resolveDir from '../util/resolve-dir';
import getForgeConfig from '../util/forge-config';
import runHook from '../util/hook';

/**
* @typedef {Object} StartOptions
@@ -62,6 +64,9 @@ export default async (providedOptions = {}) => {

let spawned;

const forgeConfig = await getForgeConfig(dir);
await runHook(forgeConfig, 'generateAssets');

await asyncOra('Launching Application', async () => {
/* istanbul ignore if */
if (process.platform === 'win32') {
@@ -0,0 +1,13 @@
import debug from 'debug';

const d = debug('electron-forge:hook');

export default async (forgeConfig, hookName, ...hookArgs) => {
const hooks = forgeConfig.hooks || {};
if (typeof hooks[hookName] === 'function') {
d('calling hook:', hookName, 'with args:', hookArgs);
await hooks[hookName](forgeConfig, ...hookArgs);
} else {
d('could not find hook:', hookName);
}
};
@@ -0,0 +1,21 @@
import { expect } from 'chai';
import { stub } from 'sinon';

import runHook from '../../src/util/hook';

describe('runHook', () => {
it('should not error when running non existent hooks', async () => {
await runHook({}, 'magic');
});

it('should not error when running a hook that is not a function', async () => {
await runHook({ hooks: { myHook: 'abc' } }, 'abc');
});

it('should run the hook if it is provided as a function', async () => {
const myStub = stub();
myStub.returns(Promise.resolve());
await runHook({ hooks: { myHook: myStub } }, 'myHook');
expect(myStub.callCount).to.equal(1);
});
});
@@ -14,6 +14,7 @@ describe('start', () => {
resolveStub = sinon.stub();
spawnStub = sinon.stub();
start = proxyquire.noCallThru().load('../../src/api/start', {
'../util/forge-config': async () => ({}),
'../util/resolve-dir': async dir => resolveStub(dir),
'../util/read-package-json': () => Promise.resolve(require('../fixture/dummy_app/package.json')),
'../util/rebuild': () => Promise.resolve(),

0 comments on commit 1a17189

Please sign in to comment.
You can’t perform that action at this time.