From db2d39de99319b652e26c9ecaa47d7d0e96f8088 Mon Sep 17 00:00:00 2001 From: dmbch Date: Thu, 18 Jan 2018 23:00:07 +0100 Subject: [PATCH] feat(config): use cosmiconfig for inheritance The `extends` config value can now reference JS, JSON or YAML files with or without file extension. --- packages/config/index.js | 41 ++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/config/index.js b/packages/config/index.js index 297313c9a..e33ea143e 100644 --- a/packages/config/index.js +++ b/packages/config/index.js @@ -1,15 +1,12 @@ 'use strict'; +var fs = require('fs'); var path = require('path'); var assign = require('deep-assign'); var root = require('pkg-dir').sync(); -var explorer = require('cosmiconfig')('hops', { - rcExtensions: true, - stopDir: root, - sync: true, -}); +var cosmiconfig = require('cosmiconfig'); function getDefaultConfig() { return { @@ -30,26 +27,38 @@ function getDefaultConfig() { } function applyUserConfig(config) { + var explorer = cosmiconfig('hops', { + rcExtensions: true, + stopDir: root, + sync: true, + }); var result = explorer.load(process.cwd()); return assign({}, config, result ? result.config : {}); } function applyInheritedConfig(config) { - var result = Object.assign({}, config); - if (config.extends) { - delete result.extends; + var result = assign({}, config); + while (result.extends) { + var configName = result.extends; + var configPath; try { - require.resolve(config.extends); - assign(result, require(config.extends)); + configPath = require.resolve(configName); } catch (_) { - try { - assign(result, require(path.join(root, config.extends))); - } catch (_) { - console.error('Failed to extend config using', config.extends); - } + configPath = path.join(root, configName); + } + delete result.extends; + if (fs.existsSync(configPath)) { + var loader = cosmiconfig('hops', { + configPath: configPath, + sync: true, + }); + var _result = loader.load(); + assign(result, _result ? _result.config : {}); + } else { + console.error('Failed to load inherited config', configName); } } - return result.extends ? applyInheritedConfig(result) : result; + return result; } function applyEnvironmentConfig(config) {