Skip to content

Commit

Permalink
feat: add support for embark.config.js
Browse files Browse the repository at this point in the history
This commit introduces support for using `embark.config.js` to calculate the
embark configuration object that is otherwise provided via `embark.json`.

If an `embark.config.js` file is present, it will be used over the
`embark.json` file.  The `embark.config.js` module needs to export either an
object or a function that can be asynchronous and has to return or resolve with
an embark configuration object:

```js
// embark.config.js

module.exports = async function () {
  let config = ...; // do lazy calculation of `embarkConfig`;
  return config;
}
```
  • Loading branch information
michaelsbradleyjr committed Mar 6, 2020
1 parent b19a58b commit e0f7913
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/core/console/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export default class Console {
return this.ipc.request("console:executeCmd", cmd, callback);
}

if (cmd.indexOf("profile") === 0 && warnIfPackageNotDefinedLocally("embark-profiler", this.embark.logger.warn) !== true) {
if (cmd.indexOf("profile") === 0 && warnIfPackageNotDefinedLocally("embark-profiler", this.embark.logger.warn, this.embark.config.embarkConfig) !== true) {
return callback(null, "please install embark-profiler plugin");
}
if (!(cmd.split(" ")[0] === "history" || cmd === __("history"))) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ export class Engine {
}

async generateEmbarkJSON() {
if (fs.existsSync('embark.json')) {
throw new Error(__('embark.json already there. Will not overwrite'));
if (fs.existsSync('embark.json') || fs.existsSync('embark.config.js')) {
throw new Error(__('embark.json or embark.config.js already exist. Will not overwrite'));
}
return fs.writeFile('embark.json', JSON.stringify(constants.defaultEmbarkConfig, null, 2));
}
Expand Down
3 changes: 1 addition & 2 deletions packages/core/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export function isEs6Module(module) {
return (typeof module === 'function' && isConstructor(module)) || (typeof module === 'object' && typeof module.default === 'function' && module.__esModule);
}

export function warnIfPackageNotDefinedLocally(packageName, warnFunc) {
export function warnIfPackageNotDefinedLocally(packageName, warnFunc, embarkConfig) {
const packageIsResolvable = findUp.sync("node_modules/" + packageName, {cwd: dappPath()});
if (!packageIsResolvable) {
return warnFunc("== WARNING: " + packageName + " could not be resolved; ensure it is defined in your dapp's package.json dependencies and then run npm or yarn install; in future versions of embark this package should be a local dependency and configured as a plugin");
Expand All @@ -325,7 +325,6 @@ export function warnIfPackageNotDefinedLocally(packageName, warnFunc) {
return warnFunc("== WARNING: it seems " + packageName + " is not defined in your dapp's package.json dependencies; In future versions of embark this package should be a local dependency and configured as a plugin");
}

const embarkConfig = fs.readJSONSync(dappPath("embark.json"));
if (!embarkConfig.plugins[packageName]) {
return warnFunc(
__("== WARNING: it seems %s is not defined in your Dapp's embark.json plugins;\nIn future versions of Embark, this package should be a local dependency and configured as a plugin", packageName)
Expand Down
Loading

0 comments on commit e0f7913

Please sign in to comment.