forked from JS-DevTools/version-bump-prompt
/
parse-args.ts
83 lines (74 loc) · 2.5 KB
/
parse-args.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { valid as isValidVersion } from 'semver'
import cac from 'cac'
import { isReleaseType } from '../release-type'
import type { VersionBumpOptions } from '../types/version-bump-options'
import { version } from '../../package.json'
import { ExitCode } from './exit-code'
/**
* The parsed command-line arguments
*/
export interface ParsedArgs {
help?: boolean
version?: boolean
quiet?: boolean
options: VersionBumpOptions
}
/**
* Parses the command-line arguments
*/
export function parseArgs(): ParsedArgs {
try {
const cli = cac('bumpp')
cli
.version(version)
.usage('[...files]')
.option('--preid <preid>', 'ID for prerelease')
.option('--all', 'Include all files')
.option('-c, --commit [msg]', 'Commit message', { default: true })
.option('-t, --tag [tag]', 'Tag name', { default: true })
.option('-p, --push', 'Push to remote', { default: true })
.option('-y, --yes', 'Skip confirmation')
.option('--no-verify', 'Skip git verification')
.option('--ignore-scripts', 'Ignore scripts', { default: false })
.option('-q, --quiet', 'Quiet mode')
.option('-v, --version <version>', 'Tagert version')
.option('-x, --execute <command>', 'Commands to execute after version bumps')
.help()
const result = cli.parse()
const args = result.options
const parsedArgs: ParsedArgs = {
help: args.help as boolean,
version: args.version as boolean,
quiet: args.quiet as boolean,
options: {
preid: args.preid,
commit: args.commit,
tag: args.tag,
push: args.push,
all: args.all,
confirm: !args.yes,
noVerify: !args.verify,
files: [...args['--'] || [], ...result.args],
ignoreScripts: args.ignoreScripts,
execute: args.execute,
},
}
// If a version number or release type was specified, then it will mistakenly be added to the "files" array
if (parsedArgs.options.files && parsedArgs.options.files.length > 0) {
const firstArg = parsedArgs.options.files[0]
if (firstArg === 'prompt' || isReleaseType(firstArg) || isValidVersion(firstArg)) {
parsedArgs.options.release = firstArg
parsedArgs.options.files.shift()
}
}
return parsedArgs
}
catch (error) {
// There was an error parsing the command-line args
return errorHandler(error as Error)
}
}
function errorHandler(error: Error): never {
console.error(error.message)
return process.exit(ExitCode.InvalidArgument)
}