Skip to content

Commit 60ac230

Browse files
committed
downloadAndUnZip: refactor out three separate functions
Logically, `downloadAndUnZip()` does three things: - generate a temporary file path - download a file to that path - unzip the downloaded file and delete it Let's refactor this function to make this separation explicit, and individually reusable. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 6b2db73 commit 60ac230

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

github-release.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,28 @@ const getWorkflowRunArtifactsURLs = async (context, token, owner, repo, workflow
5858
}, {})
5959
}
6060

61-
const downloadAndUnZip = async (token, url, name) => {
61+
const download = async (token, url, outputFile) => {
6262
const { spawnSync } = require('child_process')
6363
const auth = token ? ['-H', `Authorization: Bearer ${token}`] : []
64-
const tmpFile = `${process.env.RUNNER_TEMP || process.env.TEMP || '/tmp'}/${name}.zip`
65-
const curl = spawnSync('curl', [...auth, '-Lo', tmpFile, url])
64+
const curl = spawnSync('curl', [...auth, '-Lo', outputFile, url])
6665
if (curl.error) throw curl.error
67-
const { mkdirSync, rmSync } = require('fs')
68-
await mkdirSync(name, { recursive: true })
69-
const unzip = spawnSync('unzip', ['-d', name, tmpFile])
66+
}
67+
68+
const unzip = async (zipFile, outputDirectory) => {
69+
const { mkdirSync } = require('fs')
70+
await mkdirSync(outputDirectory, { recursive: true })
71+
const { spawnSync } = require('child_process')
72+
const unzip = spawnSync('unzip', ['-d', outputDirectory, zipFile])
7073
if (unzip.error) throw unzip.error
74+
}
75+
76+
const getTempFile = (name) => `${process.env.RUNNER_TEMP || process.env.TEMP || '/tmp'}/${name}`
77+
78+
const downloadAndUnZip = async (token, url, name) => {
79+
const tmpFile = getTempFile(`${name}.zip`)
80+
await download(token, url, tmpFile)
81+
await unzip(tmpFile, name)
82+
const { rmSync } = require('fs')
7183
rmSync(tmpFile)
7284
}
7385

@@ -292,6 +304,9 @@ module.exports = {
292304
updateRelease,
293305
uploadReleaseAsset,
294306
getWorkflowRunArtifactsURLs,
307+
download,
308+
unzip,
309+
getTempFile,
295310
downloadAndUnZip,
296311
downloadBundleArtifacts,
297312
getGitArtifacts,

0 commit comments

Comments
 (0)