/
transform.js
49 lines (42 loc) · 1.11 KB
/
transform.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
const { notify } = require("wsk");
const { format, check, getSupportInfo } = require("prettier");
const { read, write } = require("../../utils/file");
const { extension } = require("../../utils/path");
const extensions = getSupportInfo().languages.reduce(
(extensions, language) => extensions.concat(language.extensions || []),
[]
);
function isSupported(path) {
return extensions.includes(extension(path));
}
async function onEvent(event, path, options = {}) {
try {
const source = await read(path);
const options = { filepath: path };
if (isSupported(path)) {
await write(path, format(source, options));
if (typeof options.onWrite === "function") {
await options.onWrite(path);
}
notify({
message: "Linting succeeded",
value: path,
display: "success"
});
} else {
notify({
message: "Linting skipped",
value: path,
display: ["gray"]
});
}
} catch (err) {
notify({
message: "Linting failed",
value: path,
display: "error",
error: err
});
}
}
module.exports = { onEvent };