Skip to content

Commit d9660fe

Browse files
authoredMar 4, 2024
fix: resolve the right config file and args from cli (#20)
1 parent b765d45 commit d9660fe

File tree

6 files changed

+651
-540
lines changed

6 files changed

+651
-540
lines changed
 

‎package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"build": "tsup src/index.ts src/cli/index.ts --format esm,cjs --dts --clean",
5252
"watch": "npm run build -- --watch src",
5353
"start": "esno src/cli/run.ts",
54+
"test": "vitest",
5455
"upgrade": "npm-check -u && npm audit fix",
5556
"bumpp": "esno src/cli/run.ts",
5657
"prepublishOnly": "npm run clean && npm run build",
@@ -80,6 +81,7 @@
8081
"picocolors": "^1.0.0",
8182
"rimraf": "^5.0.5",
8283
"tsup": "^8.0.2",
83-
"typescript": "^5.3.3"
84+
"typescript": "^5.3.3",
85+
"vitest": "^1.3.1"
8486
}
8587
}

‎pnpm-lock.yaml

+555-511
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/cli/parse-args.ts

+45-26
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,21 @@ export interface ParsedArgs {
2626
*/
2727
export async function parseArgs(): Promise<ParsedArgs> {
2828
try {
29-
const cli = cac('bumpp')
30-
31-
cli
32-
.version(version)
33-
.usage('[...files]')
34-
.option('--preid <preid>', 'ID for prerelease')
35-
.option('--all', `Include all files (default: ${bumpConfigDefaults.all})`)
36-
.option('-c, --commit [msg]', `Commit message (default: ${bumpConfigDefaults.commit})`)
37-
.option('--no-commit', 'Skip commit')
38-
.option('-t, --tag [tag]', `Tag name (default: ${bumpConfigDefaults.tag})`)
39-
.option('--no-tag', 'Skip tag')
40-
.option('-p, --push', `Push to remote (default: ${bumpConfigDefaults.push})`)
41-
.option('-y, --yes', `Skip confirmation (default: ${!bumpConfigDefaults.confirm})`)
42-
.option('-r, --recursive', `Bump package.json files recursively (default: ${bumpConfigDefaults.recursive})`)
43-
.option('--no-verify', 'Skip git verification')
44-
.option('--ignore-scripts', `Ignore scripts (default: ${bumpConfigDefaults.ignoreScripts})`)
45-
.option('-q, --quiet', 'Quiet mode')
46-
.option('-v, --version <version>', 'Target version')
47-
.option('-x, --execute <command>', 'Commands to execute after version bumps')
48-
.help()
49-
50-
const result = cli.parse()
51-
const args = result.options
29+
const { args, resultArgs } = loadCliArgs()
5230

5331
const parsedArgs: ParsedArgs = {
5432
help: args.help as boolean,
5533
version: args.version as boolean,
5634
quiet: args.quiet as boolean,
5735
options: await loadBumpConfig({
5836
preid: args.preid,
59-
commit: !args.noCommit && args.commit,
60-
tag: !args.noTag && args.tag,
37+
commit: args.commit,
38+
tag: args.tag,
6139
push: args.push,
6240
all: args.all,
6341
confirm: !args.yes,
6442
noVerify: !args.verify,
65-
files: [...(args['--'] || []), ...result.args],
43+
files: [...(args['--'] || []), ...resultArgs],
6644
ignoreScripts: args.ignoreScripts,
6745
execute: args.execute,
6846
recursive: !!args.recursive,
@@ -116,6 +94,47 @@ export async function parseArgs(): Promise<ParsedArgs> {
11694
}
11795
}
11896

97+
export function loadCliArgs(argv = process.argv) {
98+
const cli = cac('bumpp')
99+
100+
cli
101+
.version(version)
102+
.usage('[...files]')
103+
.option('--preid <preid>', 'ID for prerelease')
104+
.option('--all', `Include all files (default: ${bumpConfigDefaults.all})`)
105+
.option('-c, --commit [msg]', 'Commit message', { default: true })
106+
.option('--no-commit', 'Skip commit', { default: false })
107+
.option('-t, --tag [tag]', 'Tag name', { default: true })
108+
.option('--no-tag', 'Skip tag', { default: false })
109+
.option('-p, --push', `Push to remote (default: ${bumpConfigDefaults.push})`)
110+
.option('-y, --yes', `Skip confirmation (default: ${!bumpConfigDefaults.confirm})`)
111+
.option('-r, --recursive', `Bump package.json files recursively (default: ${bumpConfigDefaults.recursive})`)
112+
.option('--no-verify', 'Skip git verification')
113+
.option('--ignore-scripts', `Ignore scripts (default: ${bumpConfigDefaults.ignoreScripts})`)
114+
.option('-q, --quiet', 'Quiet mode')
115+
.option('-v, --version <version>', 'Target version')
116+
.option('-x, --execute <command>', 'Commands to execute after version bumps')
117+
.help()
118+
119+
const result = cli.parse(argv)
120+
const rawArgs = cli.rawArgs
121+
const args = result.options
122+
123+
const hasCommitFlag = rawArgs.some(arg => ['-c', '--commit', '--no-commit'].includes(arg))
124+
const hasTagFlag = rawArgs.some(arg => ['-t', '--tag', '--no-tag'].includes(arg))
125+
126+
const { tag, commit, ...rest } = args
127+
128+
return {
129+
args: {
130+
...rest,
131+
commit: hasCommitFlag ? commit : undefined,
132+
tag: hasTagFlag ? tag : undefined,
133+
} as { [k: string]: any },
134+
resultArgs: result.args,
135+
}
136+
}
137+
119138
function errorHandler(error: Error): never {
120139
console.error(error.message)
121140
return process.exit(ExitCode.InvalidArgument)

‎src/config.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@ export async function loadBumpConfig(
1818
overrides?: Partial<VersionBumpOptions>,
1919
cwd = process.cwd(),
2020
) {
21+
const { config: bumppConfig } = await loadConfig<VersionBumpOptions>({
22+
name: 'bumpp',
23+
overrides: {
24+
...(overrides as VersionBumpOptions),
25+
},
26+
cwd,
27+
})
2128
const { config } = await loadConfig<VersionBumpOptions>({
2229
name: 'bump',
2330
defaults: bumpConfigDefaults,
2431
overrides: {
25-
...(overrides as VersionBumpOptions),
32+
...(bumppConfig!),
2633
},
2734
cwd,
2835
})

‎test/parse-args.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { loadCliArgs } from '../src/cli/parse-args'
3+
4+
const defaultArgs = ['node', 'bumpp']
5+
6+
describe('loadCliArgs', async () => {
7+
it('returns an object with the correct properties', () => {
8+
const result = loadCliArgs()
9+
10+
expect(typeof result).toBe('object')
11+
expect(typeof result.args).toBe('object')
12+
expect(typeof result.resultArgs).toBe('object')
13+
})
14+
15+
it('sets the commit property to undefined if no commit flag is present', () => {
16+
const result = loadCliArgs([...defaultArgs])
17+
18+
expect(result.args.commit).toBe(undefined)
19+
})
20+
21+
it('sets the commit property to true if `--commit` is present', () => {
22+
const result = loadCliArgs([...defaultArgs, '--commit'])
23+
24+
expect(result.args.commit).toBe(true)
25+
})
26+
27+
it('sets the commit property to true if `-c` is present', () => {
28+
const result = loadCliArgs([...defaultArgs, '-c'])
29+
30+
expect(result.args.commit).toBe(true)
31+
})
32+
33+
it('sets the commit property to false if `--no-commit` is present', () => {
34+
const result = loadCliArgs([...defaultArgs, '--no-commit'])
35+
36+
expect(result.args.commit).toBe(false)
37+
})
38+
})

‎tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"skipLibCheck": true
2020
},
2121
"include": [
22-
"src/**/*.ts"
22+
"src/**/*.ts",
23+
"test/**/*.ts"
2324
],
2425
"exclude": [
2526
"node_modules"

0 commit comments

Comments
 (0)
Please sign in to comment.