From 6d5428eeaaa31f7c210c6f0bcd55910d0a9d1b6d Mon Sep 17 00:00:00 2001 From: Reed von Redwitz Date: Fri, 4 Aug 2023 15:22:08 +0200 Subject: [PATCH] fix(update): update script actually supports comments in jsonc (#1591) --- src/dev/deps.ts | 1 + tests/cli_test.ts | 14 ++++++++++++-- update.ts | 12 +++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/dev/deps.ts b/src/dev/deps.ts index 92bf2cc1..73419b53 100644 --- a/src/dev/deps.ts +++ b/src/dev/deps.ts @@ -18,6 +18,7 @@ export { parse } from "https://deno.land/std@0.193.0/flags/mod.ts"; export { gte } from "https://deno.land/std@0.193.0/semver/mod.ts"; export { existsSync } from "https://deno.land/std@0.193.0/fs/mod.ts"; export * as semver from "https://deno.land/std@0.195.0/semver/mod.ts"; +export * as JSONC from "https://deno.land/std@0.195.0/jsonc/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 20ef25a1..ea068472 100644 --- a/tests/cli_test.ts +++ b/tests/cli_test.ts @@ -356,14 +356,24 @@ Deno.test("fresh-update", async function fn(t) { ); }); + const comment = "// This is a test comment"; + const regex = /("preact": "https:\/\/esm.sh\/preact@[\d.]+",\n)/; + const originalName = `${tmpDirName}/deno.json`; + const updatedName = `${originalName}c`; await t.step("execute update command deno.jsonc support", async () => { try { - Deno.renameSync(`${tmpDirName}/deno.json`, `${tmpDirName}/deno.jsonc`); + Deno.renameSync(originalName, updatedName); + let denoJsonText = await Deno.readTextFile(updatedName); + denoJsonText = denoJsonText.replace(regex, `$1${comment}\n`); + await Deno.writeTextFile(updatedName, denoJsonText); await updateAndVerify( /The manifest has been generated for \d+ routes and \d+ islands./, ); } finally { - Deno.renameSync(`${tmpDirName}/deno.jsonc`, `${tmpDirName}/deno.json`); + let denoJsonText = await Deno.readTextFile(updatedName); + denoJsonText = denoJsonText.replace(new RegExp(`\n${comment}\n`), "\n"); + await Deno.writeTextFile(updatedName, denoJsonText); + Deno.renameSync(updatedName, originalName); } }); diff --git a/update.ts b/update.ts index c13506f0..c8ab0501 100644 --- a/update.ts +++ b/update.ts @@ -1,7 +1,9 @@ import { dirname, existsSync, + extname, join, + JSONC, Node, parse, Project, @@ -49,7 +51,15 @@ if (!DENO_JSON_PATH) { ); } let denoJsonText = await Deno.readTextFile(DENO_JSON_PATH); -let denoJson = JSON.parse(denoJsonText); +const ext = extname(DENO_JSON_PATH); +let denoJson; +if (ext === ".json") { + denoJson = JSON.parse(denoJsonText); +} else if (ext === ".jsonc") { + denoJson = JSONC.parse(denoJsonText); +} else { + throw new Error(`Unsupported file extension: ${ext}`); +} if (denoJson.importMap) { const IMPORT_MAP_PATH = join(resolvedDirectory, denoJson.importMap); const importMapText = await Deno.readTextFile(IMPORT_MAP_PATH);