-
Notifications
You must be signed in to change notification settings - Fork 996
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow provided config object to extend other configs (#779)
BREAKING CHANGE: `extends` key in config file is now used for extending other config files
- Loading branch information
Showing
11 changed files
with
146 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
root = true | ||
|
||
[*.js] | ||
end_of_line = lf | ||
indent_style = space | ||
indent_size = 2 | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
var fs = require('fs') | ||
var path = require('path') | ||
var assign = require('./assign') | ||
var YError = require('./yerror') | ||
|
||
var previouslyVisitedConfigs = [] | ||
|
||
function checkForCircularExtends (path) { | ||
if (previouslyVisitedConfigs.indexOf(path) > -1) { | ||
throw new YError("Circular extended configurations: '" + path + "'.") | ||
} | ||
} | ||
|
||
function getPathToDefaultConfig (cwd, pathToExtend) { | ||
return path.isAbsolute(pathToExtend) ? pathToExtend : path.join(cwd, pathToExtend) | ||
} | ||
|
||
function applyExtends (config, cwd, subKey) { | ||
var defaultConfig = {} | ||
|
||
if (config.hasOwnProperty('extends')) { | ||
var pathToDefault = getPathToDefaultConfig(cwd, config.extends) | ||
|
||
checkForCircularExtends(pathToDefault) | ||
|
||
previouslyVisitedConfigs.push(pathToDefault) | ||
delete config.extends | ||
|
||
defaultConfig = JSON.parse(fs.readFileSync(pathToDefault, 'utf8')) | ||
if (subKey) { | ||
defaultConfig = defaultConfig[subKey] || {} | ||
} | ||
defaultConfig = applyExtends(defaultConfig, path.dirname(pathToDefault), subKey) | ||
} | ||
|
||
previouslyVisitedConfigs = [] | ||
|
||
return assign(defaultConfig, config) | ||
} | ||
|
||
module.exports = applyExtends |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"a": 44, | ||
"extends": "./circular_2.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"b": "any", | ||
"extends": "./circular_1.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"a": 30, | ||
"b": 22, | ||
"extends": "./config_2.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"z": 15 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"foo": { | ||
"a": 80, | ||
"extends": "../packageB/package.json" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"foo": { | ||
"a": 90, | ||
"b": "riffiwobbles" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters