diff --git a/lib/import_map.ts b/lib/import_map.ts index 70e30bd2..1d0cee0f 100644 --- a/lib/import_map.ts +++ b/lib/import_map.ts @@ -36,21 +36,21 @@ const isImportMapReferrer = is.ObjectOf({ // This implementation is ridiculously inefficient, but we prefer not to reimplement the whole // import_map module. Maybe we should rathre patch rust code of the import_map module. -async function readFromJson(url: URL): Promise> { - const data = await Deno.readTextFile(url.pathname); +async function readFromJson(specifier: URI<"file">): Promise> { + const data = await Deno.readTextFile(URI.relative(specifier)); if (data.length === 0) return; const json = parseJsonc(data); if (isImportMapReferrer(json)) { // The json seems to be deno.json or deno.jsonc referencing an import map. - return readFromJson(new URL(json.importMap, url)); + return readFromJson(URI.from(new URL(json.importMap, specifier).href)); } if (!isImportMapJson(json)) { // The json does not include an import map. return undefined; } - const inner = await parseFromJson(url, json); + const inner = await parseFromJson(specifier, json); return { - specifier: URI.from(url.href), + specifier, resolve(specifier, referrer) { const resolved = inner.resolve(specifier, referrer); if (resolved === specifier) { diff --git a/lib/import_map_test.ts b/lib/import_map_test.ts index 46dbd4ab..65fae968 100644 --- a/lib/import_map_test.ts +++ b/lib/import_map_test.ts @@ -11,21 +11,25 @@ describe("readFromJson()", () => { // cleanup.defer(async () => { // await Deno.remove(f); // }); - const importMap = await ImportMap.readFromJson(new URL(f, import.meta.url)); + const importMap = await ImportMap.readFromJson(URI.from(f)); assertEquals(importMap, undefined); await Deno.remove(f); }); it("test/fixtures/import-map/deno.json", async () => { const importMap = await ImportMap.readFromJson( - new URL("../test/fixtures/import-map/deno.json", import.meta.url), + URI.from( + new URL("../test/fixtures/import-map/deno.json", import.meta.url), + ), ); assertExists(importMap); }); it("test/fixtures/import-map-referred/import_map.json", async () => { const importMap = await ImportMap.readFromJson( - new URL( - "../test/fixtures/import-map-referred/deno.json", - import.meta.url, + URI.from( + new URL( + "../test/fixtures/import-map-referred/deno.json", + import.meta.url, + ), ), ); assertExists(importMap); @@ -39,10 +43,12 @@ describe("readFromJson()", () => { describe("resolve()", () => { it("resolve specifiers in import maps", async () => { const importMap = await ImportMap.readFromJson( - new URL("../test/fixtures/import-map/deno.json", import.meta.url), + URI.from( + new URL("../test/fixtures/import-map/deno.json", import.meta.url), + ), ); assertExists(importMap); - const referrer = URI.from("test/fixtures/import-map/mod.ts"); + const referrer = URI.from("./test/fixtures/import-map/mod.ts"); assertEquals( importMap.resolve("std/version.ts", referrer), { @@ -70,19 +76,21 @@ describe("resolve()", () => { assertEquals( importMap.resolve("/lib.ts", referrer), { - specifier: URI.from("test/fixtures/import-map/lib.ts"), + specifier: URI.from("./test/fixtures/import-map/lib.ts"), }, ); }); it("do not resolve an url", async () => { const importMap = await ImportMap.readFromJson( - new URL( - "../test/fixtures/import-map-no-resolve/deno.json", - import.meta.url, + URI.from( + new URL( + "../test/fixtures/import-map-no-resolve/deno.json", + import.meta.url, + ), ), ); assertExists(importMap); - const referrer = URI.from("test/fixtures/import-map-no-resolve/deps.ts"); + const referrer = URI.from("./test/fixtures/import-map-no-resolve/deps.ts"); assertEquals( importMap.resolve( "https://deno.land/std@0.171.0/testing/asserts.ts", @@ -93,9 +101,11 @@ describe("resolve()", () => { }); it("resolve specifiers in a referred import map", async () => { const importMap = await ImportMap.readFromJson( - new URL( - "../test/fixtures/import-map-referred/deno.json", - import.meta.url, + URI.from( + new URL( + "../test/fixtures/import-map-referred/deno.json", + import.meta.url, + ), ), ); assertExists(importMap); @@ -115,7 +125,9 @@ describe("resolveSimple()", () => { let importMap: ImportMap; beforeAll(async () => { const maybe = await ImportMap.readFromJson( - new URL("../test/fixtures/import-map/deno.json", import.meta.url), + URI.from( + new URL("../test/fixtures/import-map/deno.json", import.meta.url), + ), ); assertExists(maybe); importMap = maybe; diff --git a/lib/update.ts b/lib/update.ts index 48ef603c..a70563ce 100644 --- a/lib/update.ts +++ b/lib/update.ts @@ -74,7 +74,7 @@ export async function collect( await DenoGraph.ensureInit(); const importMap = options.importMap - ? await ImportMap.readFromJson(new URL(URI.from(options.importMap))) + ? await ImportMap.readFromJson(URI.from(options.importMap)) : undefined; const graph = await createGraph(specifiers, { diff --git a/lib/update_test.ts b/lib/update_test.ts index 4860e9ee..e1862cfc 100644 --- a/lib/update_test.ts +++ b/lib/update_test.ts @@ -56,7 +56,9 @@ describe("_create - with import map", () => { let importMap: ImportMap; beforeAll(async () => { importMap = (await ImportMap.readFromJson( - new URL("../test/fixtures/import-map/deno.json", import.meta.url), + URI.from( + new URL("../test/fixtures/import-map/deno.json", import.meta.url), + ), ))!; }); it("std/version.ts", async () => { diff --git a/lib/uri.ts b/lib/uri.ts index 29d79910..5a32140a 100644 --- a/lib/uri.ts +++ b/lib/uri.ts @@ -1,4 +1,5 @@ import { isAbsolute, relative, resolve, toFileUrl } from "./std/path.ts"; +import { assert, is } from "./x/unknownutil.ts"; import { Brand } from "./types.ts"; export type DefaultProtocol = Scheme extends @@ -15,14 +16,19 @@ export type RelativePath = Brand; export type AbsolutePath = Brand; export const URI = { - from(path: string): URI<"file"> { + /** + * Convert a path to a file URL. If the path is relative, it is resolved from the current + * working directory. + */ + from(path: string | URL): URI<"file"> { let url: URL; try { url = new URL(path); } catch { - return toFileUrl( + assert(path, is.String); + url = toFileUrl( isAbsolute(path) ? path : resolve(path), - ).href as URI<"file">; + ); } if (url.protocol !== "file:") { throw new TypeError(`Invalid protocol: ${url.protocol} in ${path}`); diff --git a/lib/x/unknownutil.ts b/lib/x/unknownutil.ts index 894bcaff..dfddb9a6 100644 --- a/lib/x/unknownutil.ts +++ b/lib/x/unknownutil.ts @@ -1 +1 @@ -export { is } from "https://deno.land/x/unknownutil@v3.10.0/mod.ts"; +export { assert, is } from "https://deno.land/x/unknownutil@v3.10.0/mod.ts";