forked from standard/standard
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
195 lines (165 loc) · 4.94 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
module.exports.lintText = lintText
module.exports.lintFiles = lintFiles
var dezalgo = require('dezalgo')
var eslint = require('eslint')
var extend = require('xtend')
var findRoot = require('find-root')
var fs = require('fs')
var glob = require('glob')
var ignorePkg = require('ignore')
var os = require('os')
var parallel = require('run-parallel')
var path = require('path')
var pkgConfig = require('pkg-config')
var uniq = require('uniq')
var DEFAULT_PATTERNS = [
'**/*.js',
'**/*.jsx'
]
var DEFAULT_IGNORE_PATTERNS = [
'coverage/**',
'node_modules/**',
'**/*.min.js',
'**/bundle.js'
]
var DEFAULT_CONFIG = {
configFile: path.join(__dirname, 'rc', '.eslintrc'),
reset: true,
useEslintrc: false
}
/**
* Lint text to enforce JavaScript Standard Style.
*
* @param {string} text file text to lint
* @param {Object=} opts options object
* @param {string=} opts.parser custom js parser (e.g. babel-eslint, esprima-fb)
* @param {function(Error, Object)} cb callback
*/
function lintText (text, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
opts = parseOpts(opts)
cb = dezalgo(cb)
var result
try {
result = new eslint.CLIEngine(opts._config).executeOnText(text)
} catch (err) {
return cb(err)
}
return cb(null, result)
}
/**
* Lint files to enforce JavaScript Standard Style.
*
* @param {Array.<string>} files file globs to lint
* @param {Object=} opts options object
* @param {Array.<String>=} opts.ignore file globs to ignore (has sane defaults)
* @param {string=} opts.cwd current working directory (default: process.cwd())
* @param {string=} opts.parser custom js parser (e.g. babel-eslint, esprima-fb)
* @param {function(Error, Object)} cb callback
*/
function lintFiles (files, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
opts = parseOpts(opts)
cb = dezalgo(cb)
if (typeof files === 'string') files = [ files ]
if (files.length === 0) files = DEFAULT_PATTERNS
// traverse filesystem
parallel(files.map(function (pattern) {
return function (cb) {
glob(pattern, {
cwd: opts.cwd,
ignore: opts._ignore,
nodir: true
}, cb)
}
}), function (err, results) {
if (err) return cb(err)
// flatten nested arrays
var files = results.reduce(function (files, result) {
result.forEach(function (file) {
files.push(path.resolve(opts.cwd, file))
})
return files
}, [])
// de-dupe
files = uniq(files)
if (opts._gitignore) {
if (os.platform() === 'win32') files = files.map(toUnix)
files = toAbsolute(opts.cwd, opts._gitignore.filter(toRelative(opts.cwd, files)))
if (os.platform() === 'win32') files = files.map(toWin32)
}
// undocumented – do not use (used by bin/cmd.js)
if (opts._onFiles) opts._onFiles(files)
var result
try {
result = new eslint.CLIEngine(opts._config).executeOnFiles(files)
} catch (err) {
return cb(err)
}
return cb(null, result)
})
}
function parseOpts (opts) {
if (!opts) opts = {}
opts = extend(opts)
opts._config = extend(DEFAULT_CONFIG)
if (!opts.cwd) opts.cwd = process.cwd()
opts._ignore = DEFAULT_IGNORE_PATTERNS.slice(0) // passed into glob
opts._gitignore = ignorePkg()
function addIgnorePattern (patterns) {
opts._ignore = opts._ignore.concat(patterns)
opts._gitignore.addPattern(patterns)
}
if (opts.ignore) addIgnorePattern(opts.ignore)
if (opts.parser) useCustomParser(opts.parser)
// Find package.json in the project root
var root
try {
root = findRoot(opts.cwd)
} catch (e) {}
if (root) {
var packageOpts = pkgConfig('standard', { root: false, cwd: opts.cwd })
if (packageOpts) {
// Use ignore patterns from package.json
if (packageOpts.ignore) addIgnorePattern(packageOpts.ignore)
// Use custom js parser from package.json
if (!opts.parser && packageOpts.parser) useCustomParser(packageOpts.parser)
}
// Use ignore patterns from project root .gitignore
var gitignore
try {
gitignore = fs.readFileSync(path.join(root, '.gitignore'), 'utf8')
} catch (e) {}
if (gitignore) opts._gitignore.addPattern(gitignore.split(/\r?\n/))
}
function useCustomParser (parser) {
var configFile = JSON.parse(fs.readFileSync(DEFAULT_CONFIG.configFile, 'utf8'))
configFile.parser = parser
var tmpFilename = path.join(os.tmpdir(), '.eslintrc-' + parser)
fs.writeFileSync(tmpFilename, JSON.stringify(configFile))
opts._config.configFile = tmpFilename
}
return opts
}
function toAbsolute (cwd, files) {
return files.map(function (file) {
return path.join(cwd, file)
})
}
function toRelative (cwd, files) {
return files.map(function (file) {
return path.relative(cwd, file)
})
}
function toUnix (str) {
return str.replace(/\\/g, '/')
}
function toWin32 (str) {
return str.replace(/\//g, '\\')
}