Skip to content

Commit

Permalink
Update deno CLI script
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Mar 1, 2023
1 parent c82f085 commit 208a7de
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions server/embed/deno_cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ const VERSION = /^\/v\d+$/.test(importUrl.pathname)
? importUrl.pathname.slice(1)
: "v{VERSION}";

let importMapFilepath = "import_map.json";
// stable build for UI libraries like react, to make sure the runtime is single copy
const stableBuild = new Set([
"react",
"preact",
"vue",
]);

let imFilename = "import_map.json";

async function add(args: string[], options: Record<string, string>) {
if (options.alias && args.length > 1) {
Expand All @@ -43,9 +50,7 @@ async function add(args: string[], options: Record<string, string>) {
);
await saveImportMap(importMap);
console.log(
`Added ${pkgs.length} packages to %c${
importMapFilepath.split(/[\/\\]/g).pop()
}`,
`Added ${pkgs.length} packages to %c${imFilename.split(/[\/\\]/g).pop()}`,
"color:blue",
);
if (pkgs.length > 0) {
Expand Down Expand Up @@ -158,7 +163,7 @@ async function init(args: string[], options: Record<string, string>) {
const config = await getDenoConfig();
const importMap = await loadImportMap();
if (!isNEString(config.importMap)) {
config.importMap = importMapFilepath;
config.importMap = imFilename;
}
const tasks = config.tasks as undefined | Record<string, string>;
config.tasks = {
Expand Down Expand Up @@ -251,7 +256,7 @@ async function fetchPkgInfo(query: string): Promise<Package | null> {
async function loadImportMap(): Promise<ImportMap> {
const importMap: ImportMap = { imports: {}, scopes: {} };
try {
const raw = (await Deno.readTextFile(importMapFilepath)).trim();
const raw = (await Deno.readTextFile(imFilename)).trim();
if (raw.startsWith("{") && raw.endsWith("}")) {
const { imports, scopes } = JSON.parse(raw);
if (imports) {
Expand Down Expand Up @@ -292,7 +297,7 @@ async function saveImportMap(importMap: ImportMap): Promise<void> {

// write
await Deno.writeTextFile(
importMapFilepath,
imFilename,
JSON.stringify({ imports: sortedImports, scopes: sortedScopes }, null, 2),
);
}
Expand Down Expand Up @@ -352,10 +357,11 @@ function getPkgUrl(pkg: Package): [url: string, withExports: boolean] {
Object.keys(exports).some((key) =>
key.length >= 3 && key.startsWith("./") && key !== "./package.json"
);

if (
(dependencies && Object.keys(dependencies).length > 0) ||
(peerDependencies && Object.keys(peerDependencies).length > 0)
!stableBuild.has(name) && (
(dependencies && Object.keys(dependencies).length > 0) ||
(peerDependencies && Object.keys(peerDependencies).length > 0)
)
) {
return [`${importUrl.origin}/${VERSION}/*${name}@${version}`, withExports];
}
Expand Down Expand Up @@ -446,7 +452,7 @@ if (import.meta.main) {
try {
const config = await getDenoConfig();
if (isNEString(config.importMap)) {
importMapFilepath = config.importMap;
imFilename = config.importMap;
}
await commands[command as keyof typeof commands](...parseFlags(args));
console.log(`✨ Done in ${(performance.now() - start).toFixed(2)}ms`);
Expand Down

0 comments on commit 208a7de

Please sign in to comment.