-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
119 lines (108 loc) · 3.14 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
const prettier = require("prettier");
var format = require('prettier-eslint')
const fs = require("fs");
var path = require('path')
const PRETTIER_ESLINT_PLUGIN = 'PrettierEslintPlugin';
const DEFAULT_ENCODING = "utf-8";
const DEFAULT_EXTENSIONS = prettier.getSupportInfo
? prettier
.getSupportInfo()
.languages.map(l => l.extensions)
.reduce((accumulator, currentValue) => accumulator.concat(currentValue))
: [
".css",
".graphql",
".js",
".json",
".jsx",
".less",
".sass",
".scss",
".ts",
".tsx",
".vue",
".yaml",
];
function processFilePath({ fileCurrent, encoding, filePath, eslintConfig, prettierOptions, logLevel, eslintPath, prettierPath }) {
return new Promise((resolve, reject) => {
fs.readFile(fileCurrent, encoding, (err, source) => {
if (err) {
return reject(err);
}
try{
const fmtOptions = {
text: source,
filePath, eslintConfig, prettierOptions, logLevel, eslintPath, prettierPath
}
const formatted = format(fmtOptions)
if (formatted !== source) {
fs.writeFile(fileCurrent, prettierSource, this.encoding, err => {
if (err) {
return reject(err);
}
resolve('success!');
});
} else {
resolve('success!');
}
}
catch(err){
return reject(err);
}
});
})
}
class PrettierEslintPlugin {
constructor (
{
encoding = DEFAULT_ENCODING,
extensions = DEFAULT_EXTENSIONS,
// prettier-eslint API
filePath, eslintConfig, prettierOptions, logLevel, eslintPath,
prettierPath, sillyLogs, config
} = {}
) {
// Encoding to use when reading / writing files
this.encoding = encoding
// Only process these files
this.extensions = extensions
// Expose prettier-eslint API
this.filePath = filePath
this.eslintConfig = eslintConfig || config
this.prettierOptions = prettierOptions
this.logLevel = logLevel
this.eslintPath = eslintPath
this.prettierPath = prettierPath
}
apply (compiler) {
compiler.hooks.emit.tapAsync(PRETTIER_ESLINT_PLUGIN, (compilation, callback) => {
const promises = [];
if (!compilation.fileDependencies) return;
compilation.fileDependencies.forEach(fileCurrent=> {
if (this.extensions.indexOf(path.extname(fileCurrent)) === -1 &&
fileCurrent.indexOf('node_modules') === -1 ) {
return;
}
promises.push(
processFilePath({
fileCurrent: fileCurrent,
encoding: this.encoding,
filePath : this.filePath,
eslintConfig : this.eslintConfig,
prettierOptions : this.prettierOptions,
logLevel : this.logLevel,
eslintPath : this.eslintPath,
prettierPath : this.prettierPath,
})
);
});
Promise.all(promises).then(() => {
callback();
}).catch(err => {
callback(err);
});
});
}
}
exports.processFilePath = processFilePath
exports.PrettierEslintPlugin = PrettierEslintPlugin