-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternals.ts
84 lines (78 loc) · 2.48 KB
/
internals.ts
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
import { execSync } from "child_process";
import { INTERNAL_USERS } from "./constants";
function deleteRepo(repo: string) {
try {
execSync(`rm -rf ${repo}`, { stdio: "inherit" });
console.info(`🚮 Deleted repo ${repo}`);
} catch (error) {
console.error(`❌ Failed to delete repo ${repo}:`, error);
}
}
function checkoutRepoPR(repo: string, ref: string, tempDir: string) {
try {
execSync(`git clone git@github.com:${repo}.git ${tempDir}`, {
stdio: "inherit",
});
execSync(`( cd ${tempDir} ; git checkout ${ref} )`, {
stdio: "inherit",
});
console.info(`☑️ Checked out PR #${repo}/${ref} in ${tempDir}`);
} catch (error) {
console.error(
`❌ Failed to check out PR #${repo}/${ref} in ${tempDir}:`,
error
);
throw error;
}
}
function runPrettier(tempDir: string) {
try {
execSync(`( cd ${tempDir} ; bun prettier --write . )`, {
stdio: "inherit",
});
console.info("📝 Prettier has formatted the files.");
} catch (error) {
console.error("❌ Failed to run Prettier:", error);
}
}
function commitAndPushChanges(tempDir: string, user: string) {
try {
// Check if there are any changes to commit
const changes = execSync(
`( cd ${tempDir} ; git status --porcelain )`
).toString();
if (changes) {
console.info(
"🔂 Changes detected, adding, committing and pushing..."
);
execSync(`( cd ${tempDir} ; git add . )`, { stdio: "inherit" });
execSync(`( cd ${tempDir} ; git config --list )`, {
stdio: "inherit",
});
execSync(
`( cd ${tempDir} ; git commit -m "ci: formatting applied [on behalf of ${user}]" )`,
{
stdio: "inherit",
}
);
execSync(`( cd ${tempDir} ; git push )`, { stdio: "inherit" });
} else {
console.info("🕊️ No changes to commit.");
}
} catch (error) {
console.error("❌ Failed to commit and push changes:", error);
}
}
function getTargetRepo(user: string): string {
if (INTERNAL_USERS.includes(user.toLowerCase())) {
return `systemphil/sphil`;
}
return `${user}/sphil`;
}
export {
deleteRepo,
checkoutRepoPR,
runPrettier,
commitAndPushChanges,
getTargetRepo,
};