diff --git a/src/dev/deps.ts b/src/dev/deps.ts index 388f7258..dee7ad76 100644 --- a/src/dev/deps.ts +++ b/src/dev/deps.ts @@ -13,6 +13,7 @@ export { export { walk, WalkError } from "https://deno.land/std@0.190.0/fs/walk.ts"; export { parse } from "https://deno.land/std@0.190.0/flags/mod.ts"; export { gte } from "https://deno.land/std@0.190.0/semver/mod.ts"; +export { existsSync } from "https://deno.land/std@0.190.0/fs/mod.ts"; // ts-morph export { Node, Project } from "https://deno.land/x/ts_morph@17.0.1/mod.ts"; diff --git a/tests/cli_test.ts b/tests/cli_test.ts index 97d570b4..8e883473 100644 --- a/tests/cli_test.ts +++ b/tests/cli_test.ts @@ -347,33 +347,34 @@ Deno.test("fresh-update", async function fn(t) { await cliProcess.output(); await t.step("execute update command", async () => { - const cliProcess = new Deno.Command(Deno.execPath(), { - args: [ - "run", - "-A", - path.join(Deno.cwd(), "update.ts"), - ".", - ], - cwd: tmpDirName, - stdin: "null", - stdout: "piped", - }); - - const { code, stdout } = await cliProcess.output(); - const output = new TextDecoder().decode(stdout); - - assertMatch( - output, + await updateAndVerify( /The manifest has been generated for \d+ routes and \d+ islands./, ); - assertEquals(code, 0); + }); + + await t.step("execute update command deno.jsonc support", async () => { + try { + Deno.renameSync(`${tmpDirName}/deno.json`, `${tmpDirName}/deno.jsonc`); + await updateAndVerify( + /The manifest has been generated for \d+ routes and \d+ islands./, + ); + } finally { + Deno.renameSync(`${tmpDirName}/deno.jsonc`, `${tmpDirName}/deno.json`); + } }); await t.step("execute update command (no islands directory)", async () => { await retry(() => Deno.remove(path.join(tmpDirName, "islands"), { recursive: true }) ); + await updateAndVerify( + /The manifest has been generated for \d+ routes and 0 islands./, + ); + }); + await retry(() => Deno.remove(tmpDirName, { recursive: true })); + + async function updateAndVerify(expected: RegExp) { const cliProcess = new Deno.Command(Deno.execPath(), { args: [ "run", @@ -391,10 +392,8 @@ Deno.test("fresh-update", async function fn(t) { assertMatch( output, - /The manifest has been generated for \d+ routes and 0 islands./, + expected, ); assertEquals(code, 0); - }); - - await retry(() => Deno.remove(tmpDirName, { recursive: true })); + } }); diff --git a/update.ts b/update.ts index 658b7a99..c5b4234b 100644 --- a/update.ts +++ b/update.ts @@ -1,4 +1,11 @@ -import { join, Node, parse, Project, resolve } from "./src/dev/deps.ts"; +import { + existsSync, + join, + Node, + parse, + Project, + resolve, +} from "./src/dev/deps.ts"; import { error } from "./src/dev/error.ts"; import { freshImports, twindImports } from "./src/dev/imports.ts"; import { collect, ensureMinDenoVersion, generate } from "./src/dev/mod.ts"; @@ -27,9 +34,17 @@ const unresolvedDirectory = Deno.args[0]; const resolvedDirectory = resolve(unresolvedDirectory); // Update dependencies in the import map. The import map can either be embedded -// in a deno.json file or be in a separate JSON file referenced with the +// in a deno.json (or .jsonc) file or be in a separate JSON file referenced with the // `importMap` key in deno.json. -const DENO_JSON_PATH = join(resolvedDirectory, "deno.json"); +const fileNames = ["deno.json", "deno.jsonc"]; +const DENO_JSON_PATH = fileNames + .map((fileName) => join(resolvedDirectory, fileName)) + .find((path) => existsSync(path)); +if (!DENO_JSON_PATH) { + throw new Error( + `Neither deno.json nor deno.jsonc could be found in ${resolvedDirectory}`, + ); +} let denoJsonText = await Deno.readTextFile(DENO_JSON_PATH); let denoJson = JSON.parse(denoJsonText); if (denoJson.importMap) {