1 | import * as fs from 'fs-extra'
|
2 | import * as path from 'path'
|
3 | import * as request from 'request'
|
4 |
|
5 | const GITHUB_API = 'https://api.github.com/'
|
6 | const GITHUB = 'https://github.com/'
|
7 |
|
8 | export function getGithubRepoLatestReleaseVersion (repoName: string) {
|
9 | const latestReleaseApi = `${GITHUB_API}repos/${repoName}/releases/latest`
|
10 | const p = new Promise((resolve) => {
|
11 | request(
|
12 | {
|
13 | url: latestReleaseApi,
|
14 | headers: {
|
15 | 'User-Agent': 'Awesome-Octocat-App'
|
16 | }
|
17 | },
|
18 | (err, response, body) => {
|
19 | if (err) {
|
20 | throw new Error('快应用容器版本请求失败,请重试!')
|
21 | }
|
22 | const res = JSON.parse(body)
|
23 | resolve(res.tag_name)
|
24 | }
|
25 | )
|
26 | })
|
27 | return p
|
28 | }
|
29 |
|
30 | export async function downloadGithubRepoLatestRelease (repoName: string, appPath: string, dest: string) {
|
31 | const latestTagName = await getGithubRepoLatestReleaseVersion(repoName)
|
32 | return new Promise((resolve, reject) => {
|
33 | const downloadUrl = `${GITHUB}${repoName}/archive/${latestTagName}.zip`
|
34 | const downloadTemp = 'download_temp.zip'
|
35 | request({
|
36 | url: downloadUrl,
|
37 | headers: {
|
38 | 'User-Agent': 'Awesome-Octocat-App'
|
39 | }
|
40 | })
|
41 | .on('error', reject)
|
42 | .on('complete', () => {
|
43 | const downloadTempPath = path.join(appPath, downloadTemp)
|
44 | if (fs.existsSync(downloadTempPath)) {
|
45 | fs.moveSync(downloadTempPath, path.join(dest, downloadTemp))
|
46 | resolve()
|
47 | }
|
48 | })
|
49 | .pipe(fs.createWriteStream(downloadTemp))
|
50 | })
|
51 | }
|