Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Commit

Permalink
Merge 71101d2 into 2b46cf1
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-roemer committed Nov 11, 2016
2 parents 2b46cf1 + 71101d2 commit 2f0cea8
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 39 deletions.
11 changes: 11 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
History
=======

## Unreleased

* Add in `NODE_PATH` and `PATH` from _actual resolved paths_ of prod and dev
archetypes instead of guessed paths. This will better support `yarn`, which
flattens `node_modules/.bin` in different ways than real `npm`. It is also
likely more correct than before.
[#134](https://github.com/FormidableLabs/builder/issues/134)
* Add extra higher level directory check when `LOCAL_DEV=true` and
`--expand-archetype` specified.
* Make config loading failure a simple `log.info` instead of `log.warn`.

## 3.1.0

* Add `--env` environment variable flag.
Expand Down
5 changes: 4 additions & 1 deletion bin/builder-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ module.exports = function (opts, callback) {
opts = (arguments.length === 2 ? opts : {}) || {};

// Configuration
var config = new Config();
var config = new Config({
env: opts.env,
argv: opts.argv
});

// Set up environment
var env = new Environment({
Expand Down
108 changes: 72 additions & 36 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,32 @@ var yaml = require("js-yaml");
var chalk = require("chalk");
var args = require("./args");
var log = require("./log");
var expandFlag = require("./utils/archetype").expandFlag;

/**
* Configuration wrapper.
*
* @param {Object} cfg Configuration object or JSON/YAML file (Default: `.builderrc`)
* @param {Object} opts Options object
* @param {Object} opts.env Raw environment (Default `process.env`)
* @param {Array} opts.argv Raw arguments array (Default: `process.argv`)
* @returns {void}
*/
var Config = module.exports = function (cfg) {
var Config = module.exports = function (opts) {
log.info("config:environment", JSON.stringify({
cwd: process.cwd(),
dir: __dirname
}));

this.cfg = this._loadConfig(cfg);
this.archetypes = this.cfg.archetypes || [];
// Internal global state.
opts = opts || {};
this._args = args.general(opts.argv || process.argv);
this.expandArchetype = expandFlag(opts);

// Include the `-dev` packages.
this.allArchetypes = this.archetypes.reduce(function (memo, name) {
return memo.concat([name, name + "-dev"]);
}, []);
this.cfg = this._loadConfig();
this.archetypes = this.cfg.archetypes || [];

// Array of `{ name, path }`
this.devPkgs = this._loadDevPkgs(this.archetypes);
// Array of `{ name, mod, path, scripts, config }`
this.pkgs = this._loadPkgs(this.archetypes);
};
Expand All @@ -59,32 +64,25 @@ Config.prototype._lazyRequire = function (mod) {
/**
* Load configuration.
*
* @param {Object} cfg Configuration object or JSON/YAML file (Default: `.builderrc`)
* @returns {Object} Configuration object
* @returns {Object} Configuration object
*/
Config.prototype._loadConfig = function (cfg) {
cfg = cfg || ".builderrc";

Config.prototype._loadConfig = function () {
// Override from command line.
var parsed = args.general();
if (parsed.builderrc) {
cfg = parsed.builderrc;
}
var cfgObj = this._args.builderrc || ".builderrc";
var cfg = {};

// Load from builderrc.
if (typeof cfg === "string") {
if (typeof cfgObj === "string") {
try {
cfg = yaml.safeLoad(fs.readFileSync(cfg, "utf8"));
cfg = yaml.safeLoad(fs.readFileSync(cfgObj, "utf8"));
} catch (err) {
if (err.code === "ENOENT") {
log.warn("config", "Unable to load config file: " + cfg);
} else {
log.info("config", "Unable to load config file: " + cfgObj);

if (err.code !== "ENOENT") {
log.error("config", err.toString());
throw err;
}
}

cfg = cfg || {};
}

return cfg;
Expand All @@ -108,6 +106,17 @@ Config.prototype._loadArchetypePkg = function (name) {
/*eslint-disable no-empty*/
// Pass through error
}

// Allow a lower directory peek if expanding archetype.
if (this.expandArchetype) {
try {
pkg = pkg || this._lazyRequire(
path.join(process.cwd(), "../../node_modules", name, "package.json"));
} catch (err) {
/*eslint-disable no-empty*/
// Pass through error
}
}
}

try {
Expand Down Expand Up @@ -146,18 +155,45 @@ Config.prototype._loadPkgs = function (archetypes) {
.value());

// Add scripts, config.
pkgs = _.mapValues(pkgs, function (pkg) {
var mod = pkg.mod || {};
return _.chain(pkgs)
.mapValues(function (pkg) {
var mod = pkg.mod || {};

return _.extend({
config: mod.config,
scripts: pkg.name === "ROOT" ? mod.scripts || {} : self._loadArchetypeScripts(pkg)
}, pkg);
})
.toArray()
.value();
};

return _.extend({
config: mod.config,
scripts: pkg.name === "ROOT" ? mod.scripts || {} : self._loadArchetypeScripts(pkg)
}, pkg);
});
/**
* Load dev packages (if available).
*
* @param {Array} archetypes Archetype names
* @returns {Array} Array of `{ name, path }`
*/
Config.prototype._loadDevPkgs = function (archetypes) {
var self = this;

return _.chain(archetypes)
.map(function (baseName) {
var name = baseName + "-dev";

return pkgs;
try {
return _.extend({ name: name }, self._loadArchetypePkg(name));
} catch (err) {
// Pass through error
return null;
}
})
.filter(_.identity)
.reverse()
.value();
};


/**
* Archetype package scripts.
*
Expand Down Expand Up @@ -269,8 +305,8 @@ Object.defineProperties(Config.prototype, {
* @returns {Array} Archetype bin paths
*/
get: function () {
return _.map(this.allArchetypes, function (name) {
return path.join(process.cwd(), "node_modules", name, "node_modules/.bin");
return _.map([].concat(this.pkgs, this.devPkgs), function (pkg) {
return path.join(pkg.path, "node_modules/.bin");
});
}
},
Expand All @@ -282,8 +318,8 @@ Object.defineProperties(Config.prototype, {
* @returns {Array} Archetype `node_modules` paths
*/
get: function () {
return _.map(this.allArchetypes, function (name) {
return path.join(process.cwd(), "node_modules", name, "node_modules");
return _.map([].concat(this.pkgs, this.devPkgs), function (pkg) {
return path.join(pkg.path, "node_modules");
});
}
},
Expand Down
7 changes: 5 additions & 2 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var Config = require("./config");
var Environment = require("./environment");
var Task = require("./task");
var clone = require("./utils/clone");
var expandFlag = require("./utils/archetype").expandFlag;
var runner;

// Helper for command strings for logging.
Expand Down Expand Up @@ -102,7 +103,7 @@ var expandArchetype = function (cmd, opts, env) {
env = env || {};

// Short-circuit if no expansion.
var expand = opts.expandArchetype || env._BUILDER_ARGS_EXPAND_ARCHETYPE === "true";
var expand = opts.expandArchetype || expandFlag({ env: env });
if (expand !== true) {
return cmd;
}
Expand Down Expand Up @@ -288,7 +289,9 @@ var addSetup = function (setup, shOpts) {
//
// **Note**: Could refactor this out to a higher-level `create` method or
// something that could be reused by `builder-core.js`
var config = new Config();
var config = new Config({
env: shOpts.env
});
var env = new Environment({
config: config,
env: shOpts.env
Expand Down
22 changes: 22 additions & 0 deletions lib/utils/archetype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

/**
* Archetype utilities
*/
var args = require("../args");

/**
* Return if `--expand-archetype` flag is set (or in env).
*
* @param {Object} opts Options object
* @param {Object} opts.env Raw environment (Default `process.env`)
* @param {Array} opts.argv Raw arguments array (Default: `process.argv`)
* @returns {Boolean} True if archetype is expanded.
*/
module.exports.expandFlag = function (opts) {
opts = opts || {};
var env = opts.env || process.env; // Raw environment object.
var parsed = args.general(opts.argv || process.argv);

return parsed.expandArchetype || env._BUILDER_ARGS_EXPAND_ARCHETYPE === "true";
};

0 comments on commit 2f0cea8

Please sign in to comment.