-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (110 loc) · 3.17 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
module.exports.lintText = lintText
module.exports.lintFiles = lintFiles
var dezalgo = require('dezalgo')
var eslint = require('eslint')
var findRoot = require('find-root')
var glob = require('glob')
var parallel = require('run-parallel')
var path = require('path')
var uniq = require('uniq')
var DEFAULT_PATTERNS = [
'**/*.js',
'**/*.jsx'
]
var DEFAULT_IGNORE_PATTERNS = [
'**/node_modules/**',
'.git/**',
'coverage/**',
'**/*.min.js',
'**/bundle.js'
]
var ESLINT_CONFIG = {
configFile: path.join(__dirname, 'rc', '.eslintrc'),
useEslintrc: false
}
/**
* Lint text to enforce JavaScript Standard Style.
*
* @param {string} text file text 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 {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(ESLINT_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 {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.join(opts.cwd, file))
})
return files
}, [])
// de-dupe
files = uniq(files)
// undocumented – do not use (used by bin/cmd.js)
if (opts._onFiles) opts._onFiles(files)
var result
try {
result = new eslint.CLIEngine(ESLINT_CONFIG).executeOnFiles(files)
} catch (err) {
return cb(err)
}
return cb(null, result)
})
}
function parseOpts (opts) {
if (!opts) opts = {}
if (!opts.cwd) opts.cwd = process.cwd()
// Add user ignore patterns to default ignore patterns
opts.ignore = (opts.ignore || []).concat(DEFAULT_IGNORE_PATTERNS)
// Add additional ignore patterns from the closest `package.json`
try {
var root = findRoot(opts.cwd)
var packageOpts = require(path.join(root, 'package.json')).standard
if (packageOpts) opts.ignore = opts.ignore.concat(packageOpts.ignore)
} catch (e) {}
return opts
}