Skip to content

Commit 1a56892

Browse files
authored
feat: Allow version pinning (#89)
1 parent 2318ca4 commit 1a56892

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

src/routines/update.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ config()
99

1010
const BASE_URL_VERSION = '/repos/VueTorrent/VueTorrent/contents/version.txt?ref='
1111
const BASE_URL_ZIPBALL = '/repos/VueTorrent/VueTorrent/zipball/'
12+
const BASE_URL_RELEASE = 'https://github.com/VueTorrent/VueTorrent/releases/download/$TAG/vuetorrent.zip'
1213
const STABLE_BRANCH_NAME = 'latest-release'
1314
const DEV_BRANCH_NAME = 'nightly-release'
1415
const VERSION_PATTERN = /^v?(?<version>[0-9.]+)(-(?<commits>\d+)-g(?<sha>[0-9a-f]+))?$/
@@ -24,9 +25,16 @@ const githubClient = axios.create({
2425
headers: {
2526
Accept: 'application/vnd.github.v3+json',
2627
Authorization: process.env.GITHUB_AUTH ? `Bearer ${process.env.GITHUB_AUTH}` : undefined,
28+
'User-Agent': `vuetorrent/vuetorrent-backend`
2729
}
2830
})
2931

32+
/** @typedef {{ version: string; commits?: string; sha?: string }} VersionType */
33+
34+
/**
35+
* @param version {string}
36+
* @returns {VersionType | undefined}
37+
*/
3038
function extractVersion(version) {
3139
const match = version.match(VERSION_PATTERN)
3240
return match?.groups
@@ -89,13 +97,13 @@ async function unzipFile(zipPath, extractTo) {
8997
}))
9098
}
9199

92-
async function downloadUpdate(ref) {
100+
async function downloadUpdate(url) {
93101
if (!fs.existsSync(BASE_FS_PATH)) {
94102
fs.mkdirSync(BASE_FS_PATH, { recursive: true })
95103
}
96104

97105
// Download zip file
98-
await downloadFile(BASE_URL_ZIPBALL + ref, TEMP_ZIP_PATH)
106+
await downloadFile(url, TEMP_ZIP_PATH)
99107

100108
// Backup current install if it exists
101109
if (fs.existsSync(WEBUI_OLD_PATH)) {
@@ -120,23 +128,41 @@ async function downloadUpdate(ref) {
120128
export async function checkForUpdate() {
121129
let branchName
122130
switch (process.env.RELEASE_TYPE) {
123-
case 'dev':
124-
branchName = DEV_BRANCH_NAME
125-
break
131+
case undefined:
126132
case 'stable':
127-
default:
133+
case 'latest':
128134
branchName = STABLE_BRANCH_NAME
129135
break
136+
case 'dev':
137+
case 'nightly':
138+
branchName = DEV_BRANCH_NAME
139+
break
130140
}
131141

132142
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+
}
134156

135157
if (installedVersion?.version !== latestVersion?.version
136158
|| installedVersion?.commits !== latestVersion?.commits
137159
|| 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+
}
139165
return `Update successful from ${ formatVersion(installedVersion) } to ${ formatVersion(latestVersion) }`
140166
}
141-
return 'Already using the latest version'
167+
return `Instance up-to-date using ref ${process.env.RELEASE_TYPE}`
142168
}

0 commit comments

Comments
 (0)