From 04b598dc9c2671c1b618d89c9e432344bcf33404 Mon Sep 17 00:00:00 2001 From: rcoy-v Date: Fri, 3 Feb 2017 18:45:17 +0000 Subject: [PATCH 1/2] Initial configuration extends work --- .editorconfig | 6 ++++++ lib/apply-extends.js | 19 +++++++++++++++++++ test/fixtures/extends/config_1.json | 5 +++++ test/fixtures/extends/config_2.json | 3 +++ test/yargs.js | 15 +++++++++++++++ yargs.js | 3 +++ 6 files changed, 51 insertions(+) create mode 100644 .editorconfig create mode 100644 lib/apply-extends.js create mode 100644 test/fixtures/extends/config_1.json create mode 100644 test/fixtures/extends/config_2.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..24d59c770 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,6 @@ +root = true + +[*.js] +end_of_line = lf +indent_style = space +indent_size = 2 diff --git a/lib/apply-extends.js b/lib/apply-extends.js new file mode 100644 index 000000000..d1014e5cf --- /dev/null +++ b/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 diff --git a/test/fixtures/extends/config_1.json b/test/fixtures/extends/config_1.json new file mode 100644 index 000000000..043fb87b0 --- /dev/null +++ b/test/fixtures/extends/config_1.json @@ -0,0 +1,5 @@ +{ + "a": 30, + "b": 22, + "extends": "./config_2.json" +} \ No newline at end of file diff --git a/test/fixtures/extends/config_2.json b/test/fixtures/extends/config_2.json new file mode 100644 index 000000000..886f419f4 --- /dev/null +++ b/test/fixtures/extends/config_2.json @@ -0,0 +1,3 @@ +{ + "z": 15 +} \ No newline at end of file diff --git a/test/yargs.js b/test/yargs.js index b8777e748..0e6cc0761 100644 --- a/test/yargs.js +++ b/test/yargs.js @@ -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 () { diff --git a/yargs.js b/yargs.js index 53a877435..ffc465d8d 100644 --- a/yargs.js +++ b/yargs.js @@ -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) { @@ -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 } @@ -231,6 +233,7 @@ function Yargs (processArgs, cwd, parentRequire) { ;(Array.isArray(key) ? key : [key]).forEach(function (k) { options.config[k] = parseFn || true }) + return self } From 6c6e88069e19564cc959c2e0cd122692201356b7 Mon Sep 17 00:00:00 2001 From: rcoy-v Date: Thu, 9 Feb 2017 21:59:02 +0000 Subject: [PATCH 2/2] Add insert final newline to editor config --- .editorconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/.editorconfig b/.editorconfig index 24d59c770..91c336188 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,3 +4,4 @@ root = true end_of_line = lf indent_style = space indent_size = 2 +insert_final_newline = true