1 | #!/usr/bin/env node
|
2 |
|
3 | const eslint = require('eslint')
|
4 | const eslintConfig = require('./eslint-config.js')
|
5 | const path = require('path')
|
6 | const globby = require('globby')
|
7 | const {gray, green, red} = require('kleur')
|
8 | const outdent = require('outdent')
|
9 |
|
10 | require('eslint-plugin-html')
|
11 |
|
12 | const extensions = ['.mjs', '.js', '.html']
|
13 | const args = {
|
14 | files: [],
|
15 | help: false,
|
16 | fix: false
|
17 | }
|
18 |
|
19 | for (const arg of process.argv.slice(2)) {
|
20 | if (arg.startsWith('-') && !arg.startsWith('---')) {
|
21 | if (arg === '--fix') {
|
22 | args.fix = true
|
23 | } else {
|
24 | args.help = true
|
25 | }
|
26 | } else {
|
27 | args.files.push(arg)
|
28 | }
|
29 | }
|
30 |
|
31 | if (!args.files.length) {
|
32 | args.help = true
|
33 | }
|
34 |
|
35 | if (args.help) {
|
36 | console.log(outdent`
|
37 |
|
38 | ${green('Description:')} lint js and css
|
39 |
|
40 | ${green('Usage:')} lint [--fix] [-h] <files>...
|
41 |
|
42 | ${green('Parameters:')}
|
43 |
|
44 | <files> what files to lint
|
45 |
|
46 | ${green('Options:')}
|
47 |
|
48 | --fix fix what can be fixed
|
49 | -h, --help get help
|
50 |
|
51 | `)
|
52 |
|
53 | process.exit(1)
|
54 | }
|
55 |
|
56 | (async () => {
|
57 | try {
|
58 | let files = await globby(args.files.map((file) => path.resolve(file)), {gitignore: true})
|
59 |
|
60 | files = files.filter((file) => extensions.includes(path.extname(file)))
|
61 |
|
62 | const errors = []
|
63 |
|
64 | const CLIEngine = eslint.CLIEngine
|
65 |
|
66 | const cli = new CLIEngine({
|
67 | fix: args.fix,
|
68 | envs: ['browser', 'node', 'es6'],
|
69 | useEslintrc: false,
|
70 | ...eslintConfig
|
71 | })
|
72 |
|
73 | const report = cli.executeOnFiles(files)
|
74 |
|
75 | for (const result of report.results.filter((r) => r.errorCount || r.warningCount)) {
|
76 | for (const message of result.messages) {
|
77 | errors.push({
|
78 | file: result.filePath,
|
79 | line: message.line,
|
80 | column: message.column,
|
81 | message: message.message
|
82 | })
|
83 | }
|
84 | }
|
85 |
|
86 | CLIEngine.outputFixes(report)
|
87 |
|
88 | if (errors.length) {
|
89 | console.log(`${gray('[dev]')} ${errors.length} problem${errors.length > 1 ? 's' : ''} found`)
|
90 |
|
91 | for (const {file, line, column, message} of errors.sort((a, b) => a.file.localeCompare(b.file))) {
|
92 | console.log(` ${file}:${line}:${column}: ${message}`)
|
93 | }
|
94 |
|
95 | process.exit(1)
|
96 | }
|
97 | } catch (err) {
|
98 | console.error(red(err))
|
99 |
|
100 | process.exit(1)
|
101 | }
|
102 | })()
|