Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2a46372

Browse files
committedOct 6, 2024·
feat: use confbox to preserve json style
1 parent 2a26f60 commit 2a46372

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed
 

‎package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
},
3535
"dependencies": {
3636
"citty": "^0.1.6",
37+
"confbox": "^0.1.7",
3738
"consola": "^3.2.3",
3839
"execa": "^9.4.0",
3940
"giget": "^1.2.3",

‎pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/utils/json.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parseJSON, stringifyJSON } from "confbox";
12
import { read, update, write } from "./fs";
23

34
// biome-ignore lint: lint/suspicious/noConfusingVoidType
@@ -10,7 +11,7 @@ type JSON = any;
1011
*/
1112
export async function readJSON(path: string): Promise<JSON | undefined> {
1213
const contents = await read(path);
13-
return contents ? JSON.parse(contents) : undefined;
14+
return contents ? parseJSON(contents) : undefined;
1415
}
1516

1617
/**
@@ -23,7 +24,7 @@ export async function writeJSON(
2324
json: Record<string, unknown>,
2425
opts?: Parameters<typeof write>[2],
2526
): Promise<void> {
26-
await write(path, JSON.stringify(json, undefined, 2), opts);
27+
await write(path, stringifyJSON(json), opts);
2728
}
2829

2930
/**
@@ -38,9 +39,9 @@ export async function updateJSON<T extends JSON>(
3839
): Promise<T | undefined> {
3940
let updated: T | undefined;
4041
await update(path, async (existing) => {
41-
const json = JSON.parse(existing || "{}");
42+
const json = parseJSON<T>(existing || `{\n \n}\n`);
4243
updated = (await updater(json)) || json;
43-
return JSON.stringify(updated, undefined, 2);
44+
return stringifyJSON(updated);
4445
});
4546
return updated;
4647
}

0 commit comments

Comments
 (0)
Please sign in to comment.