-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcli.js
More file actions
431 lines (415 loc) · 14 KB
/
Copy pathcli.js
File metadata and controls
431 lines (415 loc) · 14 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/env node
import { Chalk } from 'chalk'
import yargs from 'yargs/yargs'
import { hideBin } from 'yargs/helpers'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { pad } from 'lodash-es'
import { readFile } from 'node:fs/promises'
import { DateTime } from 'luxon'
import { gte } from 'semver'
import { Listr, ListrDefaultRendererLogLevels } from 'listr2'
import { checkNits, getAllValidations } from './lib/index.mjs'
import { getModeByName } from './lib/config/modes.mjs'
// Check Node.js version
if (!gte(process.version, '18.0.0')) {
console.error('idnits3 requires Node.js v18 or later.')
process.exit(1)
}
// Get package version
const cliDir = path.dirname(fileURLToPath(import.meta.url))
const pkgInfo = JSON.parse(await readFile(path.join(cliDir, 'package.json'), 'utf8'))
// Define CLI arguments config
const argv = yargs(hideBin(process.argv))
.scriptName('idnits')
.usage('$0 [args] <file-path|http-url>')
.example([
['$0 draft-ietf-abcd-01.xml', ''],
[`$0 -m submission -y ${DateTime.now().year} draft-ietf-abcd-01.xml`, ''],
['$0 https://www.rfc-editor.org/rfc/rfc2549', '']
])
.option('filter', {
alias: 'f',
describe: 'Filter output to only certain severity types. Can be declared multiple times to filter multiple severity types.',
choices: ['errors', 'warnings', 'comments'],
default: [],
nargs: 1,
type: 'array'
})
.option('mode', {
alias: 'm',
describe: 'Validation mode to use',
coerce: val => {
try {
const mode = getModeByName(val)
return mode.name
} catch (err) {
return val
}
},
choices: ['normal', 'forgive-checklist', 'submission'],
default: 'normal',
type: 'string'
})
.option('progress', {
default: true,
type: 'boolean',
hidden: true
})
.option('no-progress', {
describe: 'Disable progress messages / animations in pretty output',
type: 'boolean'
})
.option('offline', {
default: false,
describe: 'Disable validations that require an internet connection',
type: 'boolean'
})
.option('output', {
alias: 'o',
describe: 'Output format',
choices: ['pretty', 'simple', 'json', 'count'],
default: 'pretty',
type: 'string'
})
.option('solarized', {
default: false,
describe: 'Use alternate colors for a solarized light themed terminal',
type: 'boolean'
})
.option('color', {
default: true,
type: 'boolean',
hidden: true
})
.option('no-color', {
describe: 'Disable color in pretty output',
type: 'boolean'
})
.command('* <file>', 'parse and validate document', (y) => {
y.positional('file', {
type: 'string',
describe: 'Path / URL of the document to validate'
})
})
.strict()
.alias({ h: 'help' })
.help()
.version(pkgInfo.version)
.parse()
const chalk = (argv.color === false) ? new Chalk({ level: 0 }) : new Chalk()
if (argv.output === 'pretty') {
console.log(chalk.bgGray.white('▄'.repeat(64)))
console.log(chalk.bgWhite.black(`${pad('idnits ▶ ' + pkgInfo.version, 64)}`))
console.log(chalk.bgGray.white('▀'.repeat(64)))
console.log()
}
// Read document
let docRaw = ''
let docPath = ''
let docPathObj = null
if (argv.file.startsWith('http://') || argv.file.startsWith('https://')) {
// -> Remote
docPath = argv.file.trim()
const docPathUrl = new URL(docPath)
docPathObj = path.parse(docPathUrl.pathname)
if (argv.output === 'pretty') {
console.log(chalk.bgWhite.black(' Url ') + ` ${docPath}`)
}
try {
const resp = await fetch(docPath)
if (!resp.ok) {
throw new Error(resp.statusText)
}
docRaw = Buffer.from(await resp.arrayBuffer())
if (docRaw.length < 5) {
throw new Error('Document is empty!')
}
} catch (err) {
console.error(chalk.redBright(`Failed to fetch remote document: ${err.message}`))
process.exit(1)
}
} else {
// -> Local
docPath = path.resolve(process.cwd(), argv.file)
docPathObj = path.parse(docPath)
if (argv.output === 'pretty') {
console.log(chalk.bgWhite.black(' Path ') + ` ${docPath}`)
}
try {
docRaw = await readFile(docPath)
if (docRaw.length < 5) {
throw new Error('Document is empty!')
}
} catch (err) {
console.error(chalk.redBright(`Failed to read document: ${err.message}`))
process.exit(1)
}
}
// Get Mode
const mode = getModeByName(argv.mode).mode
if (argv.output === 'pretty') {
console.log(chalk.bgWhite.black(' Mode ') + ` ${argv.mode} ` + chalk.grey(`[${mode}]`))
console.log()
}
// Solarized-adapted chalk
function chalkAdapted (color) {
switch (color) {
case 'whiteBright':
return argv.solarized ? chalk.blackBright : chalk.whiteBright
case 'white':
return argv.solarized ? chalk.black : chalk.white
}
}
// Validate document
try {
let result = []
const docSizeBytes = Buffer.byteLength(docRaw)
// Validate document using task processor
if (argv.output === 'pretty' && argv.progress) {
const validations = getAllValidations(docPathObj.base.endsWith('.xml') ? 'xml' : 'txt')
const tasks = new Listr(
validations.map(valGroup => ({
title: valGroup.title,
task: () => new Listr(
valGroup.tasks.map(valTask => ({
title: valTask.title,
task: async (ctx) => {
if (valTask.isVoid) {
return valTask.task(ctx)
} else {
result.push(...(await valTask.task(ctx)))
}
}
})),
{
concurrent: valGroup.concurrent
}
),
skip: valGroup.condition ? (ctx) => !valGroup.condition(ctx) : undefined
})),
{
ctx: {
raw: docRaw,
filename: docPathObj.base,
options: {
mode,
offline: argv.offline
}
},
collectErrors: 'minimal',
exitOnError: true,
rendererOptions: {
collapseErrors: false,
collapseSubtasks: true,
showErrorMessage: false,
icon: {
[ListrDefaultRendererLogLevels.COMPLETED]: '☑️',
[ListrDefaultRendererLogLevels.FAILED]: '❌'
}
}
}
)
await tasks.run()
console.info('')
} else {
result = await checkNits(docRaw, docPathObj.base, {
mode,
offline: argv.offline
})
}
// Filter severity types
if (argv.filter && argv.filter.length > 0) {
result = result.filter(entry => {
switch (entry.constructor.name) {
case 'ValidationError': {
return argv.filter.includes('errors')
}
case 'ValidationWarning': {
return argv.filter.includes('warnings')
}
case 'ValidationComment': {
return argv.filter.includes('comments')
}
default: {
return true
}
}
})
}
// Stats by severity
const nitsBySeverity = {
error: 0,
warning: 0,
comment: 0
}
for (const res of result) {
switch (res.constructor.name) {
case 'ValidationError': {
nitsBySeverity.error++
break
}
case 'ValidationWarning': {
nitsBySeverity.warning++
break
}
case 'ValidationComment': {
nitsBySeverity.comment++
break
}
}
}
// Output results
switch (argv.output) {
// COUNT | Only return number of nits
case 'count': {
console.log(result.length)
break
}
// JSON | Return results as a stringified JSON object
case 'json': {
console.log(JSON.stringify({
result: result.length > 0 ? 'fail' : 'pass',
file: {
path: docPath,
size: docSizeBytes
},
nitsBySeverity,
nits: result.map(r => ({
severity: r.constructor.name,
code: r.name,
desc: r.message,
...r.text && { text: r.text },
...r.refUrl && { ref: r.refUrl },
...r.path && { path: r.path },
...r.lines && { line: r.lines }
}))
}, null, 2))
break
}
// SIMPLE | Results as a simple list
case 'simple': {
if (result.length === 0) {
console.log(`PASS - Document ${docPath} is nit-free. (mode: ${argv.mode})\n`)
} else {
console.error(`FAIL - Document ${docPath} has nits. (mode: ${argv.mode})\n`)
let entryIdx = 1
const validationSeverity = {
ValidationError: 'Error',
ValidationWarning: 'Warning',
ValidationComment: 'Comment'
}
for (const entry of result) {
console.log(`[${entryIdx}] ${validationSeverity[entry.constructor.name]} | ${entry.name} | ${entry.message}`)
entryIdx++
}
}
break
}
// PRETTY | Human-readable result view
case 'pretty': {
if (result.length === 0) {
console.log(chalk.bgGreen.whiteBright(' PASS ') + chalk.greenBright(' No nit found for this document. 🎉\n'))
} else {
let resultIcon = ''
const resultSeverities = []
if (nitsBySeverity.comment > 0) {
resultIcon = 'ℹ️'
resultSeverities.push(nitsBySeverity.comment + (nitsBySeverity.comment > 1 ? chalk.cyanBright(' comments') : chalk.cyanBright(' comment')))
}
if (nitsBySeverity.warning > 0) {
resultIcon = '⚠️'
resultSeverities.unshift(nitsBySeverity.warning + (nitsBySeverity.warning > 1 ? chalk.yellowBright(' warnings') : chalk.yellowBright(' warning')))
}
if (nitsBySeverity.error > 0) {
resultIcon = '❌'
resultSeverities.unshift(nitsBySeverity.error + (nitsBySeverity.error > 1 ? chalk.redBright(' errors') : chalk.redBright(' error')))
}
console.log(resultIcon + ' Review the ' + resultSeverities.join(', ') + ' listed below.\n')
// Format errors
let entryIdx = 1
for (const sev of ['ValidationError', 'ValidationWarning', 'ValidationComment']) {
switch (sev) {
case 'ValidationError': {
if (nitsBySeverity.error > 0) {
console.log(chalk.red('█'.repeat(7) + '▀'.repeat(57)))
console.log(chalk.bgRed.whiteBright(' ERROR ') + ` ${nitsBySeverity.error} nit${nitsBySeverity.error > 1 ? 's' : ''} of ⛔ error severity`)
console.log(chalk.red('█'.repeat(7) + '▄'.repeat(57)) + '\n')
}
break
}
case 'ValidationWarning': {
if (nitsBySeverity.warning > 0) {
console.log(chalk.yellow('█'.repeat(9) + '▀'.repeat(55)))
console.log(chalk.bgYellow.whiteBright(' WARNING ') + ` ${nitsBySeverity.warning} nit${nitsBySeverity.warning > 1 ? 's' : ''} of ⚠️ warning severity`)
console.log(chalk.yellow('█'.repeat(9) + '▄'.repeat(55)) + '\n')
}
break
}
case 'ValidationComment': {
if (nitsBySeverity.comment > 0) {
console.log(chalk.cyan('█'.repeat(9) + '▀'.repeat(55)))
console.log(chalk.bgCyan.whiteBright(' COMMENT ') + ` ${nitsBySeverity.comment} nit${nitsBySeverity.comment > 1 ? 's' : ''} of ℹ️ comment severity`)
console.log(chalk.cyan('█'.repeat(9) + '▄'.repeat(55)) + '\n')
}
break
}
}
for (const entry of result.filter(r => r.constructor.name === sev)) {
switch (entry.constructor.name) {
case 'ValidationError': {
console.log(chalk.bgRed.whiteBright(` ${entryIdx} `) + ' ' + chalk.redBright(entry.name))
// console.log(chalk.grey(' └- ') + chalkAdapted('white')('Code') + chalk.grey(' - ') + chalk.redBright(entry.name))
break
}
case 'ValidationWarning': {
console.log(chalk.bgYellow.whiteBright(` ${entryIdx} `) + ' ' + chalk.yellowBright(entry.name))
// console.log(chalk.grey(' └- ') + chalkAdapted('white')('Code') + chalk.grey(' - ') + chalk.yellowBright(entry.name))
break
}
case 'ValidationComment': {
console.log(chalk.bgCyan.whiteBright(` ${entryIdx} `) + ' ' + chalk.cyanBright(entry.name))
// console.log(chalk.grey(' └- ') + chalkAdapted('white')('Code') + chalk.grey(' - ') + chalk.cyanBright(entry.name))
break
}
default: {
console.log(chalk.bgRed.whiteBright(` ${entryIdx} `) + ' Unexpected Error')
}
}
console.log(chalk.grey(' └- ') + chalkAdapted('white')('Desc') + chalk.grey(' - ') + chalkAdapted('whiteBright')(entry.message))
if (entry.text) {
console.log(chalk.grey(' └- ') + chalkAdapted('white')('Text') + chalk.grey(' - ') + chalkAdapted('white')(entry.text))
}
if (entry.refUrl) {
console.log(chalk.grey(' └- ') + chalkAdapted('white')('Ref ') + chalk.grey(' - ') + chalk.cyan(entry.refUrl))
}
if (entry.path) {
console.log(chalk.grey(' └- ') + chalkAdapted('white')('Path') + chalk.grey(' - ') + chalkAdapted('white')(entry.path))
}
if (entry.lines) {
const lines = []
for (const line of entry.lines) {
lines.push(`Ln ${line.line} Col ${line.pos}`)
}
console.log(chalk.grey(' └- ') + chalkAdapted('white')('Line') + chalk.grey(' - ') + chalkAdapted('white')(lines.join(', ')))
}
console.log() // Empty line between entries
entryIdx++
}
}
if (result.length >= 5) {
console.log('-'.repeat(64) + '\n')
console.log(resultIcon + ' Review the ' + resultSeverities.join(', ') + ' listed above.\n')
}
}
break
}
default: {
throw new Error('Invalid Output Mode')
}
}
} catch (err) {
console.debug(err)
console.error(chalk.redBright(`Validation did not complete. Error:\n- ${err.message}`))
process.exit(1)
}