9
9
10
10
const BASE_URL_VERSION = '/repos/VueTorrent/VueTorrent/contents/version.txt?ref='
11
11
const BASE_URL_ZIPBALL = '/repos/VueTorrent/VueTorrent/zipball/'
12
+ const BASE_URL_RELEASE = 'https://github.com/VueTorrent/VueTorrent/releases/download/$TAG/vuetorrent.zip'
12
13
const STABLE_BRANCH_NAME = 'latest-release'
13
14
const DEV_BRANCH_NAME = 'nightly-release'
14
15
const VERSION_PATTERN = / ^ v ? (?< version > [ 0 - 9 . ] + ) ( - (?< commits > \d + ) - g (?< sha > [ 0 - 9 a - f ] + ) ) ? $ /
@@ -24,9 +25,16 @@ const githubClient = axios.create({
24
25
headers : {
25
26
Accept : 'application/vnd.github.v3+json' ,
26
27
Authorization : process . env . GITHUB_AUTH ? `Bearer ${ process . env . GITHUB_AUTH } ` : undefined ,
28
+ 'User-Agent' : `vuetorrent/vuetorrent-backend`
27
29
}
28
30
} )
29
31
32
+ /** @typedef {{ version: string; commits?: string; sha?: string } } VersionType */
33
+
34
+ /**
35
+ * @param version {string}
36
+ * @returns {VersionType | undefined }
37
+ */
30
38
function extractVersion ( version ) {
31
39
const match = version . match ( VERSION_PATTERN )
32
40
return match ?. groups
@@ -89,13 +97,13 @@ async function unzipFile(zipPath, extractTo) {
89
97
} ) )
90
98
}
91
99
92
- async function downloadUpdate ( ref ) {
100
+ async function downloadUpdate ( url ) {
93
101
if ( ! fs . existsSync ( BASE_FS_PATH ) ) {
94
102
fs . mkdirSync ( BASE_FS_PATH , { recursive : true } )
95
103
}
96
104
97
105
// Download zip file
98
- await downloadFile ( BASE_URL_ZIPBALL + ref , TEMP_ZIP_PATH )
106
+ await downloadFile ( url , TEMP_ZIP_PATH )
99
107
100
108
// Backup current install if it exists
101
109
if ( fs . existsSync ( WEBUI_OLD_PATH ) ) {
@@ -120,23 +128,41 @@ async function downloadUpdate(ref) {
120
128
export async function checkForUpdate ( ) {
121
129
let branchName
122
130
switch ( process . env . RELEASE_TYPE ) {
123
- case 'dev' :
124
- branchName = DEV_BRANCH_NAME
125
- break
131
+ case undefined :
126
132
case 'stable' :
127
- default :
133
+ case 'latest' :
128
134
branchName = STABLE_BRANCH_NAME
129
135
break
136
+ case 'dev' :
137
+ case 'nightly' :
138
+ branchName = DEV_BRANCH_NAME
139
+ break
130
140
}
131
141
132
142
const installedVersion = getInstalledVersion ( )
133
- const latestVersion = await getLatestVersion ( branchName )
143
+
144
+ /** @type {VersionType | undefined } */
145
+ let latestVersion
146
+ if ( ! branchName ) {
147
+ // If unknown release type is specified, we try to match a specific version
148
+ latestVersion = extractVersion ( process . env . RELEASE_TYPE )
149
+ } else {
150
+ latestVersion = await getLatestVersion ( branchName )
151
+ }
152
+
153
+ if ( ! latestVersion ) {
154
+ return `Unable to find candidate for release type "${ process . env . RELEASE_TYPE } "`
155
+ }
134
156
135
157
if ( installedVersion ?. version !== latestVersion ?. version
136
158
|| installedVersion ?. commits !== latestVersion ?. commits
137
159
|| installedVersion ?. sha !== latestVersion ?. sha ) {
138
- await downloadUpdate ( branchName )
160
+ if ( ! branchName ) {
161
+ await downloadUpdate ( BASE_URL_RELEASE . replace ( '$TAG' , `v${ latestVersion . version } ` ) )
162
+ } else {
163
+ await downloadUpdate ( BASE_URL_ZIPBALL + branchName )
164
+ }
139
165
return `Update successful from ${ formatVersion ( installedVersion ) } to ${ formatVersion ( latestVersion ) } `
140
166
}
141
- return 'Already using the latest version'
167
+ return `Instance up-to-date using ref ${ process . env . RELEASE_TYPE } `
142
168
}
0 commit comments