Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration to extend other config files #775

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .editorconfig
@@ -0,0 +1,7 @@
root = true

[*.js]
end_of_line = lf
indent_style = space
indent_size = 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great, thanks for adding it! Could you perhaps add insert_final_newline = true?

insert_final_newline = true
19 changes: 19 additions & 0 deletions lib/apply-extends.js
@@ -0,0 +1,19 @@
var fs = require('fs')
var path = require('path')
var assign = require('./assign')

function applyExtends (config, cwd) {
var defaultConfig = {}

if (config.hasOwnProperty('extends')) {
var pathToDefault = path.join(cwd, config.extends)
delete config.extends

defaultConfig = JSON.parse(fs.readFileSync(pathToDefault, 'utf8'))
defaultConfig = applyExtends(defaultConfig, path.dirname(pathToDefault))
}

return assign(defaultConfig, config)
}

module.exports = applyExtends
5 changes: 5 additions & 0 deletions test/fixtures/extends/config_1.json
@@ -0,0 +1,5 @@
{
"a": 30,
"b": 22,
"extends": "./config_2.json"
}
3 changes: 3 additions & 0 deletions test/fixtures/extends/config_2.json
@@ -0,0 +1,3 @@
{
"z": 15
}
15 changes: 15 additions & 0 deletions test/yargs.js
Expand Up @@ -1146,6 +1146,21 @@ describe('yargs dsl tests', function () {
argv.foo.should.equal(1)
argv.bar.should.equal(2)
})

describe('extends', function () {
it('applies default configurations when given config object', function () {
var argv = yargs
.config({
extends: './test/fixtures/extends/config_1.json',
a: 1
})
.argv

argv.a.should.equal(1)
argv.b.should.equal(22)
argv.z.should.equal(15)
})
})
})

describe('normalize', function () {
Expand Down
3 changes: 3 additions & 0 deletions yargs.js
Expand Up @@ -8,6 +8,7 @@ const Validation = require('./lib/validation')
const Y18n = require('y18n')
const objFilter = require('./lib/obj-filter')
const setBlocking = require('set-blocking')
const applyExtends = require('./lib/apply-extends')

var exports = module.exports = Yargs
function Yargs (processArgs, cwd, parentRequire) {
Expand Down Expand Up @@ -216,6 +217,7 @@ function Yargs (processArgs, cwd, parentRequire) {
self.config = function (key, msg, parseFn) {
// allow to pass a configuration object
if (typeof key === 'object') {
key = applyExtends(key, cwd)
options.configObjects = (options.configObjects || []).concat(key)
return self
}
Expand All @@ -231,6 +233,7 @@ function Yargs (processArgs, cwd, parentRequire) {
;(Array.isArray(key) ? key : [key]).forEach(function (k) {
options.config[k] = parseFn || true
})

return self
}

Expand Down