1 | #!/usr/bin/env node
|
2 |
|
3 | var fs = require('fs')
|
4 | var minimist = require('minimist')
|
5 | var standard = require('../')
|
6 | var standardFormat = require('standard-format')
|
7 | var stdin = require('get-stdin')
|
8 |
|
9 | var argv = minimist(process.argv.slice(2), {
|
10 | alias: {
|
11 | format: 'F',
|
12 | help: 'h',
|
13 | verbose: 'v'
|
14 | },
|
15 | boolean: [
|
16 | 'format',
|
17 | 'help',
|
18 | 'stdin',
|
19 | 'verbose',
|
20 | 'version'
|
21 | ]
|
22 | })
|
23 |
|
24 |
|
25 | if (argv._[0] === '-') {
|
26 | argv.stdin = true
|
27 | argv._.shift()
|
28 | }
|
29 |
|
30 | if (argv.help) {
|
31 | console.log(function () {
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | }.toString().split(/\n/).slice(2, -2).join('\n'))
|
56 | process.exit(0)
|
57 | }
|
58 |
|
59 | if (argv.version) {
|
60 | console.log(require('../package.json').version)
|
61 | process.exit(0)
|
62 | }
|
63 |
|
64 | if (argv.stdin) {
|
65 | stdin(function (text) {
|
66 | if (argv.format) {
|
67 | text = standardFormat.transform(text)
|
68 | process.stdout.write(text)
|
69 | }
|
70 | standard.lintText(text, onResult)
|
71 | })
|
72 | } else {
|
73 | var lintOpts = {}
|
74 | if (argv.format) {
|
75 | lintOpts._onFiles = function (files) {
|
76 | files.forEach(function (file) {
|
77 | var data = fs.readFileSync(file).toString()
|
78 | fs.writeFileSync(file, standardFormat.transform(data))
|
79 | })
|
80 | }
|
81 | }
|
82 | standard.lintFiles(argv._, lintOpts, onResult)
|
83 | }
|
84 |
|
85 | function onResult (err, result) {
|
86 | if (err) return error(err)
|
87 | if (result.errorCount === 0) process.exit(0)
|
88 |
|
89 | console.error(
|
90 | 'Error: Use Uber JavaScript Standard Style ' +
|
91 | '(https://github.com/uber/standard)'
|
92 | )
|
93 |
|
94 | result.results.forEach(function (result) {
|
95 | result.messages.forEach(function (message) {
|
96 | console.error(
|
97 | ' %s:%d:%d: %s%s',
|
98 | result.filePath, message.line || 0, message.column || 0, message.message,
|
99 | argv.verbose ? ' (' + message.ruleId + ')' : ''
|
100 | )
|
101 | })
|
102 | })
|
103 |
|
104 | process.exit(1)
|
105 | }
|
106 |
|
107 | function error (err) {
|
108 | console.error('Unexpected Linter Output:\n')
|
109 | console.error(err.stack || err.message || err)
|
110 | console.error(
|
111 | '\nIf you think this is a bug in `standard`, open an issue: ' +
|
112 | 'https://github.com/uber/standard'
|
113 | )
|
114 | process.exit(1)
|
115 | }
|