Skip to content

Commit

Permalink
refactor(import_map): use URI for the argument of readFromJson()
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Oct 23, 2023
1 parent e6e5d72 commit 63fafe8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 27 deletions.
10 changes: 5 additions & 5 deletions lib/import_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Maybe<ImportMap>> {
const data = await Deno.readTextFile(url.pathname);
async function readFromJson(specifier: URI<"file">): Promise<Maybe<ImportMap>> {
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) {
Expand Down
44 changes: 28 additions & 16 deletions lib/import_map_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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),
{
Expand Down Expand Up @@ -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/[email protected]/testing/asserts.ts",
Expand All @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
4 changes: 3 additions & 1 deletion lib/update_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
12 changes: 9 additions & 3 deletions lib/uri.ts
Original file line number Diff line number Diff line change
@@ -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 string> = Scheme extends
Expand All @@ -15,14 +16,19 @@ export type RelativePath = Brand<string, "RelativePath">;
export type AbsolutePath = Brand<string, "AbsolutePath">;

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}`);
Expand Down
2 changes: 1 addition & 1 deletion lib/x/unknownutil.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { is } from "https://deno.land/x/[email protected]/mod.ts";
export { assert, is } from "https://deno.land/x/[email protected]/mod.ts";

0 comments on commit 63fafe8

Please sign in to comment.