From 018b97029efdfb92dac34e70919480567dad7d11 Mon Sep 17 00:00:00 2001 From: hasundue Date: Wed, 31 Jul 2024 09:23:39 +0900 Subject: [PATCH 1/2] fix: comments are removed from JSONC files --- core/refs.ts | 25 +++++++++++++++++-------- core/refs_test.ts | 5 ++++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/core/refs.ts b/core/refs.ts index d8893856..406e514d 100644 --- a/core/refs.ts +++ b/core/refs.ts @@ -2,7 +2,7 @@ import { init, parseModule } from "@deno/graph"; import type { DependencyJson } from "@deno/graph/types"; import { toUrl } from "@molt/lib/path"; import { distinct } from "@std/collections"; -import { detect, EOL, format } from "@std/fs/eol"; +import { detect, EOL } from "@std/fs/eol"; import { extname } from "@std/path"; import { type DependencySpec, parse, stringify, tryParse } from "./specs.ts"; import { parseImportMapJson, readImportMapJson } from "./maps.ts"; @@ -157,6 +157,8 @@ function rewriteEsModule( return lines.join(eol); } +// This implementation is not quite efficient nor 100% robust, but we don't +// have a good way to deal with JSONC within the Deno ecosystem yet. function rewriteImportMap( ref: DependencyRef<"import_map">, updated: DependencySpec, @@ -164,14 +166,21 @@ function rewriteImportMap( ) { const src = ref.source; const json = parseImportMapJson(content); - if (src.scope) { - json.scopes![src.scope][src.key] = stringify(updated); - } else { - json.imports![src.key] = stringify(updated); - } - const str = JSON.stringify(json, null, 2); + + const key = src.scope + ? json.scopes![src.scope][src.key] + : json.imports![src.key]; + const eol = detect(content) ?? EOL; - return format(str, eol) + eol; + const lines = content.split(eol); + + const outdated = stringify(ref.dependency); + const index = lines.findIndex( + (line) => line.includes(key) && line.includes(outdated), + ); + lines[index] = lines[index].replace(outdated, stringify(updated)); + + return lines.join(eol); } /** Options for committing the updated dependency. */ diff --git a/core/refs_test.ts b/core/refs_test.ts index 2fc326a2..991969cf 100644 --- a/core/refs_test.ts +++ b/core/refs_test.ts @@ -242,11 +242,12 @@ describe("rewrite", () => { ); }); - it("should rewrite dependencies in an import map", async () => { + it("should rewrite dependencies in deno.jsonc", async () => { await Deno.writeTextFile( "a.json", dedent` { + // dependencies "imports": { "@std/assert": "jsr:@std/assert@^0.222.0", "@std/testing/bdd": "jsr:@std/testing@^0.222.0/bdd" @@ -260,6 +261,7 @@ describe("rewrite", () => { await Deno.readTextFile("a.json"), dedent` { + // dependencies "imports": { "@std/assert": "jsr:@std/assert@0.224.0", "@std/testing/bdd": "jsr:@std/testing@^0.222.0/bdd" @@ -272,6 +274,7 @@ describe("rewrite", () => { await Deno.readTextFile("a.json"), dedent` { + // dependencies "imports": { "@std/assert": "jsr:@std/assert@0.224.0", "@std/testing/bdd": "jsr:@std/testing@0.224.0/bdd" From dd2f6f155e071d73490a6ad4df82cede288167a7 Mon Sep 17 00:00:00 2001 From: hasundue Date: Wed, 31 Jul 2024 09:24:14 +0900 Subject: [PATCH 2/2] chore: release 0.19.1 --- cli/deno.json | 2 +- core/deno.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/deno.json b/cli/deno.json index 9c26ebc4..d318d601 100644 --- a/cli/deno.json +++ b/cli/deno.json @@ -1,6 +1,6 @@ { "name": "@molt/cli", - "version": "0.19.0", + "version": "0.19.1", "exports": { ".": "./main.ts" }, diff --git a/core/deno.json b/core/deno.json index ba79776b..f700f61b 100644 --- a/core/deno.json +++ b/core/deno.json @@ -1,6 +1,6 @@ { "name": "@molt/core", - "version": "0.19.0", + "version": "0.19.1", "exports": { ".": "./mod.ts", "./bumps": "./bumps.ts",