Skip to content

Commit

Permalink
feat: extends support with remote repo
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 31, 2022
1 parent 4885487 commit 17ef358
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 24 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- RC config support with [unjs/rc9](https://github.com/unjs/rc9)
- Multiple sources merged with [unjs/defu](https://github.com/unjs/defu)
- `.env` support with [dotenv](https://www.npmjs.com/package/dotenv)
- Support extending nested configurations
- Support extending nested configurations from multiple local or git sourecs with [tiged](https://github.com/tiged/tiged)

## Usage

Expand Down Expand Up @@ -107,6 +107,9 @@ Extending can be nested and each layer can extend from one base or more.

Final config is merged result of extended options and user options with [unjs/defu](https://github.com/unjs/defu).

Each item in extends, is a string that can be either an absolute or relative path to current config file pointing to a config file for extending or directory containing config file.
If it starts with either of `github:`, `gitlab:`, `bitbucket:` or `https:`, c12 autmatically clones it with [tiged](https://github.com/tiged/tiged).

For custom merging strategies, you can directly access each layer with `layers` property.

**Example:**
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"jiti": "^1.12.14",
"mlly": "^0.4.1",
"pathe": "^0.2.0",
"rc9": "^1.2.0"
"rc9": "^1.2.0",
"tiged": "^2.12.0"
},
"devDependencies": {
"@nuxtjs/eslint-config-typescript": "latest",
Expand Down
15 changes: 14 additions & 1 deletion src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { promises as fsp } from 'fs'
import os from 'os'
import { resolve, extname, dirname } from 'pathe'
import createJiti from 'jiti'
import * as rc9 from 'rc9'
import defu from 'defu'
import tiged from 'tiged'
import { DotenvOptions, setupDotenv } from './dotenv'

export interface InputConfig extends Record<string, any> {}
Expand Down Expand Up @@ -87,12 +90,22 @@ export async function loadConfig<T extends InputConfig=InputConfig> (opts: LoadC
return r
}

const TIGED_PREFIXES = ['github:', 'gitlab:', 'bitbucket:', 'https://']

async function extendConfig (config, configFile: string, cwd: string) {
config._layers = config._layers || []

const extendSources = (Array.isArray(config.extends) ? config.extends : [config.extends]).filter(Boolean)
delete config.extends
for (const extendSource of extendSources) {
for (let extendSource of extendSources) {
if (TIGED_PREFIXES.some(prefix => extendSource.startsWith(prefix))) {
const tmpdir = resolve(os.tmpdir(), 'c12/git', extendSource.replace(/[#]/g, '_'))
await fsp.rmdir(tmpdir, { recursive: true })
const t = tiged(extendSource, { cache: true })
await t.clone(tmpdir)
extendSource = tmpdir
}

const isDir = !extname(extendSource)
const _cwd = resolve(cwd, isDir ? extendSource : dirname(extendSource))
const _config = await loadConfigFile(_cwd, isDir ? configFile : extendSource)
Expand Down
41 changes: 30 additions & 11 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { fileURLToPath } from 'url'
import { resolve } from 'pathe'
import { expect, it, describe } from 'vitest'
import { loadConfig } from '../src'

describe('c12', () => {
it('load fixture config', async () => {
const fixtureDir = fileURLToPath(new URL('./fixture', import.meta.url))
const rFixture = (...segments: string[]) => resolve(fixtureDir, ...segments)
const r = path => fileURLToPath(new URL(path, import.meta.url))

it('load fixture config', async () => {
const { config, layers } = await loadConfig({
cwd: fixtureDir,
cwd: r('./fixture'),
dotenv: true,
overrides: {
overriden: true
Expand Down Expand Up @@ -41,8 +39,8 @@ describe('c12', () => {
secondary: 'theme_secondary'
}
},
configFile: rFixture('theme/config.ts'),
cwd: rFixture('theme')
configFile: r('./fixture/theme/config.ts'),
cwd: r('./fixture/theme')
},
{
config: {
Expand All @@ -52,14 +50,35 @@ describe('c12', () => {
text: 'base_text'
}
},
configFile: rFixture('base/config.ts'),
cwd: rFixture('base')
configFile: r('./fixture/base/config.ts'),
cwd: r('./fixture/base')
},
{
config: { devConfig: true },
configFile: rFixture('config.dev.ts'),
cwd: rFixture('.')
configFile: r('./fixture/config.dev.ts'),
cwd: r('./fixture')
}
])
})

it('extend from git repo', async () => {
const { config } = await loadConfig({
cwd: r('./fixture/new_dir'),
overrides: {
extends: ['github:unjs/c12/test/fixture']
}
})

expect(config).toMatchObject({
devConfig: true,
baseConfig: true,
colors: {
primary: 'user_primary',
text: 'base_text',
secondary: 'theme_secondary'
},
configFile: true,
overriden: false
})
})
})
Loading

0 comments on commit 17ef358

Please sign in to comment.