Skip to content
Permalink
Browse files

feat(generic): allow JS files to provide the config object

ISSUES CLOSED: #21
  • Loading branch information
MarshallOfSound committed Dec 16, 2016
1 parent bc21528 commit e57f3c785fe25db1251dac0645a6772480d1ea0b
@@ -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: {},
File renamed without changes.
@@ -18,9 +18,9 @@
"linux": ["deb", "rpm"]
},
"electronPackagerConfig": {},
"electronWinstallerConfig": {},
"electronInstallerRedhat": {},
"electronInstallerDebian": {},
"electronInstallerRedhat": {}
"electronWinstallerConfig": { "windows": "magic" }
}
},
"devDependencies": {
@@ -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',
};
@@ -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"
}
}
@@ -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.
You can’t perform that action at this time.