Skip to content

Commit

Permalink
chore: add JSR imports to deno.json and fmt/lint (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen authored Apr 9, 2024
1 parent 7a6f45f commit 1783554
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 42 deletions.
6 changes: 6 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@
"./download": "./download.ts",
"./util": "./util.ts"
},
"imports": {
"@std/encoding/hex": "jsr:@std/encoding@^0.221.0/hex",
"@std/fmt/colors": "jsr:@std/fmt@^0.221.0/colors",
"@std/fs": "jsr:@std/fs@^0.221.0",
"@std/path": "jsr:@std/path@^0.221.0"
},
"lock": false
}
13 changes: 0 additions & 13 deletions deps.ts

This file was deleted.

8 changes: 4 additions & 4 deletions download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

import {
dirname,
ensureDir,
extname,
fromFileUrl,
green,
join,
normalize,
resolve,
} from "./deps.ts";
import {
} from "@std/path";
import { ensureDir } from "@std/fs";
import { green } from "@std/fmt/colors";
import type {
ArchRecord,
CacheLocation,
FetchOptions,
Expand Down
69 changes: 62 additions & 7 deletions download_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import {
assertEquals,
assertMatch,
assertRejects,
basename,
dirname,
normalize,
} from "./test_deps.ts";
} from "jsr:@std/assert";
import { basename, dirname, normalize } from "@std/path";

import { createDownloadURL, ensureCacheLocation } from "./download.ts";

const ALL_ARCHS = ["x86_64", "aarch64"];
Expand Down Expand Up @@ -195,11 +194,34 @@ Deno.test("createDownloadURL", async ({ step }) => {
});

Deno.test("ensureCacheLocation", async ({ step }) => {
await step("deno", async () => {
await step("deno", async ({ step }) => {
const location = await ensureCacheLocation("deno");
assertEquals(basename(location), "plug");
assertEquals(basename(dirname(location)), "deno");
assert(await isDirectory(location));

await step("missing", async () => {
// Save a snapshot of `Deno.build`
const build = structuredClone(Deno.build);
const HOME = Deno.env.get("HOME");
const DENO_DIR = Deno.env.get("DENO_DIR");
// @ts-ignore TS2540
Deno.build = { os: "linux" };
Deno.env.delete("HOME");
Deno.env.delete("DENO_DIR");

await assertRejects(
() => ensureCacheLocation("deno"),
Error,
"Could not get the deno cache directory, try using another CacheLocation in the plug options.",
);

// @ts-ignore TS2540
// Restore the snapshot of `Deno.build`
Deno.build = build;
if (HOME) Deno.env.set("HOME", HOME);
if (DENO_DIR) Deno.env.set("DENO_DIR", DENO_DIR);
});
});

await step("cwd", async () => {
Expand All @@ -209,10 +231,33 @@ Deno.test("ensureCacheLocation", async ({ step }) => {
assert(await isDirectory(location));
});

await step("cache", async () => {
await step("cache", async ({ step }) => {
const location = await ensureCacheLocation("cache");
assertEquals(basename(location), "plug");
assert(await isDirectory(location));

await step("missing", async () => {
// Save a snapshot of `Deno.build`
const build = structuredClone(Deno.build);
const HOME = Deno.env.get("HOME");
const XDG_CACHE_HOME = Deno.env.get("XDG_CACHE_HOME");
// @ts-ignore TS2540
Deno.build = { os: "linux" };
Deno.env.delete("HOME");
Deno.env.delete("XDG_CACHE_HOME");

await assertRejects(
() => ensureCacheLocation("cache"),
Error,
"Could not get the cache directory, try using another CacheLocation in the plug options.",
);

// @ts-ignore TS2540
// Restore the snapshot of `Deno.build`
Deno.build = build;
if (HOME) Deno.env.set("HOME", HOME);
if (XDG_CACHE_HOME) Deno.env.set("XDG_CACHE_HOME", XDG_CACHE_HOME);
});
});

await step("tmp", async () => {
Expand All @@ -221,14 +266,24 @@ Deno.test("ensureCacheLocation", async ({ step }) => {
assert(await isDirectory(location));
});

await step("string", async () => {
await step("path string", async () => {
const location = await ensureCacheLocation("./plug/cache/");
assertEquals(basename(location), "cache");
assertEquals(basename(dirname(location)), "plug");
assertEquals(normalize(dirname(dirname(location))), Deno.cwd());
assert(await isDirectory(location));
});

await step("file string", async () => {
const location = await ensureCacheLocation(
new URL("./plug/cache/", import.meta.url).href,
);
assertEquals(basename(location), "cache");
assertEquals(basename(dirname(location)), "plug");
assertEquals(normalize(dirname(dirname(location))), Deno.cwd());
assert(await isDirectory(location));
});

await step("URL", async () => {
const location = await ensureCacheLocation(
new URL("./plug/cache/url", import.meta.url),
Expand Down
2 changes: 0 additions & 2 deletions test_deps.ts

This file was deleted.

7 changes: 6 additions & 1 deletion test_import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"@denosaurs/plug": "./mod.ts",
"@denosaurs/plug/types": "./types.ts",
"@denosaurs/plug/download": "./download.ts",
"@denosaurs/plug/util": "./util.ts"
"@denosaurs/plug/util": "./util.ts",
"@std/assert": "jsr:@std/assert@^0.221.0",
"@std/encoding/hex": "jsr:@std/encoding@^0.221.0/hex",
"@std/fmt/colors": "jsr:@std/fmt@^0.221.0/colors",
"@std/fs": "jsr:@std/fs@^0.221.0",
"@std/path": "jsr:@std/path@^0.221.0"
}
}
12 changes: 3 additions & 9 deletions util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@
* @module
*/

import {
hex,
isAbsolute,
join,
normalize,
resolve,
toFileUrl,
} from "./deps.ts";
import { isAbsolute, join, normalize, resolve, toFileUrl } from "@std/path";
import { encodeHex } from "@std/encoding/hex";

const encoder = new TextEncoder();

Expand Down Expand Up @@ -61,7 +55,7 @@ export function stringToURL(url: string): URL {
* @private
*/
export async function hash(value: string): Promise<string> {
return hex(
return encodeHex(
new Uint8Array(
await crypto.subtle.digest("SHA-256", encoder.encode(value)),
),
Expand Down
9 changes: 3 additions & 6 deletions util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ import {
assertEquals,
assertRejects,
assertThrows,
basename,
dirname,
fromFileUrl,
join,
normalize,
} from "./test_deps.ts";
} from "jsr:@std/assert";
import { basename, dirname, fromFileUrl, join, normalize } from "@std/path";

import {
cacheDir,
denoCacheDir,
Expand Down

0 comments on commit 1783554

Please sign in to comment.