Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: find config in parent directory #8

Merged
merged 4 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throw to stop the escalade loop from loading more directories.

}

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

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