Skip to content

Commit

Permalink
feat(generic): allow JS files to provide the config object
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #21
  • Loading branch information
MarshallOfSound committed Dec 17, 2016
1 parent bc21528 commit e57f3c7
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/util/forge-config.js
Expand Up @@ -3,6 +3,16 @@ import path from 'path';

export default async (dir) => {
let forgeConfig = JSON.parse(await fs.readFile(path.resolve(dir, 'package.json'))).config.forge;
if (typeof forgeConfig === 'string' && (await fs.exists(path.resolve(dir, forgeConfig)) || await fs.exists(path.resolve(dir, `${forgeConfig}.js`)))) {
try {
forgeConfig = require(path.resolve(dir, forgeConfig));
} catch (err) {
console.error(`Failed to load: ${path.resolve(dir, forgeConfig)}`);
throw err;
}
} else if (typeof forgeConfig !== 'object') {
throw new Error('Expected packageJSON.config.forge to be an object or point to a requirable JS file');
}
forgeConfig = Object.assign({
make_targets: {},
electronPackagerConfig: {},
Expand Down
File renamed without changes.
Expand Up @@ -18,9 +18,9 @@
"linux": ["deb", "rpm"]
},
"electronPackagerConfig": {},
"electronWinstallerConfig": {},
"electronInstallerRedhat": {},
"electronInstallerDebian": {},
"electronInstallerRedhat": {}
"electronWinstallerConfig": { "windows": "magic" }
}
},
"devDependencies": {
Expand Down
13 changes: 13 additions & 0 deletions test/fixture/dummy_js_conf/forge.config.js
@@ -0,0 +1,13 @@
module.exports = {
make_targets: {
win32: ['squirrel'],
darwin: ['zip'],
linux: ['deb', 'rpm'],
mas: ['zip'],
},
electronPackagerConfig: { foo: 'bar' },
electronWinstallerConfig: {},
electronInstallerDebian: {},
electronInstallerRedhat: {},
magicFn: () => 'magic result',
};
19 changes: 19 additions & 0 deletions test/fixture/dummy_js_conf/package.json
@@ -0,0 +1,19 @@
{
"name": "",
"productName": "",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"start": "electron-forge start"
},
"keywords": [],
"author": "",
"license": "MIT",
"config": {
"forge": "./forge.config.js"
},
"devDependencies": {
"electron-prebuilt-compile": "9.9.9"
}
}
39 changes: 37 additions & 2 deletions test/util_spec.js
Expand Up @@ -2,14 +2,49 @@ import { expect } from 'chai';
import path from 'path';

import resolveDir from '../src/util/resolve-dir';
import findConfig from '../src/util/forge-config';

describe('resolve-dir', () => {
it('should return null if a valid dir can not be found', async () => {
expect(await resolveDir('/foo/var/fake')).to.be.equal(null);
});

it('should return a directory if it finds a node module', async () => {
expect(await resolveDir(path.resolve(__dirname, 'fixture/foo'))).to.not.be.equal(null);
expect(await resolveDir(path.resolve(__dirname, 'fixture/foo'))).to.be.equal(path.resolve(__dirname, 'fixture'));
expect(await resolveDir(path.resolve(__dirname, 'fixture/dummy_app/foo'))).to.not.be.equal(null);
expect(await resolveDir(path.resolve(__dirname, 'fixture/dummy_app/foo'))).to.be.equal(path.resolve(__dirname, 'fixture/dummy_app'));
});
});

const defaults = {
make_targets: {
win32: ['squirrel'],
darwin: ['zip'],
linux: ['deb', 'rpm'],
mas: ['zip'],
},
electronInstallerDMG: {},
electronPackagerConfig: {},
electronWinstallerConfig: {},
electronInstallerDebian: {},
electronInstallerRedhat: {},
};

describe('forge-config', () => {
it('should resolve the object in package.json with defaults if one exists', async () => {
expect(await findConfig(path.resolve(__dirname, 'fixture/dummy_app'))).to.be.deep.equal(Object.assign({}, defaults, {
electronWinstallerConfig: { windows: 'magic' },
}));
});

it('should resolve the JS file exports in config.forge points to a JS file', async () => {
expect(JSON.parse(JSON.stringify(await findConfig(path.resolve(__dirname, 'fixture/dummy_js_conf'))))).to.be.deep.equal(Object.assign({}, defaults, {
electronPackagerConfig: { foo: 'bar' },
}));
});

it('should resolve the JS file exports in config.forge points to a JS file and maintain functions', async () => {
const conf = await findConfig(path.resolve(__dirname, 'fixture/dummy_js_conf'));
expect(conf.magicFn).to.be.a('function');
expect(conf.magicFn()).to.be.equal('magic result');
});
});

0 comments on commit e57f3c7

Please sign in to comment.