-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathgulp-prettier-eslint.js
42 lines (36 loc) · 1.25 KB
/
gulp-prettier-eslint.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
'use strict'
const { obj: map } = require('through2')
const PluginError = require('plugin-error')
const prettierEslint = require('prettier-eslint')
module.exports = () => {
const report = { changed: 0, unchanged: 0 }
return map(format).on('finish', () => {
if (report.changed > 0) {
const changed = 'formatted '
.concat(report.changed)
.concat(' file')
.concat(report.changed === 1 ? '' : 's')
const unchanged = 'left '
.concat(report.unchanged)
.concat(' file')
.concat(report.unchanged === 1 ? '' : 's')
.concat(' unchanged')
console.log(`prettier-eslint: ${changed}; ${unchanged}`)
} else {
console.log(`prettier-eslint: left ${report.unchanged} file${report.unchanged === 1 ? '' : 's'} unchanged`)
}
})
function format (file, enc, next) {
if (file.isNull()) return next()
if (file.isStream()) return next(new PluginError('gulp-prettier-eslint', 'Streaming not supported'))
const input = file.contents.toString()
const output = prettierEslint({ text: input, filePath: file.path })
if (input === output) {
report.unchanged += 1
} else {
report.changed += 1
file.contents = Buffer.from(output)
}
next(null, file)
}
}