From e1db2b4e40e6d0ed55d0146d0205a2d0b62e83d6 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Tue, 12 Mar 2024 20:35:22 -0700 Subject: [PATCH 1/2] WIP --- index.js | 18 +-- test/index.js | 325 ++++++++++++++++++++++++-------------------------- 2 files changed, 165 insertions(+), 178 deletions(-) diff --git a/index.js b/index.js index 92c437b..64a17c7 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -124,10 +123,10 @@ 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 configFiles = []; + if (Array.isArray(this.configFiles)) { + configFiles = this.configFiles.map(function (searchPaths) { + var defaultObj = { cwd: cwd, extensions: exts }; var foundPath = arrayFind(searchPaths, function (pathObj) { return findAndRegisterLoader(pathObj, defaultObj); @@ -137,7 +136,7 @@ Liftoff.prototype.buildEnvironment = function (opts) { }); } - var config = mapValues(configFiles, function (startingLocation) { + var config = configFiles.map(function (startingLocation) { var defaultConfig = {}; if (!startingLocation) { return defaultConfig; @@ -164,7 +163,12 @@ Liftoff.prototype.buildEnvironment = function (opts) { var configPath = findConfig({ configNameSearch: configNameSearch, searchPaths: searchPaths, - configPath: opts.configPath, + // If the configPath was not specified, look for the configName inside each `config` object to see if it was overridden + configPath: opts.configPath || arrayFind(config, function (cfg) { + if (cfg.hasOwnProperty(this.configName)) { + return cfg[this.configName]; + } + }), }); // if we have a config path, save the directory it resides in. diff --git a/test/index.js b/test/index.js index aa6264e..8e6dba9 100644 --- a/test/index.js +++ b/test/index.js @@ -502,48 +502,41 @@ describe('Liftoff', function () { name: 'myapp', }); app.prepare({}, function (env) { - expect(env.configFiles).toEqual({}); + expect(env.configFiles).toEqual([]); done(); }); }); - it('excludes files if value is not an array', function (done) { - var app = new Liftoff({ - name: 'myapp', - configFiles: { - foo: 'bar', - }, - }); - app.prepare({}, function (env) { - expect(env.configFiles).toEqual({}); - done(); - }); - }); + // it('excludes files if value is not an object', function (done) { + // var app = new Liftoff({ + // name: 'myapp', + // configFiles: [ + // "foobar" + // ], + // }); + // app.prepare({}, function (env) { + // expect(env.configFiles).toEqual({}); + // done(); + // }); + // }); it('should find multiple files if specified', function (done) { var app = new Liftoff({ name: 'myapp', - configFiles: { - testconfig: [ - '.', - { path: 'test/fixtures/configfiles' }, - { path: 'test', cwd: 'text/fixtures/configfiles', findUp: true }, - ], - package: [ - '.', - { path: 'test/fixtures/configfiles' }, - { path: 'test', cwd: 'text/fixtures/configfiles', findUp: true }, - ], - }, + configFiles: [ + '.', + { path: 'test/fixtures/configfiles' }, + { path: 'test', cwd: 'text/fixtures/configfiles', findUp: true }, + ], }); app.prepare({}, function (env) { - expect(env.configFiles).toEqual({ - testconfig: path.resolve( + expect(env.configFiles).toEqual([ + path.resolve( __dirname, './fixtures/configfiles/testconfig.json' ), - package: path.resolve(__dirname, '../package.json'), - }); + path.resolve(__dirname, '../package.json') + ]); done(); }); }); @@ -551,48 +544,47 @@ describe('Liftoff', function () { it('should use default cwd if not specified', function (done) { var app = new Liftoff({ name: 'myapp', - configFiles: { - testconfig: [{ path: '.', extensions: ['.js', '.json'] }], - }, + configFiles: [ + { path: '.', extensions: ['.js', '.json'] } + ], }); app.prepare( { cwd: 'test/fixtures/configfiles', }, function (env) { - expect(env.configFiles).toEqual({ - testconfig: path.resolve( + expect(env.configFiles).toEqual([ + path.resolve( __dirname, './fixtures/configfiles/testconfig.json' ), - }); + ]); done(); } ); }); - it('should use dirname of configPath if no cwd is specified', function (done) { - var app = new Liftoff({ - name: 'myapp', - configFiles: { - testconfig: [{ path: '.', extensions: ['.js', '.json'] }], - }, - }); - app.prepare( - { - configPath: 'test/fixtures/configfiles/myapp.js', - }, - function (env) { - expect(env.configFiles).toEqual({ - testconfig: path.resolve( - __dirname, - './fixtures/configfiles/testconfig.json' - ), - }); - done(); - } - ); - }); + // it('should use dirname of configPath if no cwd is specified', function (done) { + // var app = new Liftoff({ + // name: 'myapp', + // configFiles: [{ path: '.', extensions: ['.js', '.json'] }], + // }, + // }); + // app.prepare( + // { + // configPath: 'test/fixtures/configfiles/myapp.js', + // }, + // function (env) { + // expect(env.configFiles).toEqual({ + // testconfig: path.resolve( + // __dirname, + // './fixtures/configfiles/testconfig.json' + // ), + // }); + // done(); + // } + // ); + // }); it('uses default extensions if not specified (.md)', function (done) { var app = new Liftoff({ @@ -600,14 +592,14 @@ describe('Liftoff', function () { '.md': './test/fixtures/configfiles/require-md', }, name: 'myapp', - configFiles: { - README: [{ path: '.' }], - }, + configFiles: [ + { name: "README", path: '.' } + ], }); app.prepare({}, function (env) { - expect(env.configFiles).toEqual({ - README: path.resolve(__dirname, '../README.md'), - }); + expect(env.configFiles).toEqual([ + path.resolve(__dirname, '../README.md'), + ]); done(); }); }); @@ -618,14 +610,14 @@ describe('Liftoff', function () { '.txt': './test/fixtures/configfiles/require-txt', }, name: 'myapp', - configFiles: { - README: [{ path: 'test/fixtures/configfiles' }], - }, + configFiles: [ + { name: 'README', path: 'test/fixtures/configfiles' } + ], }); app.prepare({}, function (env) { - expect(env.configFiles).toEqual({ - README: path.resolve(__dirname, './fixtures/configfiles/README.txt'), - }); + expect(env.configFiles).toEqual([ + path.resolve(__dirname, './fixtures/configfiles/README.txt'), + ]); done(); }); }); @@ -634,14 +626,12 @@ describe('Liftoff', function () { var app = new Liftoff({ extensions: { '.md': null, '.txt': null }, name: 'myapp', - configFiles: { - README: [{ path: '.', extensions: ['.js'] }], - }, + configFiles: [ + { name: "README", path: '.', extensions: ['.js'] } + ], }); app.prepare({}, function (env) { - expect(env.configFiles).toEqual({ - README: undefined, - }); + expect(env.configFiles).toEqual(undefined); done(); }); }); @@ -650,16 +640,12 @@ describe('Liftoff', function () { var app = new Liftoff({ extensions: { '.md': null, '.txt': null }, name: 'myapp', - configFiles: { - README: [ - { path: 'test/fixtures/configfiles', extensions: ['.json'] }, - ], - }, + configFiles: [ + { name: "README", path: 'test/fixtures/configfiles', extensions: ['.json'] }, + ], }); app.prepare({}, function (env) { - expect(env.configFiles).toEqual({ - README: undefined, - }); + expect(env.configFiles).toEqual(undefined); done(); }); }); @@ -672,17 +658,17 @@ describe('Liftoff', function () { '.md': './test/fixtures/configfiles/require-md', }, name: 'myapp', - configFiles: { - README: [{ path: '.' }], - }, + configFiles: [ + { name: 'README', path: '.' } + ], }); app.on('loader:success', function (moduleName, module) { logRequire.push({ moduleName: moduleName, module: module }); }); app.prepare({}, function (env) { - expect(env.configFiles).toEqual({ - README: path.resolve(__dirname, '../README.md'), - }); + expect(env.configFiles).toEqual([ + path.resolve(__dirname, '../README.md'), + ]); expect(logRequire.length).toEqual(1); expect(logRequire[0].moduleName).toEqual( @@ -701,22 +687,21 @@ describe('Liftoff', function () { var app = new Liftoff({ name: 'myapp', - configFiles: { - README: [ - { - path: 'test/fixtures/configfiles', - extensions: { '.txt': './test/fixtures/configfiles/require-txt' }, - }, - ], - }, + configFiles: [ + { + name: 'README', + path: 'test/fixtures/configfiles', + extensions: { '.txt': './test/fixtures/configfiles/require-txt' }, + }, + ], }); app.on('loader:success', function (moduleName, module) { logRequire.push({ moduleName: moduleName, module: module }); }); app.prepare({}, function (env) { - expect(env.configFiles).toEqual({ - README: path.resolve(__dirname, './fixtures/configfiles/README.txt'), - }); + expect(env.configFiles).toEqual([ + path.resolve(__dirname, './fixtures/configfiles/README.txt'), + ]); expect(logRequire.length).toEqual(1); expect(logRequire[0].moduleName).toEqual( @@ -735,24 +720,23 @@ describe('Liftoff', function () { var app = new Liftoff({ name: 'myapp', - configFiles: { - README: [ - { - path: 'test/fixtures/configfiles', - extensions: { - '.txt': './test/fixtures/configfiles/require-non-exist', - }, + configFiles: [ + { + name: 'README', + path: 'test/fixtures/configfiles', + extensions: { + '.txt': './test/fixtures/configfiles/require-non-exist', }, - ], - }, + }, + ], }); app.on('loader:failure', function (moduleName, error) { logFailure.push({ moduleName: moduleName, error: error }); }); app.prepare({}, function (env) { - expect(env.configFiles).toEqual({ - README: path.resolve(__dirname, './fixtures/configfiles/README.txt'), - }); + expect(env.configFiles).toEqual([ + path.resolve(__dirname, './fixtures/configfiles/README.txt'), + ]); expect(logFailure.length).toEqual(1); expect(logFailure[0].moduleName).toEqual( @@ -769,18 +753,17 @@ describe('Liftoff', function () { var app = new Liftoff({ name: 'myapp', - configFiles: { - testconfig: [ - { - path: 'test/fixtures/configfiles', - extensions: { - // ignored - '.js': './test/fixtures/configfiles/require-js', - '.json': './test/fixtures/configfiles/require-json', - }, + configFiles: [ + { + name: 'testconfig', + path: 'test/fixtures/configfiles', + extensions: { + // ignored + '.js': './test/fixtures/configfiles/require-js', + '.json': './test/fixtures/configfiles/require-json', }, - ], - }, + }, + ], }); app.on('loader:failure', function (moduleName, error) { logFailure.push({ moduleName: moduleName, error: error }); @@ -789,12 +772,12 @@ describe('Liftoff', function () { logRequire.push({ moduleName: moduleName, module: module }); }); app.prepare({}, function (env) { - expect(env.configFiles).toEqual({ - testconfig: path.resolve( + expect(env.configFiles).toEqual([ + path.resolve( __dirname, './fixtures/configfiles/testconfig.json' ), - }); + ]); expect(logRequire.length).toEqual(0); expect(logFailure.length).toEqual(0); @@ -819,16 +802,16 @@ describe('Liftoff', function () { it('loads config if a `configFiles` is found and makes it available with the same key on `config`', function (done) { var app = new Liftoff({ name: 'myapp', - configFiles: { - testconfig: ['test/fixtures/configfiles'], - }, + configFiles: [ + { name: 'testconfig', path: 'test/fixtures/configfiles' }, + ], }); app.prepare({}, function (env) { - expect(env.config).toEqual({ - testconfig: { + expect(env.config).toEqual([ + { aaa: 'AAA', }, - }); + ]); done(); }); }); @@ -836,17 +819,17 @@ describe('Liftoff', function () { it('loads and merges a config file specified if loaded file provides `extends` property', function (done) { var app = new Liftoff({ name: 'myapp', - configFiles: { - testconfig: ['test/fixtures/configfiles-extends'], - }, + configFiles: [ + { name: 'testconfig', path: 'test/fixtures/configfiles-extends' }, + ] }); app.prepare({}, function (env) { - expect(env.config).toEqual({ - testconfig: { + expect(env.config).toEqual([ + { aaa: 'AAA', // Comes from the base, which overrode `aaa: 'CCC'` in the `extends` bbb: 'BBB', // Comes from the `extends` }, - }); + ]); done(); }); }); @@ -854,9 +837,9 @@ describe('Liftoff', function () { it('throws error on circular extends', function (done) { var app = new Liftoff({ name: 'myapp', - configFiles: { - circular1: ['test/fixtures/configfiles-extends'], - }, + configFiles: [ + { name: 'circular1', path: 'test/fixtures/configfiles-extends' }, + ], }); var circPath = path.resolve( __dirname, @@ -871,9 +854,9 @@ describe('Liftoff', function () { it('gracefully handles a null-ish extends', function (done) { var app = new Liftoff({ name: 'myapp', - configFiles: { - null: ['test/fixtures/configfiles-extends'], - }, + configFiles: [ + { name: null, path: 'test/fixtures/configfiles-extends' }, + ], }); app.prepare({}, function (env) { expect(env.config).toEqual({ @@ -886,17 +869,17 @@ describe('Liftoff', function () { it('stops processing extends on an empty (or null-ish) extends', function (done) { var app = new Liftoff({ name: 'myapp', - configFiles: { - 'load-empty': ['test/fixtures/configfiles-extends'], - }, + configFiles: [ + { name: 'load-empty', path: 'test/fixtures/configfiles-extends' }, + ], }); app.prepare({}, function (env) { - expect(env.config).toEqual({ - 'load-empty': { + expect(env.config).toEqual([ + { aaa: 'bbb', ccc: 'ddd', }, - }); + ]); done(); }); }); @@ -904,9 +887,9 @@ describe('Liftoff', function () { it('throws upon extends of missing local file', function (done) { var app = new Liftoff({ name: 'myapp', - configFiles: { - 'local-missing': ['test/fixtures/configfiles-extends'], - }, + configFiles: [ + { name: 'local-missing', path: 'test/fixtures/configfiles-extends' }, + ], }); var missingPath = path.resolve( __dirname, @@ -921,9 +904,9 @@ describe('Liftoff', function () { it('throws (with correct path) upon extends of missing deep in tree', function (done) { var app = new Liftoff({ name: 'myapp', - configFiles: { - 'extend-missing': ['test/fixtures/configfiles-extends'], - }, + configFiles: [ + { name: 'extend-missing', path: 'test/fixtures/configfiles-extends' }, + ], }); var missingPath = path.resolve( __dirname, @@ -938,9 +921,9 @@ describe('Liftoff', function () { it('throws (with correct path) upon extends using `fined` object with `path`', function (done) { var app = new Liftoff({ name: 'myapp', - configFiles: { - 'missing-path-obj': ['test/fixtures/configfiles-extends'], - }, + configFiles: [ + { name: 'missing-path-obj', path: 'test/fixtures/configfiles-extends' }, + ], }); var missingPath = path.resolve( __dirname, @@ -955,9 +938,9 @@ describe('Liftoff', function () { it('throws (with correct path) upon extends using `fined` object with `name`', function (done) { var app = new Liftoff({ name: 'myapp', - configFiles: { - 'missing-name-obj': ['test/fixtures/configfiles-extends'], - }, + configFiles: [ + { name: 'missing-name-obj', path: 'test/fixtures/configfiles-extends' }, + ], }); var missingPath = path.resolve( __dirname, @@ -972,9 +955,9 @@ describe('Liftoff', function () { it('throws (without path) upon extends using invalid `fined` object', function (done) { var app = new Liftoff({ name: 'myapp', - configFiles: { - 'missing-invalid-obj': ['test/fixtures/configfiles-extends'], - }, + configFiles: [ + { name: 'missing-invalid-obj', path: 'test/fixtures/configfiles-extends' }, + ] }); expect(function () { app.prepare({}, function () {}); @@ -985,9 +968,9 @@ describe('Liftoff', function () { it('throws upon extends of missing npm module', function (done) { var app = new Liftoff({ name: 'myapp', - configFiles: { - 'npm-missing': ['test/fixtures/configfiles-extends'], - }, + configFiles: [ + { name: 'npm-missing', path: 'test/fixtures/configfiles-extends' }, + ], }); var missingModule = 'not-installed'; expect(function () { @@ -999,9 +982,9 @@ describe('Liftoff', function () { it('throws upon extends if loading file errors', function (done) { var app = new Liftoff({ name: 'myapp', - configFiles: { - throws: ['test/fixtures/configfiles-extends'], - }, + configFiles: [ + { name: 'throws', path: 'test/fixtures/configfiles-extends' }, + ], }); var errModulePath = path.resolve( __dirname, From f8cfd1ebfc513a233c1ead933dd82f7def59367a Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Tue, 12 Mar 2024 20:37:20 -0700 Subject: [PATCH 2/2] linting --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 64a17c7..810bd7f 100644 --- a/index.js +++ b/index.js @@ -165,7 +165,7 @@ Liftoff.prototype.buildEnvironment = function (opts) { searchPaths: searchPaths, // If the configPath was not specified, look for the configName inside each `config` object to see if it was overridden configPath: opts.configPath || arrayFind(config, function (cfg) { - if (cfg.hasOwnProperty(this.configName)) { + if (Object.prototype.hasOwnProperty.call(cfg, this.configName)) { return cfg[this.configName]; } }),