Skip to content

Commit

Permalink
Don’t leave tmp directories when installing
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Mar 29, 2023
1 parent 909407e commit 8590f9c
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/prefab/install.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { usePrefix, useCache, useCellar, useDownload, useOffLicense, useFetch } from "hooks"
import { usePrefix, useCache, useCellar, useDownload, useOffLicense, useFetch, useConfig } from "hooks"
import { host, panic, pkg as pkgutils } from "utils"
import useLogger, { Logger, red, teal, gray } from "hooks/useLogger.ts"
import { Installation, StowageNativeBottle } from "types"
import { crypto, toHashString } from "deno/crypto/mod.ts"
import { Package } from "types"
import useConfig from "../hooks/useConfig.ts"
import Path from "path"

export default async function install(pkg: Package, logger?: Logger): Promise<Installation> {
const { project, version } = pkg
Expand All @@ -17,7 +17,6 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In
const stowage = StowageNativeBottle({ pkg: { project, version }, compression })
const url = useOffLicense('s3').url(stowage)
const tarball = useCache().path(stowage)
const vdirname = `v${pkg.version}`
const shelf = tea_prefix.join(pkg.project)

const log_install_msg = (install: Installation, title = 'installed') => {
Expand All @@ -30,7 +29,7 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In
}

if (dryrun) {
const install = { pkg, path: tea_prefix.join(pkg.project, vdirname) }
const install = { pkg, path: tea_prefix.join(pkg.project, `v${pkg.version}`) }
log_install_msg(install, 'imagined')
return install
}
Expand All @@ -57,17 +56,21 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In
const tee = stream.tee()
const pp: Promise<unknown>[] = []

if (is_downloading) { // cache the download
if (is_downloading) { // cache the download (write stream to disk)
tarball.parent().mkpath()
const f = await Deno.open(tarball.string, {create: true, write: true, truncate: true})
const teee = tee[1].tee()
pp.push(teee[0].pipeTo(f.writable))
tee[1] = teee[1]
}

const tmpdir = usePrefix().join("tmp").mkpath()
const tmpdir = Path.mktemp({
prefix: pkg.project.replaceAll("/", "_") + "_",
dir: usePrefix().join("local/tmp")
//NOTE ^^ inside tea prefix to avoid TMPDIR is on a different volume problems
})
const untar = new Deno.Command("tar", {
args: [tar_args],
args: [tar_args, "--strip-components", (pkg.project.split("/").length + 1).toString()],
stdin: 'piped', stdout: "inherit", stderr: "inherit",
cwd: tmpdir.string,
}).spawn()
Expand All @@ -82,7 +85,7 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In
const [computed_hash_value, checksum, tar_exit_status] = await Promise.all(pp) as [string, string, Deno.CommandStatus]

if (!tar_exit_status.success) {
throw new Error(`tar exited with status ${tar_exit_status}`)
throw new Error(`tar exited with status ${tar_exit_status.code}`)
}

if (computed_hash_value != checksum) {
Expand All @@ -92,7 +95,7 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In
throw new Error(`sha: expected: ${checksum}, got: ${computed_hash_value}`)
}

const path = tmpdir.join(pkg.project, `v${pkg.version}`).mv({ into: shelf })
const path = tmpdir.mv({ to: shelf.join(`v${pkg.version}`) })
const install = { pkg, path }

log_install_msg(install)
Expand Down

0 comments on commit 8590f9c

Please sign in to comment.