-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
93 lines (83 loc) · 2.36 KB
/
cli.js
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
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
import subarg from 'subarg'
import fs from 'node:fs'
import * as hallmark from './index.js'
if (process.version.match(/^v(\d+)\./)[1] < 16) {
// Return silently to support hallmark in 'npm test'
console.error('Skipping hallmark: Node 16 or greater is required.')
process.exit(0)
}
const argv = subarg(process.argv.slice(2), {
boolean: ['fix', 'help', 'version', 'commits', 'verbose'],
string: ['report'],
default: {
fix: false,
help: false,
version: false,
commits: true
},
alias: {
h: 'help',
v: 'version',
i: 'ignore'
}
})
if (argv.help) {
usage(0)
} else if (argv.version) {
const fp = new URL('./package.json', import.meta.url)
console.log(JSON.parse(fs.readFileSync(fp, 'utf8')).version)
} else {
const { commits, _: rest, ...options } = argv
if (rest[0] === 'lint') {
options.files = files(rest.slice(1))
hallmark.lint(options, done)
} else if (rest[0] === 'fix') {
options.files = files(rest.slice(1))
hallmark.fix(options, done)
} else if (rest[0] === 'bump') {
console.error("Error: the 'bump' command has been renamed to 'cc add'.\n")
usage(1)
} else if (rest[0] === 'cc') {
if (rest[1] === 'add') {
const targets = rest.slice(2)
if (!targets.length || !targets.every(Boolean)) usage(1)
hallmark.cc.add(targets, { ...options, commits }, done)
} else if (rest[1] === 'init') {
const { gte, lte, ...rest } = options
hallmark.cc.add({ gte, lte }, { ...rest, commits }, done)
} else {
console.error('Error: unknown command.')
usage(1)
}
} else {
// Old usage (no commands)
// TODO: deprecate?
options.files = files(rest)
hallmark[options.fix ? 'fix' : 'lint'](options, done)
}
}
function files (rest) {
return rest.length ? rest : null
}
function done (err, result) {
if (err) {
// Due to bundling the CLI into 1 file the error can be noisy (#120) so
// print a more friendly (shorter) message by default.
if (argv.verbose) throw err
console.error('%s: %s', err.name, err.message || err)
process.exit(1)
}
process.exit(result.code)
}
function usage (exitCode) {
const fp = new URL('./USAGE', import.meta.url)
const usage = fs.readFileSync(fp, 'utf8').trim()
if (exitCode) {
console.error(usage)
process.exit(exitCode)
} else {
console.log(usage)
process.exit()
}
}