Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Deno CLI #742

Merged
merged 3 commits into from
Oct 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions CLI.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const stableBuild = new Set([
]);

let imFilename = "import_map.json";
let indentWidth = 2;

async function add(args: string[], options: Record<string, string>) {
if (options.alias && args.length > 1) {
Expand Down Expand Up @@ -170,18 +169,31 @@ async function init(_args: string[], _options: Record<string, string>) {
if (!isNEString(config.importMap)) {
config.importMap = imFilename;
}
if (config.importMap === "deno.json") {
delete config.importMap;
}
const tasks = config.tasks as Record<string, string> | undefined;
config.tasks = {
...tasks,
"esm:add": `deno run -A ${importUrl.origin}/${VERSION} add`,
"esm:update": `deno run -A ${importUrl.origin}/${VERSION} update`,
"esm:remove": `deno run -A ${importUrl.origin}/${VERSION} remove`,
};
await Deno.writeTextFile(
"deno.json",
JSON.stringify(config, null, indentWidth),
);
await saveImportMap(importMap);
if (imFilename === "deno.json") {
await saveImportMap({
...config,
...importMap
});
}
else {
await Deno.writeTextFile(
"deno.json",
await denoFmt(
JSON.stringify(config)
)
);
await saveImportMap(importMap);
}
console.log("Initialized %cdeno.json%c, 3 task added:", "color:green", "");
console.log(
" - %cdeno task esm:add%c [packages...]",
Expand Down Expand Up @@ -259,12 +271,36 @@ async function fetchPkgInfo(query: string): Promise<Package | null> {
return pkg;
}

// https://github.com/denoland/fresh/blob/main/src/dev/mod.ts#L154-L170
async function denoFmt(code: string, ext = "json") {
const proc = new Deno.Command(Deno.execPath(), {
args: ["fmt", "--ext", ext, "-"],
stdin: "piped",
stdout: "piped",
stderr: "null",
}).spawn();

const raw = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(code));
controller.close();
},
});
await raw.pipeTo(proc.stdin);
const { stdout } = await proc.output();

const formattedStr = new TextDecoder().decode(stdout);
return formattedStr;
}

async function loadImportMap(): Promise<ImportMap> {
const importMap: ImportMap = { imports: {}, scopes: {} };
let importMap: ImportMap = { imports: {}, scopes: {} };
try {
const raw = (await Deno.readTextFile(imFilename)).trim();
if (raw.startsWith("{") && raw.endsWith("}")) {
const { imports, scopes } = JSON.parse(raw);
const parsed = JSON.parse(raw);
const { imports, scopes } = parsed;
importMap = { ...parsed };
if (imports) {
Object.assign(importMap.imports, imports);
}
Expand Down Expand Up @@ -304,10 +340,8 @@ async function saveImportMap(importMap: ImportMap): Promise<void> {
// write
await Deno.writeTextFile(
imFilename,
JSON.stringify(
{ imports: sortedImports, scopes: sortedScopes },
null,
indentWidth,
await denoFmt(
JSON.stringify({ ...importMap, imports: sortedImports, scopes: sortedScopes })
),
);
}
Expand Down Expand Up @@ -468,8 +502,8 @@ if (import.meta.main) {
if (isNEString(config.importMap)) {
imFilename = config.importMap;
}
if (typeof config?.fmt?.options?.indentWidth === "number") {
indentWidth = config.fmt.options.indentWidth;
else {
imFilename = "deno.json";
}
await commands[command as keyof typeof commands](...parseFlags(args));
console.log(`✨ Done in ${(performance.now() - start).toFixed(2)}ms`);
Expand Down