overload downloadTool to accept destination path (#257)

This commit is contained in:
eric sciple
2019-12-16 11:59:48 -05:00
committed by GitHub
parent 17acd9c66f
commit 61d502068b
2 changed files with 53 additions and 9 deletions

View File

@@ -48,9 +48,13 @@ if (!tempDirectory || !cacheRoot) {
* Download a tool from an url and stream it into a file
*
* @param url url of tool to download
* @param dest path to download tool
* @returns path to downloaded tool
*/
export async function downloadTool(url: string): Promise<string> {
export async function downloadTool(
url: string,
dest?: string
): Promise<string> {
// Wrap in a promise so that we can resolve from within stream callbacks
return new Promise<string>(async (resolve, reject) => {
try {
@@ -58,14 +62,13 @@ export async function downloadTool(url: string): Promise<string> {
allowRetries: true,
maxRetries: 3
})
const destPath = path.join(tempDirectory, uuidV4())
await io.mkdirP(tempDirectory)
dest = dest || path.join(tempDirectory, uuidV4())
await io.mkdirP(path.dirname(dest))
core.debug(`Downloading ${url}`)
core.debug(`Downloading ${destPath}`)
core.debug(`Downloading ${dest}`)
if (fs.existsSync(destPath)) {
throw new Error(`Destination file path ${destPath} already exists`)
if (fs.existsSync(dest)) {
throw new Error(`Destination file path ${dest} already exists`)
}
const response: httpm.HttpClientResponse = await http.get(url)
@@ -80,13 +83,13 @@ export async function downloadTool(url: string): Promise<string> {
throw err
}
const file: NodeJS.WritableStream = fs.createWriteStream(destPath)
const file: NodeJS.WritableStream = fs.createWriteStream(dest)
file.on('open', async () => {
try {
const stream = response.message.pipe(file)
stream.on('close', () => {
core.debug('download complete')
resolve(destPath)
resolve(dest)
})
} catch (err) {
core.debug(