-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
79 lines (66 loc) · 2.7 KB
/
Copy pathcli.js
File metadata and controls
79 lines (66 loc) · 2.7 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
const wcagify = require('./wcagify')
const help = `Usage: wcagify <ref or name> [options]
Look up WCAG 2.2 success criteria.
Examples:
wcagify 1.4.3
wcagify focus order
wcagify 1.4.3 --markdown
wcagify --level AA
wcagify --level AA --impact Cognitive --markdown
Options:
-m, --markdown Output as a markdown link
-j, --json Output as JSON
-l, --level <level> List criteria for a conformance level (A, AA, AAA, Removed)
-i, --impact <impact> List criteria by impact (Auditory, Cognitive, Motor, Visual)
-w, --wcag <version> WCAG version to link to: 2.2 (default) or 2.1
-h, --help Show this help
-v, --version Show the version`
const asMarkdown = reference => `[${reference.criterion}](${reference.link})`
const asText = reference => [
reference.criterion,
`Level: ${reference.level}`,
`Impacts: ${reference.impacts.join(', ') || 'None'}`,
reference.link
].join('\n')
function run (args, out, err) {
const flags = { markdown: false, json: false, level: null, impact: null, wcag: undefined }
const words = []
for (let i = 0; i < args.length; i++) {
const arg = args[i]
if (arg === '--markdown' || arg === '-m') flags.markdown = true
else if (arg === '--json' || arg === '-j') flags.json = true
else if (arg === '--level' || arg === '-l') flags.level = String(args[++i])
else if (arg === '--impact' || arg === '-i') flags.impact = String(args[++i])
else if (arg === '--wcag' || arg === '-w') flags.wcag = String(args[++i])
else if (arg === '--help' || arg === '-h') { out(help); return 0 }
else if (arg === '--version' || arg === '-v') { out(require('./package.json').version); return 0 }
else words.push(arg)
}
const options = { version: flags.wcag }
try {
if (flags.level || flags.impact) {
let list = flags.level ? wcagify.byLevel(flags.level, options) : wcagify.byImpact(flags.impact, options)
if (flags.level && flags.impact) {
const impacted = new Set(wcagify.byImpact(flags.impact, options).map(reference => reference.ref))
list = list.filter(reference => impacted.has(reference.ref))
}
if (flags.json) out(JSON.stringify(list, null, 2))
else list.forEach(reference => out(flags.markdown ? asMarkdown(reference) : `${reference.criterion} (Level ${reference.level})`))
return 0
}
const query = words.join(' ')
if (!query) {
err(help)
return 1
}
const reference = wcagify(query, options)
if (flags.json) out(JSON.stringify(reference, null, 2))
else if (flags.markdown) out(asMarkdown(reference))
else out(asText(reference))
return 0
} catch (error) {
err(error.message)
return 1
}
}
module.exports = run