Skip to content

Commit

Permalink
feat(rebuilder): allow configuration of electron-rebuild
Browse files Browse the repository at this point in the history
Put an electronRebuildConfig in the forge config and pass it through to electron-rebuild.
  • Loading branch information
Ben Demboski authored and malept committed Jan 8, 2018
1 parent 8945e0a commit b986f26
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 28 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ config object:
"linux": ["deb", "rpm", "flatpak"] // An array of linux make targets
},
"electronPackagerConfig": {},
"electronRebuildConfig": {},
"electronWinstallerConfig": {},
"electronInstallerDMG": {},
"electronInstallerFlatpak": {},
Expand Down Expand Up @@ -188,6 +189,13 @@ You can set `electronPackagerConfig` with any of the options from
* `platform` (use the `--platform` Forge command line argument instead, so it's available to all of Forge)
* `quiet`

You can set `electronRebuildConfig` with any of the options from
[Electron Rebuild](https://github.com/electron/electron-rebuild/), except:

* `electronVersion`/`--version` (uses the exact version specified for `electron-prebuilt-compile` in your `devDependencies`)
* `arch`/`--arch` (use the `--arch` Forge command line argument instead, so it's available to all of Forge)
* `buildPath`/`--module-dir` (uses your project's `node_modules`)

**NOTE:** You can also set your `forge` config property of your package.json to point to a JS file that exports the config object:

```js
Expand Down
2 changes: 1 addition & 1 deletion src/api/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default async (providedOptions = {}) => {
const pruneEnabled = !('prune' in forgeConfig.electronPackagerConfig) || forgeConfig.electronPackagerConfig.prune;

const rebuildHookFn = async (buildPath, electronVersion, pPlatform, pArch, done) => {
await rebuildHook(buildPath, electronVersion, pPlatform, pArch);
await rebuildHook(buildPath, electronVersion, pPlatform, pArch, forgeConfig.electronRebuildConfig);
packagerSpinner = ora('Packaging Application').start();
done();
};
Expand Down
5 changes: 3 additions & 2 deletions src/api/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export default async (providedOptions = {}) => {
throw `Please set your application's 'version' in '${dir}/package.json'.`;
}

await rebuild(dir, packageJSON.devDependencies['electron-prebuilt-compile'], process.platform, process.arch);
const forgeConfig = await getForgeConfig(dir);

await rebuild(dir, packageJSON.devDependencies['electron-prebuilt-compile'], process.platform, process.arch, forgeConfig.electronRebuildConfig);

const spawnOpts = {
cwd: dir,
Expand All @@ -73,7 +75,6 @@ export default async (providedOptions = {}) => {

let spawned;

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

await asyncOra('Launching Application', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/util/forge-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default async (dir) => {
make_targets: {},
publish_targets: {},
electronPackagerConfig: {},
electronRebuildConfig: {},
electronWinstallerConfig: {},
electronInstallerDebian: {},
electronInstallerDMG: {},
Expand Down
8 changes: 6 additions & 2 deletions src/util/rebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import rebuild from 'electron-rebuild';

import asyncOra from '../util/ora-handler';

export default async (buildPath, electronVersion, platform, arch) => {
export default async (buildPath, electronVersion, platform, arch, config = {}) => {
await asyncOra('Preparing native dependencies', async (rebuildSpinner) => {
const rebuilder = rebuild({ buildPath, electronVersion, arch });
const rebuilder = rebuild(Object.assign({}, config, {
buildPath,
electronVersion,
arch,
}));
const { lifecycle } = rebuilder;

let found = 0;
Expand Down
1 change: 1 addition & 0 deletions test/fast/forge-config_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const defaults = {
},
electronInstallerDMG: {},
electronPackagerConfig: {},
electronRebuildConfig: {},
electronWinstallerConfig: {},
electronInstallerDebian: {},
electronInstallerRedhat: {},
Expand Down
70 changes: 47 additions & 23 deletions test/slow/rebuild_spec_slow.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,67 @@ import { yarnOrNpmSpawn, hasYarn } from '../../src/util/yarn-or-npm';
describe('rebuilder', () => {
const testModulePath = path.resolve(os.tmpdir(), 'electron-forge-rebuild-test');

before(async () => {
async function setupProject() {
await fs.remove(testModulePath);
await fs.mkdirs(testModulePath);
await fs.writeFile(path.resolve(testModulePath, 'package.json'), await fs.readFile(path.resolve(__dirname, '../fixture/native_app/package.json'), 'utf8'));
await yarnOrNpmSpawn(hasYarn() ? [] : ['install'], {
cwd: testModulePath,
stdio: process.platform === 'win32' ? 'inherit' : 'pipe',
});
});
}

before(async () => {
await rebuild(testModulePath, '1.4.12', process.platform, process.arch);
});
async function doRebuild(config) {
await rebuild(testModulePath, '1.4.12', process.platform, process.arch, config);
}

it('should have rebuilt top level prod dependencies', async () => {
const forgeMeta = path.resolve(testModulePath, 'node_modules', 'ref', 'build', 'Release', '.forge-meta');
expect(await fs.pathExists(forgeMeta), 'ref build meta should exist').to.equal(true);
});
describe('no config', () => {
before(async () => {
await setupProject();
await doRebuild();
});

it('should have rebuilt children of top level prod dependencies', async () => {
const forgeMeta = path.resolve(testModulePath, 'node_modules', 'microtime', 'build', 'Release', '.forge-meta');
expect(await fs.pathExists(forgeMeta), 'microtime build meta should exist').to.equal(true);
});
it('should have rebuilt top level prod dependencies', async () => {
const forgeMeta = path.resolve(testModulePath, 'node_modules', 'ref', 'build', 'Release', '.forge-meta');
expect(await fs.pathExists(forgeMeta), 'ref build meta should exist').to.equal(true);
});

it('should have rebuilt children of scoped top level prod dependencies', async () => {
const forgeMeta = path.resolve(testModulePath, 'node_modules', '@newrelic/native-metrics', 'build', 'Release', '.forge-meta');
expect(await fs.pathExists(forgeMeta), '@newrelic/native-metrics build meta should exist').to.equal(true);
});
it('should have rebuilt children of top level prod dependencies', async () => {
const forgeMeta = path.resolve(testModulePath, 'node_modules', 'microtime', 'build', 'Release', '.forge-meta');
expect(await fs.pathExists(forgeMeta), 'microtime build meta should exist').to.equal(true);
});

it('should have rebuilt children of scoped top level prod dependencies', async () => {
const forgeMeta = path.resolve(testModulePath, 'node_modules', '@newrelic/native-metrics', 'build', 'Release', '.forge-meta');
expect(await fs.pathExists(forgeMeta), '@newrelic/native-metrics build meta should exist').to.equal(true);
});

it('should have rebuilt top level optional dependencies', async () => {
const forgeMeta = path.resolve(testModulePath, 'node_modules', 'zipfile', 'build', 'Release', '.forge-meta');
expect(await fs.pathExists(forgeMeta), 'zipfile build meta should exist').to.equal(true);
});

it('should have rebuilt top level optional dependencies', async () => {
const forgeMeta = path.resolve(testModulePath, 'node_modules', 'zipfile', 'build', 'Release', '.forge-meta');
expect(await fs.pathExists(forgeMeta), 'zipfile build meta should exist').to.equal(true);
it('should not have rebuilt top level devDependencies', async () => {
const forgeMeta = path.resolve(testModulePath, 'node_modules', 'ffi', 'build', 'Release', '.forge-meta');
expect(await fs.pathExists(forgeMeta), 'ffi build meta should not exist').to.equal(false);
});
});

it('should not have rebuilt top level devDependencies', async () => {
const forgeMeta = path.resolve(testModulePath, 'node_modules', 'ffi', 'build', 'Release', '.forge-meta');
expect(await fs.pathExists(forgeMeta), 'ffi build meta should not exist').to.equal(false);
describe('with config', () => {
before(async () => {
await setupProject();
await doRebuild({ onlyModules: ['ref'] });
});

it('should have rebuilt module in onlyModules', async () => {
const forgeMeta = path.resolve(testModulePath, 'node_modules', 'ref', 'build', 'Release', '.forge-meta');
expect(await fs.pathExists(forgeMeta), 'ref build meta should exist').to.equal(true);
});

it('should not have rebuilt module not in onlyModules', async () => {
const forgeMeta = path.resolve(testModulePath, 'node_modules', 'zipfile', 'build', 'Release', '.forge-meta');
expect(await fs.pathExists(forgeMeta), 'zipfile build meta should not exist').to.equal(false);
});
});

after(async () => {
Expand Down

0 comments on commit b986f26

Please sign in to comment.