Skip to content

Commit

Permalink
feat: find config in parent directory (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
aleclarson and antfu committed Mar 4, 2024
1 parent 0ef9933 commit 032bd7a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -62,6 +62,7 @@
"@jsdevtools/ez-spawn": "^3.0.4",
"c12": "^1.9.0",
"cac": "^6.7.14",
"escalade": "^3.1.2",
"fast-glob": "^3.3.2",
"js-yaml": "^4.1.0",
"prompts": "^2.4.2",
Expand Down
14 changes: 8 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 39 additions & 11 deletions src/config.ts
@@ -1,5 +1,7 @@
import process from 'node:process'
import { dirname } from 'node:path'
import { loadConfig } from 'c12'
import escalade from 'escalade/sync'
import type { VersionBumpOptions } from './types/version-bump-options'

export const bumpConfigDefaults: VersionBumpOptions = {
Expand All @@ -18,25 +20,51 @@ export async function loadBumpConfig(
overrides?: Partial<VersionBumpOptions>,
cwd = process.cwd(),
) {
const { config: bumppConfig } = await loadConfig<VersionBumpOptions>({
name: 'bumpp',
overrides: {
...(overrides as VersionBumpOptions),
},
cwd,
})
const name = 'bump'
const configFile = findConfigFile(name, cwd)
const { config } = await loadConfig<VersionBumpOptions>({
name: 'bump',
name,
defaults: bumpConfigDefaults,
overrides: {
...(bumppConfig!),
...(overrides as VersionBumpOptions),
},
cwd,
cwd: configFile ? dirname(configFile) : cwd,
})

return config!
}

function findConfigFile(name: string, cwd: string) {
let foundRepositoryRoot = false
try {
const candidates = ['js', 'mjs', 'ts', 'mts', 'json'].map(ext => `${name}.config.${ext}`)
return escalade(cwd, (_dir, files) => {
const match = files.find((file) => {
if (candidates.includes(file))
return true
if (file === '.git')
foundRepositoryRoot = true
return false
})

if (match)
return match

// Stop at the repository root.
if (foundRepositoryRoot) {
// eslint-disable-next-line no-throw-literal
throw null
}

return false
})
}
catch (error) {
if (foundRepositoryRoot)
return null
throw error
}
}

export function defineConfig(config: Partial<VersionBumpOptions>) {
return config
}

0 comments on commit 032bd7a

Please sign in to comment.