mirror of https://github.com/actions/toolkit
overload downloadTool to accept destination path (#257)
parent
17acd9c66f
commit
61d502068b
|
@ -47,6 +47,47 @@ describe('@actions/tool-cache', function() {
|
||||||
expect(fs.statSync(downPath).size).toBe(35)
|
expect(fs.statSync(downPath).size).toBe(35)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('downloads a 35 byte file (dest)', async () => {
|
||||||
|
const dest = 'test-download-file'
|
||||||
|
try {
|
||||||
|
const downPath: string = await tc.downloadTool(
|
||||||
|
'http://example.com/bytes/35',
|
||||||
|
dest
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(downPath).toEqual(dest)
|
||||||
|
expect(fs.existsSync(downPath)).toBeTruthy()
|
||||||
|
expect(fs.statSync(downPath).size).toBe(35)
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
await fs.promises.unlink(dest)
|
||||||
|
} catch {
|
||||||
|
// intentionally empty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('downloads a 35 byte file (dest requires mkdirp)', async () => {
|
||||||
|
const dest = 'test-download-dir/test-download-file'
|
||||||
|
try {
|
||||||
|
const downPath: string = await tc.downloadTool(
|
||||||
|
'http://example.com/bytes/35',
|
||||||
|
dest
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(downPath).toEqual(dest)
|
||||||
|
expect(fs.existsSync(downPath)).toBeTruthy()
|
||||||
|
expect(fs.statSync(downPath).size).toBe(35)
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
await fs.promises.unlink(dest)
|
||||||
|
await fs.promises.rmdir(path.dirname(dest))
|
||||||
|
} catch {
|
||||||
|
// intentionally empty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
it('downloads a 35 byte file after a redirect', async () => {
|
it('downloads a 35 byte file after a redirect', async () => {
|
||||||
nock('http://example.com')
|
nock('http://example.com')
|
||||||
.get('/redirect-to')
|
.get('/redirect-to')
|
||||||
|
|
|
@ -48,9 +48,13 @@ if (!tempDirectory || !cacheRoot) {
|
||||||
* Download a tool from an url and stream it into a file
|
* Download a tool from an url and stream it into a file
|
||||||
*
|
*
|
||||||
* @param url url of tool to download
|
* @param url url of tool to download
|
||||||
|
* @param dest path to download tool
|
||||||
* @returns path to downloaded 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
|
// Wrap in a promise so that we can resolve from within stream callbacks
|
||||||
return new Promise<string>(async (resolve, reject) => {
|
return new Promise<string>(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
|
@ -58,14 +62,13 @@ export async function downloadTool(url: string): Promise<string> {
|
||||||
allowRetries: true,
|
allowRetries: true,
|
||||||
maxRetries: 3
|
maxRetries: 3
|
||||||
})
|
})
|
||||||
const destPath = path.join(tempDirectory, uuidV4())
|
dest = dest || path.join(tempDirectory, uuidV4())
|
||||||
|
await io.mkdirP(path.dirname(dest))
|
||||||
await io.mkdirP(tempDirectory)
|
|
||||||
core.debug(`Downloading ${url}`)
|
core.debug(`Downloading ${url}`)
|
||||||
core.debug(`Downloading ${destPath}`)
|
core.debug(`Downloading ${dest}`)
|
||||||
|
|
||||||
if (fs.existsSync(destPath)) {
|
if (fs.existsSync(dest)) {
|
||||||
throw new Error(`Destination file path ${destPath} already exists`)
|
throw new Error(`Destination file path ${dest} already exists`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const response: httpm.HttpClientResponse = await http.get(url)
|
const response: httpm.HttpClientResponse = await http.get(url)
|
||||||
|
@ -80,13 +83,13 @@ export async function downloadTool(url: string): Promise<string> {
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
|
|
||||||
const file: NodeJS.WritableStream = fs.createWriteStream(destPath)
|
const file: NodeJS.WritableStream = fs.createWriteStream(dest)
|
||||||
file.on('open', async () => {
|
file.on('open', async () => {
|
||||||
try {
|
try {
|
||||||
const stream = response.message.pipe(file)
|
const stream = response.message.pipe(file)
|
||||||
stream.on('close', () => {
|
stream.on('close', () => {
|
||||||
core.debug('download complete')
|
core.debug('download complete')
|
||||||
resolve(destPath)
|
resolve(dest)
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
core.debug(
|
core.debug(
|
||||||
|
|
Loading…
Reference in New Issue