feat(generic): allow JS files to provide the config object
ISSUES CLOSED: #21
This commit is contained in:
Родитель
bc21528d04
Коммит
e57f3c785f
|
@ -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: {},
|
||||
|
|
|
@ -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,6 +2,7 @@ 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 () => {
|
||||
|
@ -9,7 +10,41 @@ describe('resolve-dir', () => {
|
|||
});
|
||||
|
||||
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');
|
||||
});
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче