-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli.js
executable file
·92 lines (76 loc) · 2.26 KB
/
cli.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
#!/usr/bin/env node
import { readFileSync } from 'fs'
import { join } from 'path'
import { fileURLToPath } from 'url'
import { formatReport, lint } from './index.js'
const ROOT = join(fileURLToPath(import.meta.url), '..')
function getPackage() {
return JSON.parse(readFileSync(join(ROOT, 'package.json')))
}
let args = process.argv.slice(2)
let USAGE =
'Usage:\n' +
' npx browserslist-lint\n' +
' npx browserslist-lint "QUERIES"\n' +
' npx browserslist-lint --json "QUERIES"\n' +
' npx browserslist-lint --config="path/to/browserlist/file"\n' +
' npx browserslist-lint --env="environment name defined in config"\n' +
' npx browserslist-lint --stats="path/to/browserlist/stats/file"\n'
function isArg(arg) {
return args.some(str => {
return str === arg || str.indexOf(arg + '=') === 0
})
}
function error(msg) {
process.stderr.write('browserslist-lint: ' + msg + '\n')
process.exit(1)
}
if (isArg('--help') || isArg('-h')) {
process.stdout.write(getPackage().description + '.\n\n' + USAGE + '\n')
} else if (isArg('--version') || isArg('-v')) {
process.stdout.write('browserslist-lint ' + getPackage().version + '\n')
} else {
let mode = 'human'
let opts = {}
let queries
for (let i = 0; i < args.length; i++) {
if (args[i][0] !== '-') {
queries = args[i].replace(/^["']|["']$/g, '')
continue
}
let arg = args[i].split('=')
let name = arg[0]
let value = arg[1]
if (value) value = value.replace(/^["']|["']$/g, '')
if (name === '--config' || name === '-b') {
opts.config = value
} else if (name === '--env' || name === '-e') {
opts.env = value
} else if (name === '--stats' || name === '-s') {
opts.stats = value
} else if (name === '--json') {
mode = 'json'
} else {
error('Unknown arguments ' + args[i] + '.\n\n' + USAGE)
}
}
let problems
try {
problems = lint(queries, opts)
} catch (e) {
if (e.name === 'BrowserslistError') {
error(e.message)
} else {
throw e
}
}
if (problems.length === 0) {
process.exit(0)
}
if (mode === 'human') {
process.stdout.write(formatReport(problems) + '\n')
} else if (mode === 'json') {
process.stdout.write(JSON.stringify(problems, null, ' ') + '\n')
}
process.exit(1)
}