-
Notifications
You must be signed in to change notification settings - Fork 723
/
Copy pathcli.ts
86 lines (74 loc) · 2.65 KB
/
cli.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
84
85
86
import fs from 'fs'
import { Command } from 'commander'
import { generate } from '../src/generate'
import { validate } from '../src/validate'
import { version } from '../package.json'
const program = new Command()
program
.name('optl')
.description('CLI for generating and validating tokenlists')
.version(version)
program
.command('validate')
.description('Validate tokenlist data files')
.requiredOption('--datadir <datadir>', 'Directory containing data files')
.option(
'--tokens <tokens>',
'Comma-separated list of token symbols to validate'
)
.action(async (options) => {
const results = await validate(options.datadir, options.tokens.split(','))
const validationResultsFilePath = 'validation_results.txt'
const errs = results.filter((r) => r.type === 'error')
const warns = results.filter((r) => r.type === 'warning')
if (errs.length > 0 || warns.length > 0) {
fs.writeFileSync(
validationResultsFilePath,
`Below are the results from running validation for the token changes. To ` +
`re-run the validation locally run: ` +
`pnpm validate --datadir ./data --tokens ${options.tokens}\n\n`
)
}
if (errs.length > 0) {
fs.appendFileSync(
validationResultsFilePath,
`These errors caused the validation to fail:\n${errs
.map((err) => err.message)
.join('\r\n')}\n\n`
)
for (const err of errs) {
if (err.message.startsWith('final token list is invalid')) {
// Message generated here is super long and doesn't really give more information than the
// rest of the errors, so just print a short version of it instead.
console.error(`error: final token list is invalid`)
} else {
console.error(`error: ${err.message}`)
}
}
}
if (warns.length > 0) {
fs.appendFileSync(
validationResultsFilePath,
`These warnings were found during validation, but did not cause validation to fail:\n${warns
.map((warn) => warn.message)
.join('\r\n')}\n`
)
for (const warn of warns) {
console.log(`warning: ${warn.message}`)
}
}
if (errs.length > 0) {
// Exit with error code so CI fails
process.exit(1)
}
})
program
.command('generate')
.description('Generates a tokenlist data file')
.requiredOption('--datadir <datadir>', 'Directory containing data files')
.requiredOption('--outfile <outfile>', 'Output file to write')
.action(async (options) => {
const list = generate(options.datadir)
fs.writeFileSync(options.outfile, JSON.stringify(list, null, 2))
})
program.parse()