-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.js
More file actions
69 lines (61 loc) · 1.71 KB
/
clean.js
File metadata and controls
69 lines (61 loc) · 1.71 KB
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
import { readdir, rm } from "fs/promises";
const owner = "DataFlowAnalysis";
const repo = "OnlineEditor";
const branches = await fetch(
`https://api.github.com/repos/${owner}/${repo}/branches`,
{
headers: {
Authorization: `Bearer ${process.env.TOKEN}`,
Accept: "application/vnd.github+json",
},
},
)
.then((res) => res.json())
.then((data) => data.map((b) => b.name));
const files = (await getFiles(".")).filter((p) => p.length > 0);
for (const file of files) {
let doDelete = false;
if (file.startsWith("branches/")) {
if (!branches.includes(file.substring(9, file.length - 1))) {
doDelete = true;
}
} else if (file.startsWith("prs/")) {
const prNumber = Number(file.substring(4, file.length - 1));
const response = await fetch(
`https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}`,
{
headers: {
Authorization: `Bearer ${process.env.TOKEN}`,
Accept: "application/vnd.github+json",
},
},
);
if (response.status === 404) {
doDelete = true;
continueM
}
const prData = await response.json();
if (prData.state !== "open") {
doDelete = true;
}
}
if (doDelete) {
console.log(`Deleting ${file}`);
await rm(file, { recursive: true });
}
}
async function getFiles(dir) {
const files = [];
async function step(_dir) {
const dirFiles = await readdir(_dir, { withFileTypes: true });
for (const file of dirFiles) {
if (file.isDirectory()) {
await step(`${_dir}/${file.name}`);
} else if (file.name.endsWith("index.html")) {
files.push(`${_dir}/`);
}
}
}
await step(dir);
return files.map((f) => f.substring(2));
}