forked from standard/standard-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.js
More file actions
executable file
·174 lines (148 loc) · 4.34 KB
/
cmd.js
File metadata and controls
executable file
·174 lines (148 loc) · 4.34 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
#!/usr/bin/env node
module.exports = Cli
var minimist = require('minimist')
var getStdin = require('get-stdin')
function Cli (opts) {
var Linter = require('../').linter
var standard = opts.linter || new Linter(opts)
opts = Object.assign({
cmd: 'standard-engine',
tagline: 'JavaScript Custom Style',
version: '0.0.0'
}, opts)
var argv = minimist(process.argv.slice(2), {
alias: {
global: 'globals',
plugin: 'plugins',
env: 'envs',
help: 'h',
verbose: 'v'
},
boolean: [
'fix',
'help',
'stdin',
'verbose',
'version'
],
string: [
'global',
'plugin',
'parser',
'env'
]
})
// Unix convention: Command line argument `-` is a shorthand for `--stdin`
if (argv._[0] === '-') {
argv.stdin = true
argv._.shift()
}
if (argv.help) {
if (opts.tagline) console.log('%s - %s (%s)', opts.cmd, opts.tagline, opts.homepage)
console.log(`
Usage:
${opts.cmd} <flags> [FILES...]
If FILES is omitted, all JavaScript source files (*.js, *.jsx, *.mjs, *.cjs)
in the current working directory are checked, recursively.
Certain paths (node_modules/, coverage/, vendor/, *.min.js, and
files/folders that begin with '.' like .git/) are automatically ignored.
Paths in a project's root .gitignore file are also automatically ignored.
Flags:
--fix Automatically fix problems
-v, --verbose Show rule names for errors (to ignore specific rules)
--version Show current version
-h, --help Show usage information
Flags (advanced):
--stdin Read file text from stdin
--global Declare global variable
--plugin Use custom eslint plugin
--env Use custom eslint environment
--parser Use custom js parser (e.g. babel-eslint)
`)
process.exitCode = 0
return
}
if (argv.version) {
console.log(opts.version)
process.exitCode = 0
return
}
var lintOpts = {
fix: argv.fix,
globals: argv.global,
plugins: argv.plugin,
envs: argv.env,
parser: argv.parser
}
var stdinText
if (argv.stdin) {
getStdin().then(function (text) {
stdinText = text
standard.lintText(text, lintOpts, onResult)
})
} else {
standard.lintFiles(argv._, lintOpts, onResult)
}
function onResult (err, result) {
if (err) return onError(err)
if (argv.stdin && argv.fix) {
if (result.results[0].output) {
// Code contained fixable errors, so print the fixed code
process.stdout.write(result.results[0].output)
} else {
// Code did not contain fixable errors, so print original code
process.stdout.write(stdinText)
}
}
if (!result.errorCount && !result.warningCount) {
process.exitCode = 0
return
}
console.error('%s: %s (%s)', opts.cmd, opts.tagline, opts.homepage)
// Are any fixable rules present?
var isFixable = result.results.some(function (result) {
return result.messages.some(function (message) {
return !!message.fix
})
})
if (isFixable) {
console.error(
'%s: %s',
opts.cmd,
'Run `' + opts.cmd + ' --fix` to automatically fix some problems.'
)
}
result.results.forEach(function (result) {
result.messages.forEach(function (message) {
log(
' %s:%d:%d: %s%s',
result.filePath, message.line || 0, message.column || 0, message.message,
argv.verbose ? ' (' + message.ruleId + ')' : ''
)
})
})
process.exitCode = result.errorCount ? 1 : 0
}
function onError (err) {
console.error(opts.cmd + ': Unexpected linter output:\n')
console.error(err.stack || err.message || err)
console.error(
'\nIf you think this is a bug in `%s`, open an issue: %s',
opts.cmd, opts.bugs
)
process.exitCode = 1
}
/**
* Print lint errors to stdout -- this is expected output from `standard-engine`.
* Note: When fixing code from stdin (`standard --stdin --fix`), the transformed
* code is printed to stdout, so print lint errors to stderr in this case.
*/
function log () {
if (argv.stdin && argv.fix) {
arguments[0] = opts.cmd + ': ' + arguments[0]
console.error.apply(console, arguments)
} else {
console.log.apply(console, arguments)
}
}
}