Skip to content

Commit

Permalink
feat!: Define configFiles with an array to prioritize configs (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated authored Mar 16, 2024
1 parent fad21a9 commit 55123fc
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 279 deletions.
89 changes: 35 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ Default: `null`

#### opts.configFiles

An object of configuration files to find. Each property is keyed by the default basename of the file being found, and the value is an array of [path arguments](#path-arguments) of which the order indicates priority to find.
An array of configuration files to find with each value being a [path arguments](#path-arguments).

See [Config Files](#config-files) for the config file specification.
The order of the array indicates the priority that config file overrides are applied. See [Config Files](#config-files) for the config file specification and description of overrides.

**Note:** This option is useful if, for example, you want to support an `.apprc` file in addition to an `appfile.js`. If you only need a single configuration file, you probably don't need this. In addition to letting you find multiple files, this option allows more fine-grained control over how configuration files are located.

Type: `Object`
Type: `Array`

Default: `null`

Expand Down Expand Up @@ -223,9 +223,9 @@ In this example Liftoff will look for the `.hacker.js` file relative to the `cwd
```js
const MyApp = new Liftoff({
name: 'hacker',
configFiles: {
'.hacker': [{ path: '.' }],
},
configFiles: [
{ name: '.hacker', path: '.' }
],
});
```

Expand All @@ -234,16 +234,15 @@ In this example, Liftoff will look for `.hackerrc` in the home directory.
```js
const MyApp = new Liftoff({
name: 'hacker',
configFiles: {
'.hacker': [
{
path: '~',
extensions: {
rc: null,
},
configFiles: [
{
name: '.hacker',
path: '~',
extensions: {
rc: null,
},
],
},
},
],
});
```

Expand All @@ -252,30 +251,13 @@ In this example, Liftoff will look in the `cwd` and then lookup the tree for the
```js
const MyApp = new Liftoff({
name: 'hacker',
configFiles: {
'.hacker': [
{
path: '.',
findUp: true,
},
],
},
});
```

In this example, the `name` is overridden and the key is ignored so Liftoff looks for `.override.js`.

```js
const MyApp = new Liftoff({
name: 'hacker',
configFiles: {
hacker: [
{
path: '.',
name: '.override',
},
],
},
configFiles: [
{
name: '.hacker',
path: '.',
findUp: true,
},
],
});
```

Expand All @@ -284,14 +266,13 @@ In this example, Liftoff will use the home directory as the `cwd` and looks for
```js
const MyApp = new Liftoff({
name: 'hacker',
configFiles: {
'.hacker': [
{
path: '.',
cwd: '~',
},
[,
},
configFiles: [
{
name: '.hacker',
path: '.',
cwd: '~',
},
],
});
```

Expand Down Expand Up @@ -330,9 +311,9 @@ MyApp.prepare(
const Liftoff = require('liftoff');
const Hacker = new Liftoff({
name: 'hacker',
configFiles: {
'.hacker': [{ path: '.', cwd: '~' }],
},
configFiles: [
{ name: '.hacker', path: '.', cwd: '~' }
],
});
const onExecute = function (env, argv) {
// Do post-execute things
Expand Down Expand Up @@ -454,8 +435,8 @@ A function called after your environment is prepared. A good place to modify the
- `configBase`: the base directory of your configuration file (if found)
- `modulePath`: the full path to the local module your project relies on (if found)
- `modulePackage`: the contents of the local module's package.json (if found)
- `configFiles`: an object of filepaths for each found config file (filepath values will be null if not found)
- `config`: an object with keys matching `configFiles` but with the loaded config object
- `configFiles`: an array of filepaths for each found config file (filepath values will be null if not found)
- `config`: an array of loaded config objects in the same order as `configFiles`

### execute(env, [forcedFlags], callback(env, argv))

Expand Down Expand Up @@ -490,8 +471,8 @@ A function called after your application is executed. When invoked, `this` will
- `configBase`: the base directory of your configuration file (if found)
- `modulePath`: the full path to the local module your project relies on (if found)
- `modulePackage`: the contents of the local module's package.json (if found)
- `configFiles`: an object of filepaths for each found config file (filepath values will be null if not found)
- `config`: an object with keys matching `configFiles` but with the loaded config object
- `configFiles`: an array of filepaths for each found config file (filepath values will be null if not found)
- `config`: an array of loaded config objects in the same order as `configFiles`

### events

Expand Down
25 changes: 9 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var extend = require('extend');
var resolve = require('resolve');
var flaggedRespawn = require('flagged-respawn');
var isPlainObject = require('is-plain-object').isPlainObject;
var mapValues = require('object.map');
var fined = require('fined');

var findCwd = require('./lib/find_cwd');
Expand Down Expand Up @@ -66,7 +65,7 @@ Liftoff.prototype.buildEnvironment = function (opts) {
function findAndRegisterLoader(pathObj, defaultObj) {
var found = fined(pathObj, defaultObj);
if (!found) {
return;
return null;
}
if (isPlainObject(found.extension)) {
registerLoader(eventEmitter, found.extension, found.path, cwd);
Expand Down Expand Up @@ -139,20 +138,16 @@ Liftoff.prototype.buildEnvironment = function (opts) {
return config;
}

var configFiles = {};
if (isPlainObject(this.configFiles)) {
configFiles = mapValues(this.configFiles, function (searchPaths, fileStem) {
var defaultObj = { name: fileStem, cwd: cwd, extensions: exts };

var foundPath = arrayFind(searchPaths, function (pathObj) {
return findAndRegisterLoader(pathObj, defaultObj);
});
var configFiles = [];
if (Array.isArray(this.configFiles)) {
configFiles = this.configFiles.map(function (pathObj) {
var defaultObj = { cwd: cwd, extensions: exts };

return foundPath;
return findAndRegisterLoader(pathObj, defaultObj);
});
}

var config = mapValues(configFiles, function (startingLocation) {
var config = configFiles.map(function (startingLocation) {
var defaultConfig = {};
if (!startingLocation) {
return defaultConfig;
Expand All @@ -161,17 +156,15 @@ Liftoff.prototype.buildEnvironment = function (opts) {
return loadConfig(cwd, startingLocation, defaultConfig);
});

var configPathOverride = arrayFind(Object.keys(config), function (key) {
var cfg = config[key];
var configPathOverride = arrayFind(config, function (cfg) {
if (Object.prototype.hasOwnProperty.call(cfg, configName)) {
if (isString(cfg[configName])) {
return cfg[configName];
}
}
});

var additionPreloads = arrayFind(Object.keys(config), function (key) {
var cfg = config[key];
var additionPreloads = arrayFind(config, function (cfg) {
if (Object.prototype.hasOwnProperty.call(cfg, 'preload')) {
if (Array.isArray(cfg.preload)) {
if (cfg.preload.every(isString)) {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"fined": "^2.0.0",
"flagged-respawn": "^2.0.0",
"is-plain-object": "^5.0.0",
"object.map": "^1.0.1",
"rechoir": "^0.8.0",
"resolve": "^1.20.0"
},
Expand Down
10 changes: 10 additions & 0 deletions test/array_find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var expect = require('expect');

var arrayFind = require('../lib/array_find');

describe('buildConfigName', function () {
it('returns undefined if called with non-array', function (done) {
expect(arrayFind({})).toEqual(undefined);
done();
});
});
Loading

0 comments on commit 55123fc

Please sign in to comment.