Skip to content

Commit 4e42e32

Browse files
authored
fix(cli): prevent empty CLI files from overriding config-file files (#120)
1 parent 1f9277c commit 4e42e32

3 files changed

Lines changed: 33 additions & 12 deletions

File tree

src/cli/parse-args.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ export async function parseArgs(): Promise<ParsedArgs> {
2525
try {
2626
const { args, resultArgs } = loadCliArgs()
2727

28+
// Strip a leading release type / version number from positional args before
29+
// passing files to loadBumpConfig, so it doesn't override config-file `files`.
30+
const rawFiles = [...(args['--'] || []), ...resultArgs]
31+
let releaseFromArgs: string | undefined
32+
if (rawFiles.length > 0) {
33+
const firstArg = rawFiles[0]
34+
if (firstArg === 'prompt' || isReleaseType(firstArg) || isValidVersion(firstArg)) {
35+
releaseFromArgs = firstArg
36+
rawFiles.shift()
37+
}
38+
}
39+
2840
const parsedArgs: ParsedArgs = {
2941
help: args.help as boolean,
3042
version: args.version as boolean,
@@ -40,27 +52,17 @@ export async function parseArgs(): Promise<ParsedArgs> {
4052
confirm: args.yes === undefined ? undefined : !args.yes,
4153
noVerify: args.verify === undefined ? undefined : !args.verify,
4254
install: args.install,
43-
files: [...(args['--'] || []), ...resultArgs],
55+
files: rawFiles.length ? rawFiles : undefined,
4456
ignoreScripts: args.ignoreScripts,
4557
currentVersion: args.currentVersion,
4658
execute: args.execute,
4759
printCommits: args.printCommits,
4860
recursive: args.recursive,
49-
release: args.release,
61+
release: args.release ?? releaseFromArgs,
5062
configFilePath: args.configFilePath,
5163
}),
5264
}
5365

54-
// If a version number or release type was specified, then it will mistakenly be added to the "files" array
55-
if (parsedArgs.options.files && parsedArgs.options.files.length > 0) {
56-
const firstArg = parsedArgs.options.files[0]
57-
58-
if (firstArg === 'prompt' || isReleaseType(firstArg) || isValidVersion(firstArg)) {
59-
parsedArgs.options.release = firstArg
60-
parsedArgs.options.files.shift()
61-
}
62-
}
63-
6466
if (parsedArgs.options.recursive && parsedArgs.options.files?.length)
6567
console.log(styleText('yellow', 'The --recursive option is ignored when files are specified'))
6668

test/fixtures/bump.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { defineConfig } from '../../src'
2+
3+
export default defineConfig({
4+
files: ['custom.json', 'packages/**/package.json'],
5+
})

test/parse-args.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,17 @@ describe('parseArgs (confirm regression fix)', () => {
9797
expect(options.confirm).toBe(true)
9898
})
9999
})
100+
101+
describe('loadBumpConfig (files override fix #119)', () => {
102+
const fixtureDir = new URL('./fixtures', import.meta.url).pathname
103+
104+
it('preserves config files when no CLI files are passed (files: undefined)', async () => {
105+
const config = await loadBumpConfig({ files: undefined }, fixtureDir)
106+
expect(config.files).toEqual(['custom.json', 'packages/**/package.json'])
107+
})
108+
109+
it('overrides config files when CLI files are explicitly passed', async () => {
110+
const config = await loadBumpConfig({ files: ['explicit.json'] }, fixtureDir)
111+
expect(config.files).toEqual(['explicit.json'])
112+
})
113+
})

0 commit comments

Comments
 (0)