UNPKG

2.8 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3var fs = require('fs')
4var minimist = require('minimist')
5var standard = require('../')
6var standardFormat = require('standard-format')
7var stdin = require('get-stdin')
8
9var 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// running `standard -` is equivalent to `standard --stdin`
25if (argv._[0] === '-') {
26 argv.stdin = true
27 argv._.shift()
28}
29
30if (argv.help) {
31 console.log(function () {
32 /*
33 standard - Uber JavaScript Standard Style
34
35 Usage:
36 standard <flags> [FILES...]
37
38 If FILES is omitted, then all JavaScript source files (*.js, *.jsx) in the current
39 working directory are checked, recursively.
40
41 Certain paths (node_modules/, .git/, coverage/, *.min.js, bundle.js) are
42 automatically excluded.
43
44 Flags:
45 -F --format Automatically format code. (using standard-format)
46 -v, --verbose Show error codes. (so you can ignore specific rules)
47 --stdin Read file text from stdin.
48 --version Show current version.
49 -h, --help Show usage information.
50
51 Readme: https://github.com/uber/standard
52 Report bugs: https://github.com/uber/standard/issues
53
54 */
55 }.toString().split(/\n/).slice(2, -2).join('\n'))
56 process.exit(0)
57}
58
59if (argv.version) {
60 console.log(require('../package.json').version)
61 process.exit(0)
62}
63
64if (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
85function 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
107function 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}