Skip to content

Commit

Permalink
resolve relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Mar 16, 2024
1 parent 9518999 commit 951e9a7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/configfiles/override-config-path-relative.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"myappfile": "../override-the-config-path.js"
}
19 changes: 17 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'] }
],
},
});
Expand Down

0 comments on commit 951e9a7

Please sign in to comment.