UNPKG

4.16 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3var commondir = require('commondir')
4var editorConfigGetIndent = require('editorconfig-get-indent')
5var fs = require('fs')
6var minimist = require('minimist')
7var standard = require('../')
8var standardFormat = require('uber-standard-format')
9var stdin = require('get-stdin')
10var standardReporter = require('standard-reporter')
11var fmt = require('util').format
12var reporter
13
14var 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// running `standard -` is equivalent to `standard --stdin`
30if (argv._[0] === '-') {
31 argv.stdin = true
32 argv._.shift()
33}
34
35if (argv.help) {
36 console.log(function () {
37 /*
38 standard - Uber JavaScript Standard Style
39
40 Usage:
41 standard <flags> [FILES...]
42
43 If FILES is omitted, then all JavaScript source files (*.js, *.jsx) in the current
44 working directory are checked, recursively.
45
46 Certain paths (node_modules/, .git/, coverage/, *.min.js, bundle.js) are
47 automatically excluded.
48
49 Flags:
50 -F --format Automatically format code. (using standard-format)
51 -v, --verbose Show error codes. (so you can ignore specific rules)
52 --stdin Read file text from stdin.
53 --version Show current version.
54 -h, --help Show usage information.
55 -r, --reporter Specify a reporter type. options: json, checkstyle or stylish
56 --no-colors No colored output on terminal for stylish reporter
57
58 Readme: https://github.com/uber/standard
59 Report bugs: https://github.com/uber/standard/issues
60
61 */
62 }.toString().split(/\n/).slice(2, -2).join('\n'))
63 process.exit(0)
64}
65
66if (argv.version) {
67 console.log(require('../package.json').version)
68 process.exit(0)
69}
70
71if (argv.reporter) {
72 reporter = standardReporter({
73 type: argv.reporter,
74 sink: process.stdout,
75 colors: argv.colors || true
76 })
77}
78
79if (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
106function 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
134function 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 * Print lint errors to stdout since this is expected output from `standard`.
146 * Note: When formatting code from stdin (`standard --stdin --format`), the transformed
147 * code is printed to stdout, so print lint errors to stderr in this case.
148 */
149function 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}