fix(update): update script actually supports comments in jsonc (#1591)
This commit is contained in:
@@ -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 { 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 { 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 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
|
// ts-morph
|
||||||
export { Node, Project } from "https://deno.land/x/ts_morph@17.0.1/mod.ts";
|
export { Node, Project } from "https://deno.land/x/ts_morph@17.0.1/mod.ts";
|
||||||
|
|||||||
+12
-2
@@ -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 () => {
|
await t.step("execute update command deno.jsonc support", async () => {
|
||||||
try {
|
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(
|
await updateAndVerify(
|
||||||
/The manifest has been generated for \d+ routes and \d+ islands./,
|
/The manifest has been generated for \d+ routes and \d+ islands./,
|
||||||
);
|
);
|
||||||
} finally {
|
} 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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import {
|
import {
|
||||||
dirname,
|
dirname,
|
||||||
existsSync,
|
existsSync,
|
||||||
|
extname,
|
||||||
join,
|
join,
|
||||||
|
JSONC,
|
||||||
Node,
|
Node,
|
||||||
parse,
|
parse,
|
||||||
Project,
|
Project,
|
||||||
@@ -49,7 +51,15 @@ if (!DENO_JSON_PATH) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
let denoJsonText = await Deno.readTextFile(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) {
|
if (denoJson.importMap) {
|
||||||
const IMPORT_MAP_PATH = join(resolvedDirectory, denoJson.importMap);
|
const IMPORT_MAP_PATH = join(resolvedDirectory, denoJson.importMap);
|
||||||
const importMapText = await Deno.readTextFile(IMPORT_MAP_PATH);
|
const importMapText = await Deno.readTextFile(IMPORT_MAP_PATH);
|
||||||
|
|||||||
Reference in New Issue
Block a user