Skip to content

Commit

Permalink
feat: allow extending from multiple keys
Browse files Browse the repository at this point in the history
resolves #24
  • Loading branch information
pi0 committed Sep 1, 2022
1 parent e0cdc8d commit 33cb210
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
14 changes: 8 additions & 6 deletions src/loader.ts
Expand Up @@ -40,7 +40,7 @@ export interface LoadConfigOptions<T extends InputConfig=InputConfig> {
resolve?: (id: string, opts: LoadConfigOptions) => null | ResolvedConfig | Promise<ResolvedConfig | null>

extend?: false | {
extendKey?: string
extendKey?: string | string[]
}
}

Expand Down Expand Up @@ -128,12 +128,14 @@ export async function loadConfig<T extends InputConfig=InputConfig> (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) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/config.ts
@@ -1,6 +1,6 @@
export default {
theme: './theme',
extends: [
'./theme',
'./config.dev',
'c12-npm-test'
],
Expand Down
15 changes: 9 additions & 6 deletions test/index.test.ts
Expand Up @@ -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 } }
Expand Down Expand Up @@ -67,11 +70,11 @@ describe('c12', () => {
},
"configFile": true,
"extends": [
"./theme",
"./config.dev",
"c12-npm-test",
],
"overriden": false,
"theme": "./theme",
},
"configFile": "config",
"cwd": "<path>/fixture",
Expand All @@ -82,11 +85,6 @@ describe('c12', () => {
},
"configFile": ".configrc",
},
{
"config": {
"virtual": true,
},
},
{
"config": {
"colors": {
Expand All @@ -111,6 +109,11 @@ describe('c12', () => {
"configFile": "<path>/fixture/base/config.ts",
"cwd": "<path>/fixture/base",
},
{
"config": {
"virtual": true,
},
},
{
"config": {
"devConfig": true,
Expand Down

0 comments on commit 33cb210

Please sign in to comment.