From 951e9a796dc6f158952c94ea40a0e761173ecbcb Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Fri, 15 Mar 2024 19:31:09 -0700 Subject: [PATCH] resolve relative paths --- index.js | 11 +++++++++-- ...th.js => override-config-path-absolute.js} | 0 .../override-config-path-relative.json | 3 +++ test/index.js | 19 +++++++++++++++++-- 4 files changed, 29 insertions(+), 4 deletions(-) rename test/fixtures/configfiles/{override-config-path.js => override-config-path-absolute.js} (100%) create mode 100644 test/fixtures/configfiles/override-config-path-relative.json diff --git a/index.js b/index.js index 5d74742..f9ebebc 100644 --- a/index.js +++ b/index.js @@ -50,6 +50,8 @@ Liftoff.prototype.buildEnvironment = function (opts) { // make a copy of search paths that can be mutated for this run var searchPaths = this.searchPaths.slice(); + // store the instance configName to use in closures without access to `this` + var configName = this.configName; // calculate current cwd var cwd = findCwd(opts); @@ -113,6 +115,13 @@ Liftoff.prototype.buildEnvironment = function (opts) { 'Encountered error when loading config file: ' + configFilePath ); } + + // resolve something like `{ gulpfile: "./abc.xyz" }` to the absolute path + // based on the path of the configFile + if (Object.prototype.hasOwnProperty.call(configFile, configName)) { + configFile[configName] = path.resolve(configFilePath, configFile[configName]); + } + visited[configFilePath] = true; if (configFile && configFile.extends) { var nextCwd = path.dirname(configFilePath); @@ -146,8 +155,6 @@ Liftoff.prototype.buildEnvironment = function (opts) { return loadConfig(cwd, startingLocation, defaultConfig); }); - var configName = this.configName; - var configPathOverride = arrayFind(Object.keys(config), function (key) { var cfg = config[key]; if (Object.prototype.hasOwnProperty.call(cfg, configName)) { diff --git a/test/fixtures/configfiles/override-config-path.js b/test/fixtures/configfiles/override-config-path-absolute.js similarity index 100% rename from test/fixtures/configfiles/override-config-path.js rename to test/fixtures/configfiles/override-config-path-absolute.js diff --git a/test/fixtures/configfiles/override-config-path-relative.json b/test/fixtures/configfiles/override-config-path-relative.json new file mode 100644 index 0000000..c221f9a --- /dev/null +++ b/test/fixtures/configfiles/override-config-path-relative.json @@ -0,0 +1,3 @@ +{ + "myappfile": "../override-the-config-path.js" +} diff --git a/test/index.js b/test/index.js index 4517d2f..9b26e01 100644 --- a/test/index.js +++ b/test/index.js @@ -575,8 +575,23 @@ describe('Liftoff', function () { var app = new Liftoff({ name: 'myapp', configFiles: { - 'override-config-path': [ - { path: 'test/fixtures/configfiles', extensions: ['.js', '.json'] } + 'override-config-path-absolute': [ + { path: 'test/fixtures/configfiles', extensions: ['.js'] } + ], + }, + }); + app.prepare({}, function (env) { + expect(env.configPath).toMatch(/override-the-config-path\.js$/); + done(); + }); + }); + + it('resolves relative configPath if the configName key exists in the config', function (done) { + var app = new Liftoff({ + name: 'myapp', + configFiles: { + 'override-config-path-relative': [ + { path: 'test/fixtures/configfiles', extensions: ['.json'] } ], }, });