Initial commit

This commit is contained in:
Robin Raju committed 2020-07-21 21:02:57 +02:00
commit b73ea9d0ac
17 files changed
+8065

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
export interface DownloadRequestData {
repoPath: string
fileTarget: string
token?: string
}
+54
View File
@@ -0,0 +1,54 @@
import * as fs from "fs"
import * as path from "path"
import * as thc from "typed-rest-client/HttpClient"
const API_ROOT = "https://api.github.com/repos"
const httpClient: thc.HttpClient = new thc.HttpClient("gh-api-client")
export async function download(
repoPath: string,
fileDest: string,
token: string
): Promise<void> {
const latestRelease = await getlatestRelease(repoPath)
await downloadFile(latestRelease, fileDest)
console.log(latestRelease)
}
async function getlatestRelease(repoPath: string): Promise<[string, string]> {
const response = await httpClient.get(
`${API_ROOT}/${repoPath}/releases/latest`
)
const responseBody = await response.readBody()
const _release = JSON.parse(responseBody.toString())
if (_release && _release["assets"].length > 0) {
const asset = _release["assets"][0]
return [asset["name"], asset["browser_download_url"]]
}
return ["", ""]
}
async function downloadFile(
[fileName, downloadUrl]: [string, string],
out: string
): Promise<string> {
const response = await httpClient.get(downloadUrl)
const outFilePath: string = path.join(out, fileName)
const fileStream: NodeJS.WritableStream = fs.createWriteStream(outFilePath)
if (response.message.statusCode !== 200) {
const err: Error = new Error(
`Unexpected response: ${response.message.statusCode}`
)
throw err
}
return new Promise((resolve, reject) => {
fileStream.on("error", err => reject(err))
const outStream = response.message.pipe(fileStream)
outStream.on("close", () => {
resolve(outFilePath)
})
})
}
+16
View File
@@ -0,0 +1,16 @@
import * as core from "@actions/core"
import {download} from "./download"
async function run(): Promise<void> {
try {
const repoPath = core.getInput("repo-path")
const fileDest = core.getInput("target")
const token = core.getInput("token")
const res = await download(repoPath, fileDest, token)
console.log(`Downloaded: ${res}`)
} catch (error) {
core.setFailed(error.message)
}
}
run()