std/_tools/utils.ts
Yoshiya Hinosawa 26d6d2b510
chore: use new format for workspaces and re-enable using Deno canary and v1.x (#5375)
* chore: use new format for workspaces

* fixes

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
2024-07-11 10:24:30 +10:00

37 lines
1.2 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
const workspaces = JSON.parse(await Deno.readTextFile("deno.json"))
.workspace as string[];
// deno-lint-ignore no-explicit-any
const denoConfig = {} as Record<string, any>;
for (const workspace of workspaces) {
const { default: config } = await import("../" + workspace + "/deno.json", {
with: { type: "json" },
});
denoConfig[config.name.replace("@std/", "")] = config;
}
export function resolveWorkspaceSpecifiers(
specifier: string,
referrer: string,
) {
if (specifier.startsWith("../") || specifier.startsWith("./")) {
return new URL(specifier, referrer).href;
} else if (specifier.startsWith("@std/")) {
let [_std, pkg, exp] = specifier.split("/");
if (exp === undefined) {
exp = ".";
} else {
exp = "./" + exp;
}
const pkgPath = "../" + pkg!.replaceAll("-", "_") + "/";
const config = denoConfig[pkg!];
if (typeof config.exports === "string") {
return new URL(pkgPath + config.exports, import.meta.url).href;
}
return new URL(pkgPath + config.exports[exp], import.meta.url).href;
} else {
return new URL(specifier).href;
}
}