-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
80 lines (62 loc) · 2.1 KB
/
index.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
#!/usr/bin/env node
import program from 'commander'
import { runChecks } from 'hydra-validator-core/run-checks.js'
import deps from 'matchdep'
import debug, { Debugger } from 'debug'
import { ResultKind } from 'hydra-validator-core'
export type Loggers = Record<string, Debugger>
const loggers: Loggers = {
success: debug('SUCCESS'),
informational: debug('INFO'),
warning: debug('WARNING'),
failure: debug('FAILURE'),
error: debug('ERROR'),
}
debug.enable('*')
debug.formatters.p = (level) => {
return level === 0 ? '' : ` ${'-'.repeat(level * 2)}`
}
debug.formatters.l = (logger: ResultKind) => {
if (logger === 'informational') {
return ' '
}
if (logger === 'error') {
return ' '
}
return ''
}
const plugins = deps.filterAll(['hydra-validator-*', 'hydra-validator-core'], `${process.cwd()}/package.json`)
async function main() {
for (const plugin of plugins) {
const match = plugin.match(/^hydra-validator-([\d\w]+)$/)
if (!match) {
continue
}
const commandName = match[1]
const { check, options } = await import(plugin)
const command = program
.command(`${commandName} <url>`)
if (options && Array.isArray(options)) {
for (const option of options) {
command.option(option.flags, option.description, option.defaultValue)
}
}
command.action(function (this: any, url: string) {
Promise.resolve()
.then(async () => {
let unsucessfulCount = 0
const firstCheck = check(url, { ...this, cwd: process.cwd(), log: loggers })
const checkGenerator = runChecks(firstCheck)
for await (const check of checkGenerator) {
loggers[check.result.status]('%l %p %s %s', check.result.status, check.level, check.result.description, (check.result as any).details || '')
if (check.result.status === 'failure' || check.result.status === 'error') {
unsucessfulCount++
}
}
process.exit(unsucessfulCount)
})
})
}
}
// eslint-disable-next-line no-console
main().then(() => program.parse(process.argv)).catch(console.error)