From 33cb21032ec9d06baac4c69fc0dbf174b89b8944 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 1 Sep 2022 10:42:48 +0200 Subject: [PATCH] feat: allow extending from multiple keys resolves #24 --- src/loader.ts | 14 ++++++++------ test/fixture/config.ts | 2 +- test/index.test.ts | 15 +++++++++------ 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/loader.ts b/src/loader.ts index 76612ea..b53207d 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -40,7 +40,7 @@ export interface LoadConfigOptions { resolve?: (id: string, opts: LoadConfigOptions) => null | ResolvedConfig | Promise extend?: false | { - extendKey?: string + extendKey?: string | string[] } } @@ -128,12 +128,14 @@ export async function loadConfig (opts: LoadC async function extendConfig (config, opts: LoadConfigOptions) { config._layers = config._layers || [] - if (!opts.extend) { - return + if (!opts.extend) { return } + let keys = opts.extend.extendKey + if (typeof keys === 'string') { keys = [keys] } + const extendSources = [] + for (const key of keys) { + extendSources.push(...(Array.isArray(config[key]) ? config[key] : [config[key]]).filter(Boolean)) + delete config[key] } - const key = opts.extend.extendKey - const extendSources = (Array.isArray(config[key]) ? config[key] : [config[key]]).filter(Boolean) - delete config[key] for (const extendSource of extendSources) { const _config = await resolveConfig(extendSource, opts) if (!_config.config) { diff --git a/test/fixture/config.ts b/test/fixture/config.ts index f52feaf..9dee3ca 100644 --- a/test/fixture/config.ts +++ b/test/fixture/config.ts @@ -1,6 +1,6 @@ export default { + theme: './theme', extends: [ - './theme', './config.dev', 'c12-npm-test' ], diff --git a/test/index.test.ts b/test/index.test.ts index cd736b8..80f5972 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -11,6 +11,9 @@ describe('c12', () => { const { config, layers } = await loadConfig({ cwd: r('./fixture'), dotenv: true, + extend: { + extendKey: ['theme', 'extends'] + }, resolve: (id) => { if (id === 'virtual') { return { config: { virtual: true } } @@ -67,11 +70,11 @@ describe('c12', () => { }, "configFile": true, "extends": [ - "./theme", "./config.dev", "c12-npm-test", ], "overriden": false, + "theme": "./theme", }, "configFile": "config", "cwd": "/fixture", @@ -82,11 +85,6 @@ describe('c12', () => { }, "configFile": ".configrc", }, - { - "config": { - "virtual": true, - }, - }, { "config": { "colors": { @@ -111,6 +109,11 @@ describe('c12', () => { "configFile": "/fixture/base/config.ts", "cwd": "/fixture/base", }, + { + "config": { + "virtual": true, + }, + }, { "config": { "devConfig": true,