Skip to content
Permalink
Browse files

feat(generic): allow config options to use string templating

Use lodash templating to allow the config object to reuse values from the existing package.json file
  • Loading branch information
MarshallOfSound authored and malept committed Dec 27, 2016
1 parent dac33f0 commit 5a568cb8ac1f9a5ba7cc5f21c256356934bb2f06
Showing with 21 additions and 1 deletion.
  1. +1 −0 package.json
  2. +20 −1 src/util/forge-config.js
@@ -68,6 +68,7 @@
"fs-promise": "^1.0.0",
"github": "^7.2.0",
"inquirer": "^2.0.0",
"lodash.template": "^4.4.0",
"log-symbols": "^1.0.2",
"node-gyp": "^3.4.0",
"ora": "^0.3.0",
@@ -1,9 +1,11 @@
import fs from 'fs-promise';
import path from 'path';
import _template from 'lodash.template';
import readPackageJSON from './read-package-json';

export default async (dir) => {
let forgeConfig = (await readPackageJSON(dir)).config.forge;
const packageJSON = await readPackageJSON(dir);
let forgeConfig = packageJSON.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));
@@ -28,5 +30,22 @@ export default async (dir) => {
mas: ['zip'],
linux: ['deb', 'rpm'],
}, forgeConfig.make_targets);

const templateObj = Object.assign({}, packageJSON, { year: (new Date()).getFullYear() });
const template = (obj) => {
Object.keys(obj).forEach((objKey) => {
if (typeof obj[objKey] === 'object' && obj !== null) {
template(obj[objKey]);
} else if (typeof obj[objKey] === 'string') {
obj[objKey] = _template(obj[objKey])(templateObj); // eslint-disable-line
if (obj[objKey].startsWith('require:')) {
obj[objKey] = require(path.resolve(dir, obj[objKey].substr(8))); // eslint-disable-line
}
}
});
};

template(forgeConfig);

return forgeConfig;
};

0 comments on commit 5a568cb

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