-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-lint.js
58 lines (50 loc) · 1.52 KB
/
js-lint.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
'use strict';
const gulp = require('gulp');
const $ = require('gulp-load-plugins')();
const through2 = require('through2').obj;
const fs = require('fs');
const multipipe = require('multipipe');
// function isFixed(file) {
// return file.eslint != null && file.eslint.fixed; // has ESLint fixed the file contents?
// }
module.exports = function(options) {
return function() {
let eslintResults = {};
let cacheFilePath = options.cacheFilePath;
try {
eslintResults = JSON.parse(fs.readFileSync(cacheFilePath));
} catch (e) {
}
return gulp.src(options.src, {read: false})
.pipe($.if(
function(file) {
return eslintResults[file.path] && eslintResults[file.path].mtime == file.stat.mtime.toJSON();
},
through2(function(file, enc, callback) {
file.eslint = eslintResults[file.path].eslint;
callback(null, file);
}),
multipipe(
through2(function(file, enc, callback) {
file.contents = fs.readFileSync(file.path);
callback(null, file);
}),
$.eslint({ fix: true }), // https://stackoverflow.com/a/37108027/6263942
// how to use? $.eslint.format(),
// how to use? $.if(isFixed, gulp.dest(options.dest)),
$.eslint.failAfterError(),
through2(function(file, enc, callback) {
eslintResults[file.path] = {
eslint: file.eslint,
mtime: file.stat.mtime,
};
callback(null, file);
})
)
))
.on('end', function() {
fs.writeFileSync(cacheFilePath, JSON.stringify((eslintResults)));
})
.pipe($.eslint.format());
};
};