-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.ts
122 lines (87 loc) · 4.43 KB
/
update.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
118
119
120
121
122
import { writeAll } from "$std/streams/write_all.ts"
import { exists } from "$std/fs/mod.ts"
import { decompress } from "zip"
const updateDirectory = './update/'
const backupDirectory = './backup/'
const checkRemoteVersion = async () => {
const repoUrl = await fetch("https://raw.githubusercontent.com/ovniroto/stream-bot/main/version")
const remoteVersion = await repoUrl.json()
return remoteVersion.version
}
const checkAppVersion = async () => {
const decoder = new TextDecoder("utf-8")
const fileData = await Deno.readFile("./version")
const appVersion = JSON.parse(decoder.decode(fileData))
return appVersion.version
}
const downloadVersion = async (version: string) => {
const response = await fetch(`https://github.com/ovniroto/stream-bot/archive/refs/tags/v${version}.zip`)
const blob = await response.blob()
const buf = await blob.arrayBuffer()
const data = new Uint8Array(buf)
const updateDirectoryExists = await exists(updateDirectory)
if(!updateDirectoryExists) await Deno.mkdir(updateDirectory)
const file = await Deno.create(updateDirectory + `StreamBot_v${version}.zip`)
await writeAll(file, data)
Deno.close(file.rid)
console.log(`[StreamBot] Download completed!`)
}
const unzipVersion = async (version: string) => {
const file = updateDirectory + "StreamBot_v" + version + ".zip"
return await decompress(file, updateDirectory)
}
const moveVersion = async (version: string) => {
const newVersionDirectory = updateDirectory + "stream-bot-" + version + "/"
await Deno.rename(newVersionDirectory + "src", "./src")
await Deno.rename(newVersionDirectory + ".gitignore", "./.gitignore")
await Deno.rename(newVersionDirectory + "deno.json", "./deno.json")
await Deno.rename(newVersionDirectory + "version", "./version")
await Deno.rename(newVersionDirectory + "update.ts", "./update.ts")
await Deno.rename(newVersionDirectory + "twind.config.ts", "./twind.config.ts")
await Deno.rename(newVersionDirectory + "StreamBot_Start.bat", "./StreamBot_Start.bat")
await Deno.rename(newVersionDirectory + "StreamBot_Update.bat", "./StreamBot_Update.bat")
await Deno.rename(newVersionDirectory + "LICENSE", "./LICENSE")
await Deno.rename(newVersionDirectory + "README.md", "./README.md")
await Deno.rename(newVersionDirectory + "CHANGELOG.md", "./CHANGELOG.md")
}
const backupFiles = async () => {
const backupDirectoryExists = await exists(backupDirectory)
if(!backupDirectoryExists) await Deno.mkdir(backupDirectory)
await Deno.rename("./src", backupDirectory + "src")
await Deno.rename("./.gitignore", backupDirectory + ".gitignore")
await Deno.rename("./deno.json", backupDirectory + "deno.json")
await Deno.rename("./version", backupDirectory + "version")
await Deno.rename("./update.ts", backupDirectory + "update.ts")
await Deno.rename("./twind.config.ts", backupDirectory + "twind.config.ts")
await Deno.rename("./StreamBot_Start.bat", backupDirectory + "StreamBot_Start.bat")
await Deno.rename("./StreamBot_Update.bat", backupDirectory + "StreamBot_Update.bat")
await Deno.rename("./README.md", backupDirectory + "README.md")
await Deno.rename("./CHANGELOG.md", backupDirectory + "CHANGELOG.md")
await Deno.rename("./LICENSE", backupDirectory + "LICENSE")
console.log(`[StreamBot] Backup completed!`)
}
const cleanInstall = async () => {
await Deno.remove(updateDirectory, { recursive: true })
await Deno.remove(backupDirectory, { recursive: true })
}
const appUpdate = async () => {
console.log(`[StreamBot] Checking new version...`)
const remoteVersion = await checkRemoteVersion()
const appVersion = await checkAppVersion()
if(remoteVersion !== appVersion) {
console.log(`[StreamBot] New version ${remoteVersion} found! Downloading...`)
await downloadVersion(remoteVersion)
console.log(`[StreamBot] Backup files...`)
await backupFiles()
console.log(`[StreamBot] Installing new version...`)
const unzip = await unzipVersion(remoteVersion)
if(!unzip) return console.log(`[StreamBot] [ERROR:UNZIP] An error has occurred when decompressing files`)
await moveVersion(remoteVersion)
console.log(`[StreamBot] Clean old files...`)
await cleanInstall()
console.log(`[StreamBot] New version ${remoteVersion} installed!`)
return
}
console.log(`[StreamBot] No new version detected.`)
}
appUpdate()