-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathcmd.js
executable file
·162 lines (143 loc) · 3.45 KB
/
cmd.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env node
import { exec } from 'node:child_process'
import fs from 'node:fs'
import http from 'node:http'
import https from 'node:https'
import path from 'node:path'
import nopt from 'nopt'
import pretty from '../lib/format-pretty.js'
import formatTap from '../lib/format-tap.js'
import Validator from '../lib/validator.js'
import Tap from '../lib/tap.js'
import * as utils from '../lib/utils.js'
import subsystem from '../lib/rules/subsystem.js'
const knownOpts = {
help: Boolean,
version: Boolean,
'validate-metadata': Boolean,
'validate-no-metadata': Boolean,
tap: Boolean,
out: path,
list: Boolean,
'list-subsystems': Boolean
}
const shortHand = {
h: ['--help'],
v: ['--version'],
V: ['--validate-metadata'],
t: ['--tap'],
o: ['--out'],
l: ['--list'],
ls: ['--list-subsystems']
}
const parsed = nopt(knownOpts, shortHand)
if (parsed.help) {
const usagePath = path.join(new URL(import.meta.url).pathname, '../usage.txt')
const help = await fs.promises.readFile(usagePath, 'utf8')
console.log(help)
process.exit(0)
}
if (parsed.version) {
const pkgJsonPath = path.join(new URL(import.meta.url).pathname, '../../package.json')
const pkgJson = await fs.promises.readFile(pkgJsonPath, 'utf8')
const { version } = JSON.parse(pkgJson)
console.log(`core-validate-commit v${version}`)
process.exit(0)
}
const args = parsed.argv.remain
if (!parsed.help && !args.length) { args.push('HEAD') }
function load (sha, cb) {
try {
const parsed = new URL(sha)
return loadPatch(parsed, cb)
} catch (_) {
exec(`git show --quiet --format=medium ${sha}`, (err, stdout, stderr) => {
if (err) return cb(err)
cb(null, stdout.trim())
})
}
}
function loadPatch (uri, cb) {
let h = http
if (~uri.protocol.indexOf('https')) {
h = https
}
const headers = {
'user-agent': 'core-validate-commit'
}
h.get(uri, { headers }, (res) => {
let buf = ''
res.on('data', (chunk) => {
buf += chunk
})
res.on('end', () => {
try {
const out = JSON.parse(buf)
cb(null, out)
} catch (err) {
cb(err)
}
})
}).on('error', cb)
}
const v = new Validator(parsed)
if (parsed['list-subsystems']) {
utils.describeSubsystem(subsystem.defaults.subsystems.sort())
process.exit(0)
}
if (parsed.list) {
const ruleNames = Array.from(v.rules.keys())
const max = ruleNames.reduce((m, item) => {
if (item.length > m) m = item.length
return m
}, 0)
for (const rule of v.rules.values()) {
utils.describeRule(rule, max)
}
process.exit(0)
}
if (parsed.tap) {
const tap = new Tap()
tap.pipe(process.stdout)
if (parsed.out) tap.pipe(fs.createWriteStream(parsed.out))
let count = 0
const total = args.length
v.on('commit', (c) => {
count++
const test = tap.test(c.commit.sha)
formatTap(test, c.commit, c.messages, v)
if (count === total) {
setImmediate(() => {
tap.end()
if (tap.status === 'fail') { process.exitCode = 1 }
})
}
})
tapRun()
} else {
v.on('commit', (c) => {
pretty(c.commit, c.messages, v)
commitRun()
})
commitRun()
}
function tapRun () {
if (!args.length) return
const sha = args.shift()
load(sha, (err, data) => {
if (err) throw err
v.lint(data)
tapRun()
})
}
function commitRun () {
if (!args.length) {
process.exitCode = v.errors
return
}
const sha = args.shift()
load(sha, (err, data) => {
if (err) throw err
v.lint(data)
})
}