Skip to content

Commit 25b139b

Browse files
authored
fix(update): Use release branches instead of release asset (#51)
1 parent 454f47a commit 25b139b

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/routines/update.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ import path from 'path'
66

77
config()
88

9-
const BASE_URL = 'https://api.github.com/repos/VueTorrent/VueTorrent/releases/'
10-
const STABLE_BRANCH_NAME = 'latest'
11-
const DEV_BRANCH_NAME = 'tags/latest_nightly'
9+
10+
const BASE_URL_VERSION = 'https://api.github.com/repos/VueTorrent/VueTorrent/contents/version.txt?ref='
11+
const BASE_URL_ZIPBALL = 'https://api.github.com/repos/VueTorrent/VueTorrent/zipball/'
12+
const STABLE_BRANCH_NAME = 'latest-release'
13+
const DEV_BRANCH_NAME = 'nightly-release'
1214
const VERSION_PATTERN = /^v?(?<version>[0-9.]+)(-(?<commits>\d+)-g(?<sha>[0-9a-f]+))?$/
1315

1416
const BASE_FS_PATH = process.env.VUETORRENT_PATH || '/vuetorrent'
15-
const TEMP_ZIP_PATH = `${BASE_FS_PATH}/vuetorrent.zip`
16-
const WEBUI_PATH = `${BASE_FS_PATH}/vuetorrent`
17-
const VERSION_FILE_PATH = `${WEBUI_PATH}/version.txt`
18-
const WEBUI_OLD_PATH = `${BASE_FS_PATH}/vuetorrent-old`
17+
const TEMP_ZIP_PATH = `${ BASE_FS_PATH }/vuetorrent.zip`
18+
const WEBUI_PATH = `${ BASE_FS_PATH }/vuetorrent`
19+
const VERSION_FILE_PATH = `${ WEBUI_PATH }/version.txt`
20+
const WEBUI_OLD_PATH = `${ BASE_FS_PATH }/vuetorrent-old`
1921

2022
function extractVersion(version) {
2123
const match = version.match(VERSION_PATTERN)
@@ -24,8 +26,8 @@ function extractVersion(version) {
2426

2527
function formatVersion(version) {
2628
if (!version) return 'unknown'
27-
if (!version.commits || !version.sha) return `v${version.version}`
28-
return `v${version.version}-${version.commits}-g${version.sha}`
29+
if (!version.commits || !version.sha) return `v${ version.version }`
30+
return `v${ version.version }-${ version.commits }-g${ version.sha }`
2931
}
3032

3133
function getInstalledVersion() {
@@ -47,10 +49,8 @@ async function getLatestVersion(url) {
4749
}
4850
})
4951

50-
return {
51-
latestVersion: extractVersion(response.data.name),
52-
download_url: response.data.assets[0].browser_download_url
53-
}
52+
// Extract from base64
53+
return extractVersion(atob(response.data.content.trim()))
5454
}
5555

5656
async function downloadFile(url, outputPath) {
@@ -74,7 +74,8 @@ async function unzipFile(zipPath, extractTo) {
7474
const zip = await JSZip.loadAsync(data)
7575
await Promise.all(Object.keys(zip.files).map(async (filename) => {
7676
const file = zip.files[filename]
77-
const filePath = path.join(extractTo, filename)
77+
const newName = filename.slice(filename.indexOf('/'))
78+
const filePath = path.join(extractTo, newName)
7879
if (file.dir) {
7980
fs.mkdirSync(filePath, { recursive: true })
8081
} else {
@@ -102,7 +103,7 @@ async function downloadUpdate(link) {
102103

103104
// Unzip the downloaded file
104105
try {
105-
await unzipFile(TEMP_ZIP_PATH, BASE_FS_PATH)
106+
await unzipFile(TEMP_ZIP_PATH, WEBUI_PATH)
106107
} catch (err) {
107108
console.error(err)
108109
// Restore backup if unzip fails
@@ -113,25 +114,27 @@ async function downloadUpdate(link) {
113114
}
114115

115116
export async function checkForUpdate() {
116-
let url
117+
let versionUrl, downloadUrl
117118
switch (process.env.RELEASE_TYPE) {
118119
case 'dev':
119-
url = BASE_URL + DEV_BRANCH_NAME
120+
versionUrl = BASE_URL_VERSION + DEV_BRANCH_NAME
121+
downloadUrl = BASE_URL_ZIPBALL + DEV_BRANCH_NAME
120122
break
121123
case 'stable':
122124
default:
123-
url = BASE_URL + STABLE_BRANCH_NAME
125+
versionUrl = BASE_URL_VERSION + STABLE_BRANCH_NAME
126+
downloadUrl = BASE_URL_ZIPBALL + STABLE_BRANCH_NAME
124127
break
125128
}
126129

127130
const installedVersion = getInstalledVersion()
128-
const { latestVersion, download_url } = await getLatestVersion(url)
131+
const latestVersion = await getLatestVersion(versionUrl)
129132

130133
if (installedVersion?.version !== latestVersion?.version
131-
|| installedVersion?.commits !== latestVersion?.commits
132-
|| installedVersion?.sha !== latestVersion?.sha) {
133-
await downloadUpdate(download_url)
134-
return `Update successful from ${formatVersion(installedVersion)} to ${formatVersion(latestVersion)}`
134+
|| installedVersion?.commits !== latestVersion?.commits
135+
|| installedVersion?.sha !== latestVersion?.sha) {
136+
await downloadUpdate(downloadUrl)
137+
return `Update successful from ${ formatVersion(installedVersion) } to ${ formatVersion(latestVersion) }`
135138
}
136139
return 'Already using the latest version'
137140
}

0 commit comments

Comments
 (0)