-
Notifications
You must be signed in to change notification settings - Fork 59
/
index.js
61 lines (50 loc) · 1.54 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
const deglob = require('deglob')
const fs = require('fs')
const formatter = require('esformatter')
const ESFORMATTER_CONFIG = require('./rc/esformatter.json')
const DEFAULT_IGNORE = [
'node_modules/**',
'.git/**',
'**/*.min.js',
'**/bundle.js'
]
const MULTI_NEWLINE_N = /((?:\n){3,})/g
const MULTI_NEWLINE_RN = /((?:\r\n){3,})/g
const EOL_SEMICOLON = /;(?=\r?\n)/g
const EOL_SEMICOLON_WITH_COMMENT = /;(?=\s*\/[/*][\s\w*/]*\r?\n)/g
const SOF_NEWLINES = /^(\r?\n)+/g
module.exports.transform = function (file) {
file = file
.replace(MULTI_NEWLINE_N, '\n\n')
.replace(MULTI_NEWLINE_RN, '\r\n\r\n')
.replace(EOL_SEMICOLON, '')
.replace(EOL_SEMICOLON_WITH_COMMENT, '')
.replace(SOF_NEWLINES, '')
const formatted = formatter.format(file, ESFORMATTER_CONFIG)
.replace(EOL_SEMICOLON, '')
// run replace again; esformatter-semicolon-first will re-add semicolon at EOL
return formatted
}
module.exports.load = function (opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
if (!opts) opts = {}
let ignore = [].concat(DEFAULT_IGNORE) // globs to ignore
if (opts.ignore) ignore = ignore.concat(opts.ignore)
const deglobOpts = {
ignore,
cwd: opts.cwd || process.cwd(),
useGitIgnore: true,
usePackageJson: true,
configKey: 'standard'
}
deglob(['**/*.js', '**/*.jsx'], deglobOpts, function (err, files) {
if (err) return cb(err)
files = files.map(function (f) {
return { name: f, data: fs.readFileSync(f).toString() } // assume utf8
})
cb(null, files)
})
}