-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy.ts
117 lines (96 loc) · 3.02 KB
/
deploy.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
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
import { execSync } from "child_process"
import * as app from "#app"
import restart from "#tables/restart.ts"
type State = "waiting" | "running" | "done" | "error"
type Command = { cmd: string; state: State; time: number }
export default new app.Command({
name: "deploy",
description: "Deploy Lab Tool",
channelType: "all",
botOwnerOnly: true,
cooldown: {
duration: 10000,
type: app.CooldownType.Global,
},
async run(message) {
message.triggerCooldown()
const commands: Command[] = [
{ state: "waiting", time: 0, cmd: "git reset --hard" },
{ state: "waiting", time: 0, cmd: "git pull" },
{ state: "waiting", time: 0, cmd: "npm install" },
{ state: "waiting", time: 0, cmd: "npm run build" },
{ state: "waiting", time: 0, cmd: "pm2 restart tool" },
]
const format = (command: Command) =>
`${app.emote(
message,
(
{
waiting: "Minus",
running: "Loading",
done: "CheckMark",
error: "Cross",
} as const
)[command.state],
)} ${command.state === "running" ? "**" : ""}\`>_ ${command.cmd}\`${
command.state === "running" ? "**" : ""
} ${command.time ? `(**${command.time}** ms)` : ""}`.trim()
const makeView = (finish?: boolean, errored?: boolean) =>
`${commands
.map((command) =>
format({ ...command, state: finish ? "done" : command.state }),
)
.join(
"\n",
)}\n${app.emote(message, finish ? "CheckMark" : errored ? "Cross" : "Loading")} ${
finish ? `**Deployed** 🚀` : errored ? "Errored" : "Deploying..."
}`
const run = async (command: Command) => {
command.state = "running"
await view.edit(makeView())
try {
execSync(command.cmd, { cwd: process.cwd() })
} catch (error: any) {
command.state = "error"
await view.edit(makeView(false, true))
throw error
}
command.state = "done"
}
const view = await message.channel.send(makeView())
const created_at = new Date().toISOString()
await restart.query.insert({
content: makeView(true),
last_channel_id: message.channel.id,
last_message_id: view.id,
created_at,
})
try {
for (const command of commands) {
const time = Date.now()
await run(command)
command.time = Date.now() - time
}
} catch (error: any) {
await restart.query.delete().where({ created_at })
app.error(error)
return view.edit({
embeds: [
new app.EmbedBuilder()
.setTitle("\\❌ An error has occurred.")
.setColor("Red")
.setDescription(
await app.code.stringify({
content: (error?.stack ?? error?.message ?? String(error))
.split("")
.reverse()
.slice(0, 2000)
.reverse()
.join(""),
}),
),
],
})
}
},
})