-
Notifications
You must be signed in to change notification settings - Fork 44
/
index.js
98 lines (79 loc) · 2.22 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
const globby = require('globby')
const path = require('path')
const fs = require('fs')
const prettierx = require('prettierx')
const { createIgnorer, isSupportedExtension, getOptions } = require('./utils')
function format (source, options) {
return prettierx.format(source, getOptions(options))
}
function check (source, options) {
return prettierx.check(source, getOptions(options))
}
function getFileInfo (filePath, options) {
return prettierx.getFileInfo(filePath, getOptions(options))
}
function formatWithCursor (source, options) {
return prettierx.formatWithCursor(source, getOptions(options))
}
function run (cwd, config) {
const onProcessed = config.onProcessed || function () {}
const patterns = config.patterns.concat([
'!**/node_modules/**',
'!./node_modules/**',
'!**/.{git,svn,hg}/**',
'!./.{git,svn,hg}/**'
])
let filePaths
try {
filePaths = globby
.sync(patterns, { dot: true, nodir: true })
.map(filePath => path.relative(process.cwd(), filePath))
} catch (error) {
console.error(`Unable to expand glob pattern: ${error.message}`)
}
const ignorer = createIgnorer(cwd)
const files = filePaths.filter(isSupportedExtension).filter(ignorer)
for (const relative of files) {
const start = Date.now()
const file = path.join(cwd, relative)
const input = fs.readFileSync(file, 'utf8')
const fileOptions = Object.assign(
{},
prettierx.resolveConfig.sync(file, {
editorconfig: true
}),
config.options
)
if (config.check) {
const formatted = check(input, fileOptions)
onProcessed({
file: relative,
runtime: Date.now() - start,
formatted,
check: config.check
})
continue
}
const output = format(input, fileOptions)
const formatted = output === input
if (!formatted) {
fs.writeFileSync(file, output)
}
onProcessed({
file: relative,
runtime: Date.now() - start,
formatted,
check: config.check
})
}
}
module.exports = {
format,
check,
run,
getFileInfo,
formatWithCursor,
resolveConfig: prettierx.resolveConfig,
clearConfigCache: prettierx.clearConfigCache,
getSupportInfo: prettierx.getSupportInfo
}