fix: failing download of a plugin resulted stuck in the state

Massimo Melina committed May 28, 2023 at 17:09 UTC 2e074483ad90ed96f3eb1ef2481583996093fc28
1 file changed +31 -27
src/github.ts
+31 -27
@@ -24,33 +24,37 @@ export async function downloadPlugin(repo: string, branch='', overwrite?: boolea
24 if (downloading[repo])
25 return new ApiError(HTTP_CONFLICT, "already downloading")
26 downloadProgress(repo, true)
27 - const rec = await getRepoInfo(repo)
28 - if (!branch)
29 - branch = rec.default_branch
30 - const short = repo.split('/')[1] // second part, repo without the owner
31 - if (!short)
32 - return new ApiError(HTTP_BAD_REQUEST, "bad repo")
33 - const folder2repo = getFolder2repo()
34 - const folder = overwrite ? _.findKey(folder2repo, x => x===repo)! // use existing folder
35 - : short in folder2repo ? repo.replace('/','-') // longer form only if another plugin is using short form
36 - : short
37 - const installPath = PLUGINS_PATH + '/' + folder
38 - const GITHUB_ZIP_ROOT = short + '-' + branch // GitHub puts everything within this folder
39 - const rootWithinZip = GITHUB_ZIP_ROOT + '/' + DIST_ROOT
40 - const foldersToCopy = [ // from longer to shorter, so we first test the longer
41 - rootWithinZip + '-' + process.platform + '-' + process.arch,
42 - rootWithinZip + '-' + process.platform,
43 - rootWithinZip,
44 - ].map(x => x + '/')
45 - // this zip doesn't have content-length, so we cannot produce progress event
46 - const stream = await httpsStream(`https://github.com/${repo}/archive/refs/heads/${branch}.zip`)
47 - await unzip(stream, path => {
48 - const folder = foldersToCopy.find(x => path.startsWith(x))
49 - return folder ? installPath + '/' + path.slice(folder.length) : false
50 - })
51 - downloadProgress(repo, undefined)
52 - await rescan() // workaround: for some reason, operations are not triggering the rescan of the watched folder. Let's invoke it.
53 - return folder
27 + try {
28 + const rec = await getRepoInfo(repo)
29 + if (!branch)
30 + branch = rec.default_branch
31 + const short = repo.split('/')[1] // second part, repo without the owner
32 + if (!short)
33 + return new ApiError(HTTP_BAD_REQUEST, "bad repo")
34 + const folder2repo = getFolder2repo()
35 + const folder = overwrite ? _.findKey(folder2repo, x => x===repo)! // use existing folder
36 + : short in folder2repo ? repo.replace('/','-') // longer form only if another plugin is using short form
37 + : short
38 + const installPath = PLUGINS_PATH + '/' + folder
39 + const GITHUB_ZIP_ROOT = short + '-' + branch // GitHub puts everything within this folder
40 + const rootWithinZip = GITHUB_ZIP_ROOT + '/' + DIST_ROOT
41 + const foldersToCopy = [ // from longer to shorter, so we first test the longer
42 + rootWithinZip + '-' + process.platform + '-' + process.arch,
43 + rootWithinZip + '-' + process.platform,
44 + rootWithinZip,
45 + ].map(x => x + '/')
46 + // this zip doesn't have content-length, so we cannot produce progress event
47 + const stream = await httpsStream(`https://github.com/${repo}/archive/refs/heads/${branch}.zip`)
48 + await unzip(stream, path => {
49 + const folder = foldersToCopy.find(x => path.startsWith(x))
50 + return folder ? installPath + '/' + path.slice(folder.length) : false
51 + })
52 + await rescan() // workaround: for some reason, operations are not triggering the rescan of the watched folder. Let's invoke it.
53 + return folder
54 + }
55 + finally {
56 + downloadProgress(repo, undefined)
57 + }
58 }
59
60 export function getRepoInfo(id: string) {