Skip to content

Commit

Permalink
Use deno^2 (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl authored Oct 23, 2024
1 parent 5ae6c80 commit e8c3b03
Show file tree
Hide file tree
Showing 35 changed files with 107 additions and 111 deletions.
5 changes: 2 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
]
}
},
"pkgx": "deno^1.40",
"pkgx": "deno^2.0",
"tasks": {
"test": "deno test --parallel --unstable-fs --unstable-ffi --allow-env --allow-read --allow-ffi --allow-net=dist.pkgx.dev,github.com,codeload.github.com --allow-write --allow-run=tar,uname,/bin/sh,foo,'C:\\Windows\\system32\\cmd.exe'",
"test": "deno test --parallel --unstable-fs --unstable-ffi --allow-all",
"typecheck": "deno check ./mod.ts"
},
"lint": {
Expand All @@ -25,7 +25,6 @@
},
"imports": {
"is-what": "https://deno.land/x/[email protected]/src/index.ts",
"deno/": "https://deno.land/[email protected]/",
"outdent": "https://deno.land/x/[email protected]/mod.ts"
}
}
13 changes: 6 additions & 7 deletions src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import * as outdent from "outdent"
export { outdent }

// importing super specifically to reduce final npm bundle size
import * as crypto from "deno/crypto/mod.ts"
import { moveSync } from "deno/fs/move.ts"
import { readLines } from "deno/io/read_lines.ts"
import { writeAll } from "deno/io/write_all.ts"
import { parse as parseYaml } from "deno/yaml/parse.ts"
import { SEPARATOR as SEP } from "deno/path/mod.ts"
import { fromFileUrl } from "deno/path/mod.ts"
import * as crypto from "jsr:@std/crypto@1"
import { moveSync } from "jsr:@std/fs@1"
import { readLines } from "https://deno.land/[email protected]/io/read_lines.ts"
import { writeAll } from "jsr:@std/[email protected]"
import { parse as parseYaml } from "jsr:@std/yaml@1"
import { SEPARATOR as SEP, fromFileUrl } from "jsr:@std/path@1"

const streams = { writeAll }
const io = { readLines }
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useCache.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Stowage, StowageNativeBottle } from "../types.ts"
import { useTestConfig } from "./useTestConfig.ts"
import { assertEquals } from "deno/assert/mod.ts"
import { assertEquals } from "jsr:@std/assert"
import SemVer from "../utils/semver.ts"
import useCache from "./useCache.ts"
import host from "../utils/host.ts"
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useCellar.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals, assertRejects } from "deno/assert/mod.ts"
import { assertEquals, assertRejects } from "jsr:@std/assert"
import SemVer, * as semver from "../utils/semver.ts"
import { useTestConfig } from "./useTestConfig.ts"
import useCellar from "./useCellar.ts"
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, assertEquals, assertFalse, assertThrows, assertMatch } from "deno/assert/mod.ts"
import { assert, assertEquals, assertFalse, assertThrows, assertMatch } from "jsr:@std/assert"
import { _internals, ConfigDefault } from "./useConfig.ts"
import { useTestConfig } from "./useTestConfig.ts"
import Path from "../utils/Path.ts"
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useDownload.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useTestConfig } from "./useTestConfig.ts"
import { assert } from "deno/assert/mod.ts"
import { assert } from "jsr:@std/assert"
import useDownload from "./useDownload.ts"

//TODO don’t actually do http obv.
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useDownload.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { deno } from "../deps.ts"
const { crypto: crypto_, streams: { writeAll } } = deno
const { crypto } = crypto_
import { encodeHex } from "deno/encoding/hex.ts"
import { encodeHex } from "jsr:@std/encoding"
import { PkgxError, panic } from "../utils/error.ts"
import useConfig from "./useConfig.ts"
import useFetch from "./useFetch.ts"
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useFetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stub, assertSpyCallArgs } from "deno/testing/mock.ts"
import { stub, assertSpyCallArgs } from "jsr:@std/testing/mock"
import { useTestConfig } from "./useTestConfig.ts";
import useFetch, { _internals } from "./useFetch.ts"

Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useInventory.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// deno-lint-ignore-file require-await no-explicit-any
import { assertEquals } from "deno/assert/assert_equals.ts"
import { assertEquals } from "jsr:@std/assert"
import SemVer, * as semver from "../utils/semver.ts"
import { assertRejects } from "deno/assert/mod.ts"
import * as mock from "deno/testing/mock.ts"
import { assertRejects } from "jsr:@std/assert"
import * as mock from "jsr:@std/testing/mock"
import { _internals } from "./useFetch.ts"
import specimen from "./useInventory.ts"

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useMoustaches.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "deno/assert/mod.ts"
import { assertEquals } from "jsr:@std/assert"
import { useTestConfig } from "./useTestConfig.ts"
import useMoustaches from "./useMoustaches.ts"
import { Package } from "../types.ts"
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/usePantry.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { assert, assertEquals, assertThrows } from "deno/assert/mod.ts"
import { assert, assertEquals, assertThrows } from "jsr:@std/assert"
import usePantry, { validatePackageRequirement } from "./usePantry.ts"
import { useTestConfig, srcroot } from "./useTestConfig.ts"
import { _internals } from "../utils/host.ts"
import { stub } from "deno/testing/mock.ts"
import { stub } from "jsr:@std/testing/mock"
import SemVer from "../utils/semver.ts"

Deno.test("provides()", async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useShellEnv.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "deno/assert/assert_equals.ts"
import { assertEquals } from "jsr:@std/assert"
import { useTestConfig } from "./useTestConfig.ts"
import useShellEnv from "./useShellEnv.ts"
import SemVer from "../utils/semver.ts"
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useSync.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import specimen, { _internals } from "./useSync.ts"
import { useTestConfig } from "./useTestConfig.ts"
import * as mock from "deno/testing/mock.ts"
import { assert } from "deno/assert/mod.ts"
import * as mock from "jsr:@std/testing/mock"
import { assert } from "jsr:@std/assert"
import usePantry from "./usePantry.ts"

// NOTE actually syncs from github
Expand Down
52 changes: 29 additions & 23 deletions src/hooks/useSync.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// deno-lint-ignore-file no-deprecated-deno-api
// ^^ Deno.Command is not implemented for dnt shims yet
import { deno } from "../deps.ts"
const { streams: { writeAll } } = deno
import { flock } from "../utils/flock.ts"
import useDownload from "./useDownload.ts"
import usePantry from "./usePantry.ts"
Expand Down Expand Up @@ -64,20 +60,18 @@ async function sync(pantry_dir: Path) {
// ∴ download the latest tarball and uncompress over the top
//FIXME deleted packages will not be removed with this method
const src = new URL(`https://github.com/pkgxdev/pantry/archive/refs/heads/main.tar.gz`)
const proc = Deno.run({
cmd: ["tar", "xzf", "-", "--strip-components=1"],
const proc = new Deno.Command("tar", {
args: ["xzf", "-", "--strip-components=1"],
cwd: pantry_dir.string,
stdin: "piped"
})
await useDownload().download({ src }, blob => writeAll(proc.stdin, blob))
proc.stdin.close()
stdin: "piped",
}).spawn()
const writer = proc.stdin.getWriter()
await useDownload().download({ src }, blob => writer.write(blob))
writer.close()

if (!(await proc.status()).success) {
if (!(await proc.status).success) {
throw new Error("untar failed")
}

proc.close()

}
}

Expand All @@ -94,16 +88,28 @@ export interface RunOptions {
}

async function run(opts: RunOptions) {
const cmd = opts.cmd.map(x => `${x}`)
const env = (({ HTTP_PROXY, HTTPS_PROXY }) => ({ HTTP_PROXY, HTTPS_PROXY }))(Deno.env.toObject())
const proc = Deno.run({ ...opts, cmd, stdout: 'null', clearEnv: true, env })
const cmd = opts.cmd.map(x => `${x}`);
const env = (({ HTTP_PROXY, HTTPS_PROXY }) => ({ HTTP_PROXY, HTTPS_PROXY }))(Deno.env.toObject());

const proc = new Deno.Command(cmd[0], {
args: cmd.slice(1),
stdout: 'null',
clearEnv: true,
env,
...opts,
});

const child = proc.spawn();

try {
const exit = await proc.status()
if (!exit.success) throw new Error(`run.exit(${exit.code})`)
const status = await child.status;
if (!status.success) {
throw new Error(`run.exit(${status.code})`);
}
} catch (err) {
err.cause = proc
throw err
} finally {
proc.close()
if (err instanceof Error) {
err.cause = child;
}
throw err;
}
}
2 changes: 1 addition & 1 deletion src/hooks/useSyncCache.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import specimen, { provides, dependencies, available, runtime_env, completion, companions } from "./useSyncCache.ts"
import { useTestConfig } from "./useTestConfig.ts"
import { assert, assertEquals } from "deno/assert/mod.ts"
import { assert, assertEquals } from "jsr:@std/assert"
import { _internals } from "./useSync.ts"
import usePantry from "./usePantry.ts"

Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useTestConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import useConfig, { ConfigDefault } from "./useConfig.ts"
import { fromFileUrl } from "deno/path/mod.ts"
import { fromFileUrl } from "jsr:@std/path"
import Path from "../utils/Path.ts"

export function useBaseTestConfig(env?: Record<string, string>) {
Expand Down Expand Up @@ -33,7 +33,7 @@ export const srcroot = (() => {
}
})()

import { walkSync } from 'deno/fs/mod.ts'
import { walkSync } from 'jsr:@std/fs'

// deno/dnt has a broken shim for this function
function copyDirectory(src: string, dest: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/plumbing/hydrate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert, assertEquals, assertRejects } from "deno/assert/mod.ts"
import { describe, it } from "deno/testing/bdd.ts"
import { assert, assertEquals, assertRejects } from "jsr:@std/assert"
import { describe, it } from "jsr:@std/testing/bdd"
import { PackageRequirement } from "../types.ts"
import * as semver from "../utils/semver.ts"
import hydrate from "./hydrate.ts"
Expand Down
4 changes: 2 additions & 2 deletions src/plumbing/install.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// deno-lint-ignore-file no-explicit-any
import { useTestConfig, srcroot } from "../hooks/useTestConfig.ts"
import { assert, assertEquals, assertFalse } from "deno/assert/mod.ts"
import { assert, assertEquals, assertFalse } from "jsr:@std/assert"
import install, { ConsoleLogger, Logger } from "./install.ts"
import { stub } from "deno/testing/mock.ts"
import { stub } from "jsr:@std/testing/mock"
import SemVer from "../utils/semver.ts"
import { Package } from "../types.ts"
import host from "../utils/host.ts";
Expand Down
21 changes: 7 additions & 14 deletions src/plumbing/install.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// deno-lint-ignore-file no-deprecated-deno-api
// ^^ dnt doesn’t support Deno.Command yet so we’re stuck with the deprecated Deno.run for now

import { Package, Installation, StowageNativeBottle } from "../types.ts"
import useOffLicense from "../hooks/useOffLicense.ts"
import useDownload from "../hooks/useDownload.ts"
Expand All @@ -11,9 +8,6 @@ import useCache from "../hooks/useCache.ts"
import useFetch from "../hooks/useFetch.ts"
import { createHash } from "node:crypto"
import Path from "../utils/Path.ts"
import { deno } from "../deps.ts"

const { streams: { writeAll } } = deno

export default async function install(pkg: Package, logger?: Logger): Promise<Installation> {
const { project, version } = pkg
Expand Down Expand Up @@ -49,15 +43,16 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In
//NOTE ^^ inside pkgx prefix to avoid TMPDIR is on a different volume problems
})
const tar_args = compression == 'xz' ? 'xJf' : 'xzf' // laughably confusing
const untar = Deno.run({
cmd: ["tar", tar_args, "-", "--strip-components", (pkg.project.split("/").length + 1).toString()],
const untar = new Deno.Command("tar", {
args: [tar_args, "-", "--strip-components", (pkg.project.split("/").length + 1).toString()],
stdin: 'piped', stdout: "inherit", stderr: "inherit",
cwd: tmpdir.string,
/// hard coding path to ensure we don’t deadlock trying to use ourselves to untar ourselves
env: { PATH }
})
}).spawn()
const hasher = createHash("sha256")
const remote_SHA_promise = remote_SHA(new URL(`${url}.sha256sum`))
const writer = untar.stdin.getWriter()

let total: number | undefined
let n = 0
Expand All @@ -72,16 +67,14 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In
n += blob.length
hasher.update(blob)
logger?.installing?.({ pkg, progress: total ? n / total : total })
return writeAll(untar.stdin, blob)
return writer.write(blob)
})

untar.stdin.close()
writer.close()

const untar_exit_status = await untar.status()
const untar_exit_status = await untar.status
if (!untar_exit_status.success) {
throw new Error(`tar exited with status ${untar_exit_status.code}`)
} else {
untar.close() //TODO should we do this for the error case too or what?
}

const computed_hash_value = hasher.digest("hex")
Expand Down
2 changes: 1 addition & 1 deletion src/plumbing/link.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useTestConfig } from "../hooks/useTestConfig.ts"
import { assert } from "deno/assert/mod.ts"
import { assert } from "jsr:@std/assert"
import SemVer from "../utils/semver.ts"
import link from "./link.ts";

Expand Down
4 changes: 2 additions & 2 deletions src/plumbing/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default async function link(pkg: Package | Installation) {
} catch (err) {
// we were deleted by another thing linking simultaneously
//FIXME our flock should surround the link step too
if (err.code != 'ENOENT') throw err
if (err instanceof Error && "code" in err && err.code != 'ENOENT') throw err
}
}

Expand All @@ -63,7 +63,7 @@ export default async function link(pkg: Package | Installation) {
shelf.join(symname).rm().string,
{type: 'dir'})
} catch (err) {
if (err instanceof Deno.errors.AlreadyExists || err.code === 'EEXIST') {
if (err instanceof Deno.errors.AlreadyExists || err instanceof Error && "code" in err && err.code === 'EEXIST') {
//FIXME race condition for installing the same pkg simultaneously
// real fix is to lock around the entire download/untar/link process
return
Expand Down
4 changes: 2 additions & 2 deletions src/plumbing/resolve.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// deno-lint-ignore-file require-await
import { assert, assertEquals, fail, assertRejects } from "deno/assert/mod.ts"
import { assert, assertEquals, fail, assertRejects } from "jsr:@std/assert"
import { Installation, Package, PackageRequirement } from "../types.ts"
import { useTestConfig } from "../hooks/useTestConfig.ts"
import useInventory from "../hooks/useInventory.ts"
import resolve, { _internals } from "./resolve.ts"
import useCellar from "../hooks/useCellar.ts"
import * as semver from "../utils/semver.ts"
import { stub } from "deno/testing/mock.ts"
import { stub } from "jsr:@std/testing/mock"
import SemVer from "../utils/semver.ts"
import Path from "../utils/Path.ts"

Expand Down
2 changes: 1 addition & 1 deletion src/plumbing/which.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, assertEquals } from "deno/assert/mod.ts"
import { assert, assertEquals } from "jsr:@std/assert"
import { isArray } from "is-what"
import which from "./which.ts"
import { useTestConfig } from "../hooks/useTestConfig.ts";
Expand Down
4 changes: 2 additions & 2 deletions src/porcelain/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import { useTestConfig } from "../hooks/useTestConfig.ts"
import SemVer, * as semver from "../utils/semver.ts"
import install, { ConsoleLogger, _internals } from "./install.ts"
import { assert, assertEquals } from "deno/assert/mod.ts"
import { assert, assertEquals } from "jsr:@std/assert"
import type { Installation } from "../types.ts"
import usePantry from "../hooks/usePantry.ts"
import useConfig from "../hooks/useConfig.ts"
import { stub } from "deno/testing/mock.ts"
import { stub } from "jsr:@std/testing/mock"
import Path from "../utils/Path.ts";

Deno.test("porcelain.install", async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/porcelain/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useTestConfig } from "../hooks/useTestConfig.ts"
import { assertEquals, assertRejects } from "deno/assert/mod.ts"
import { assertEquals, assertRejects } from "jsr:@std/assert"
import undent from "outdent"
import run from "./run.ts"

Expand Down
4 changes: 2 additions & 2 deletions src/utils/Path.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert, assertEquals, assertFalse, assertThrows } from "deno/assert/mod.ts"
import { SEPARATOR as SEP } from "deno/path/mod.ts"
import { assert, assertEquals, assertFalse, assertThrows } from "jsr:@std/assert"
import { SEPARATOR as SEP } from "jsr:@std/path"
import Path from "./Path.ts"

Deno.test("test Path", async test => {
Expand Down
Loading

0 comments on commit e8c3b03

Please sign in to comment.