Skip to content

Commit

Permalink
feat: cache download Promise (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas authored Apr 13, 2022
1 parent e243837 commit 96fbb2a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
57 changes: 33 additions & 24 deletions src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface RunOptions {

class DenoBridge {
cacheDirectory: string
currentDownload?: ReturnType<DenoBridge['downloadBinary']>
debug: boolean
onAfterDownload?: LifecycleHook
onBeforeDownload?: LifecycleHook
Expand All @@ -48,6 +49,34 @@ class DenoBridge {
this.versionRange = options.versionRange ?? DENO_VERSION_RANGE
}

private async downloadBinary() {
if (this.onBeforeDownload) {
this.onBeforeDownload()
}

await fs.mkdir(this.cacheDirectory, { recursive: true })

this.log(`Downloading Deno CLI to ${this.cacheDirectory}...`)

const binaryPath = await download(this.cacheDirectory, this.versionRange)
const downloadedVersion = await DenoBridge.getBinaryVersion(binaryPath)

// We should never get here, because it means that `DENO_VERSION_RANGE` is
// a malformed semver range. If this does happen, let's throw an error so
// that the tests catch it.
if (downloadedVersion === undefined) {
throw new Error('Could not read downloaded binary')
}

await this.writeVersionFile(downloadedVersion)

if (this.onAfterDownload) {
this.onAfterDownload()
}

return binaryPath
}

static async getBinaryVersion(binaryPath: string) {
try {
const { stdout } = await execa(binaryPath, ['--version'])
Expand Down Expand Up @@ -98,30 +127,12 @@ class DenoBridge {
return globalBinaryName
}

private async getRemoteBinary() {
if (this.onBeforeDownload) {
this.onBeforeDownload()
}

await fs.mkdir(this.cacheDirectory, { recursive: true })

const binaryPath = await download(this.cacheDirectory, this.versionRange)
const downloadedVersion = await DenoBridge.getBinaryVersion(binaryPath)

// We should never get here, because it means that `DENO_VERSION_RANGE` is
// a malformed semver range. If this does happen, let's throw an error so
// that the tests catch it.
if (downloadedVersion === undefined) {
throw new Error('Could not read downloaded binary')
}

await this.writeVersionFile(downloadedVersion)

if (this.onAfterDownload) {
this.onAfterDownload()
private getRemoteBinary() {
if (this.currentDownload === undefined) {
this.currentDownload = this.downloadBinary()
}

return binaryPath
return this.currentDownload
}

private log(...data: unknown[]) {
Expand Down Expand Up @@ -166,8 +177,6 @@ class DenoBridge {
return { global: false, path: cachedPath }
}

this.log('Downloading Deno CLI...')

const downloadedPath = await this.getRemoteBinary()

return { global: false, path: downloadedPath }
Expand Down
7 changes: 6 additions & 1 deletion src/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ const download = async (targetDirectory: string, versionRange: string) => {
})

await extractBinaryFromZip(zipPath, binaryPath, binaryName)
await fs.promises.unlink(zipPath)

try {
await fs.promises.unlink(zipPath)
} catch {
// no-op
}

return binaryPath
}
Expand Down

0 comments on commit 96fbb2a

Please sign in to comment.