-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcli.js
executable file
·94 lines (80 loc) · 2.43 KB
/
cli.js
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
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const globby = require('globby')
const path = require('path')
const program = require('commander')
const standardMarkdown = require('./')
let patterns = [
'**/*.md',
'**/*.markdown',
'!**/.git/**',
'!**/coverage/**',
'!**/dist/**',
'!**/node_modules/**',
'!**/vendor/**',
'!*.min.js',
'!bundle.js'
]
let cwd
program
.version(require('./package.json').version)
.arguments('[cwd] [patterns...]')
.option('-f, --fix', 'Attempt to fix basic standard JS issues')
.option('-v, --verbose', 'Verbose mode')
.action(function (cwdValue, patternArgs) {
if (cwdValue == null) return
// If cwd is an actual path, set it to be the cwd
// Otherwise interpret it as a glob pattern
if (fs.existsSync(path.resolve(cwdValue)) && fs.lstatSync(path.resolve(cwdValue)).isDirectory()) {
cwd = cwdValue
} else {
if (cwdValue) {
patterns = [cwdValue].concat(patternArgs).concat(patterns.slice(2))
}
}
})
.parse(process.argv)
cwd = cwd || process.cwd()
// The files to run our command against
const files = globby.sync(patterns, { cwd: cwd }).map(function (file) {
return path.resolve(cwd, file)
})
let afterLint = function () {}
// Auto fix the files first if we were told to
if (program.fix) {
afterLint = function (result) {
if (result.input !== result.output) {
console.log('File has changed: ' + result.file)
}
fs.writeFileSync(result.file, result.output)
}
}
// Lint the files
standardMarkdown[program.fix ? 'formatFiles' : 'lintFiles'](files, function (err, results) {
if (err) throw err
// No errors
if (results.every(function (result) { return result.errors.length === 0 })) {
process.exit(0)
}
let lastFilePath
let totalErrors = 0
function pad (width, string, padding) {
return (width <= string.length) ? string : pad(width, string + padding, padding)
}
results.forEach(afterLint)
// Errors!
results.forEach(function (result) {
totalErrors += result.errors.length
result.errors.forEach(function (error) {
const filepath = path.relative(cwd, result.file)
if (filepath !== lastFilePath) {
console.log('\n ' + filepath)
}
lastFilePath = filepath
console.log(' ' + pad(10, error.line + ':' + error.column + ': ', ' ') + error.message)
})
})
console.log('\nThere are ' + totalErrors + ' errors in "' + cwd + '"')
process.exit(1)
})