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

fix: comments are removed from JSONC files #208

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion cli/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@molt/cli",
"version": "0.19.0",
"version": "0.19.1",
"exports": {
".": "./main.ts"
},
Expand Down
2 changes: 1 addition & 1 deletion core/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@molt/core",
"version": "0.19.0",
"version": "0.19.1",
"exports": {
".": "./mod.ts",
"./bumps": "./bumps.ts",
Expand Down
25 changes: 17 additions & 8 deletions core/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -157,21 +157,30 @@ 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,
content: string,
) {
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. */
Expand Down
5 changes: 4 additions & 1 deletion core/refs_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down