This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean.js
59 lines (51 loc) · 1.92 KB
/
clean.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
const fs = require("fs");
const path = require("path");
let ignored;
/**
* @param {string} file name
* @param {Array} files in directory
* @returns {boolean} is compiled or not
*/
function isCompiledFile(file, files) {
let file_sub1 = file.substr(0, file.lastIndexOf("."));
let file_sub2 = file.substr(0, file_sub1.lastIndexOf("."));
let file_sub3 = file.substr(0, file_sub2.lastIndexOf("."));
return (
(file.endsWith(".d.ts.map") && files.includes(`${file_sub3}.ts`)) ||
(file.endsWith(".d.ts") && files.includes(`${file_sub2}.ts`)) ||
(file.endsWith(".js.map") && files.includes(`${file_sub2}.ts`)) ||
(file.endsWith(".js") && files.includes(`${file_sub1}.ts`)) ||
(file.endsWith(".css.map") && files.includes(`${file_sub2}.scss`)) ||
(file.endsWith(".css") && files.includes(`${file_sub1}.scss`))
);
}
/**
* @param {Array} source to target
* @returns {void}
*/
async function clean(source = []) {
let items = await fs.promises.readdir(path.join(...source));
items = items.filter(item => {
return !item.startsWith(".");
});
if (typeof ignored === "undefined") {
try {
// eslint-disable-next-line require-atomic-updates
ignored = await fs.promises.readFile(path.join(...source, ".gitignore"), "utf-8");
ignored = ignored.split("\n").map(ignore => ignore.trim());
} catch (_1) {
ignored = [];
}
}
for (let index = 0; index < items.length; index += 1) {
let item_path = path.join(...source.concat(items[index]));
let item_stats = await fs.promises.stat(item_path);
if (item_stats.isDirectory() && !ignored.includes(items[index])) {
await clean(source.concat(items[index]));
}
if (item_stats.isFile() && isCompiledFile(items[index], items)) {
await fs.promises.unlink(item_path);
}
}
}
clean([__dirname]);