1 | #!/usr/bin/env node
|
2 |
|
3 | var commondir = require('commondir')
|
4 | var editorConfigGetIndent = require('editorconfig-get-indent')
|
5 | var fs = require('fs')
|
6 | var minimist = require('minimist')
|
7 | var standard = require('../')
|
8 | var standardFormat = require('uber-standard-format')
|
9 | var stdin = require('get-stdin')
|
10 | var standardReporter = require('standard-reporter')
|
11 | var fmt = require('util').format
|
12 | var reporter
|
13 |
|
14 | var argv = minimist(process.argv.slice(2), {
|
15 | alias: {
|
16 | format: 'F',
|
17 | help: 'h',
|
18 | verbose: 'v'
|
19 | },
|
20 | boolean: [
|
21 | 'format',
|
22 | 'help',
|
23 | 'stdin',
|
24 | 'verbose',
|
25 | 'version'
|
26 | ]
|
27 | })
|
28 |
|
29 |
|
30 | if (argv._[0] === '-') {
|
31 | argv.stdin = true
|
32 | argv._.shift()
|
33 | }
|
34 |
|
35 | if (argv.help) {
|
36 | console.log(function () {
|
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 | }.toString().split(/\n/).slice(2, -2).join('\n'))
|
63 | process.exit(0)
|
64 | }
|
65 |
|
66 | if (argv.version) {
|
67 | console.log(require('../package.json').version)
|
68 | process.exit(0)
|
69 | }
|
70 |
|
71 | if (argv.reporter) {
|
72 | reporter = standardReporter({
|
73 | type: argv.reporter,
|
74 | sink: process.stdout,
|
75 | colors: argv.colors || true
|
76 | })
|
77 | }
|
78 |
|
79 | if (argv.stdin) {
|
80 | editorConfigGetIndent(process.cwd(), function (err, indent) {
|
81 | if (err) return onError(err)
|
82 | stdin(function (text) {
|
83 | if (argv.format) {
|
84 | text = standardFormat.transform(text, indent)
|
85 | process.stdout.write(text)
|
86 | }
|
87 | standard.lintText(text, onResult)
|
88 | })
|
89 | })
|
90 | } else {
|
91 | var lintOpts = {}
|
92 | if (argv.format) {
|
93 | lintOpts._onFiles = function (files) {
|
94 | editorConfigGetIndent(commondir(files), function (err, indent) {
|
95 | if (err) return onError(err)
|
96 | files.forEach(function (file) {
|
97 | var data = fs.readFileSync(file).toString()
|
98 | fs.writeFileSync(file, standardFormat.transform(data, indent))
|
99 | })
|
100 | })
|
101 | }
|
102 | }
|
103 | standard.lintFiles(argv._, lintOpts, onResult)
|
104 | }
|
105 |
|
106 | function onResult (err, result) {
|
107 | if (err) return onError(err)
|
108 | if (result.errorCount === 0) process.exit(0)
|
109 |
|
110 | console.error(
|
111 | 'Error: Use Uber JavaScript Standard Style ' +
|
112 | '(https://github.com/uber/standard)'
|
113 | )
|
114 |
|
115 | result.results.forEach(function (result) {
|
116 | result.messages.forEach(function (message) {
|
117 | log(
|
118 | ' %s:%d:%d: %s %s',
|
119 | result.filePath, message.line || 0, message.column || 0, message.message,
|
120 | '(' + message.ruleId + ')'
|
121 | )
|
122 | })
|
123 | })
|
124 |
|
125 | if (reporter) {
|
126 | reporter.end()
|
127 | }
|
128 |
|
129 | process.on('exit', function () {
|
130 | process.exit(1)
|
131 | })
|
132 | }
|
133 |
|
134 | function onError (err) {
|
135 | console.error('standard: Unexpected linter output:\n')
|
136 | console.error(err.stack || err.message || err)
|
137 | console.error(
|
138 | '\nIf you think this is a bug in `standard`, open an issue: ' +
|
139 | 'https://github.com/uber/standard'
|
140 | )
|
141 | process.exit(1)
|
142 | }
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | function log () {
|
150 | if (reporter) {
|
151 | reporter.write(fmt.apply(null, arguments) + '\n')
|
152 | } else if (argv.stdin && argv.format) {
|
153 | arguments[0] = 'standard: ' + arguments[0]
|
154 | console.error.apply(console, arguments)
|
155 | } else {
|
156 | console.log.apply(console, arguments)
|
157 | }
|
158 | }
|