-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.ts
More file actions
173 lines (151 loc) · 4.64 KB
/
Copy pathjson.ts
File metadata and controls
173 lines (151 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Utilities for reading and writing NpgsqlRest JSONC config files.
* These files have // comment headers before the JSON body.
*/
/**
* Separate the // comment header from the JSON body.
* Header = all leading lines that are empty or start with //.
*/
export function splitHeaderAndJson(text: string): { header: string; json: string } {
const lines = text.split("\n");
let headerEnd = 0;
for (let i = 0; i < lines.length; i++) {
const trimmed = lines[i].trim();
if (trimmed === "" || trimmed.startsWith("//")) {
headerEnd = i + 1;
} else {
break;
}
}
const header = lines.slice(0, headerEnd).join("\n");
const json = lines.slice(headerEnd).join("\n");
return { header: header ? header + "\n" : "", json };
}
/**
* Strip // and block comments from JSON text (respects string literals).
*/
export function stripJsonComments(text: string): string {
let result = "";
let i = 0;
const len = text.length;
while (i < len) {
// String literal
if (text[i] === '"') {
result += '"';
i++;
while (i < len && text[i] !== '"') {
if (text[i] === "\\") {
result += text[i] + (text[i + 1] ?? "");
i += 2;
} else {
result += text[i];
i++;
}
}
if (i < len) {
result += '"';
i++;
}
continue;
}
// Line comment
if (text[i] === "/" && text[i + 1] === "/") {
while (i < len && text[i] !== "\n") i++;
continue;
}
// Block comment
if (text[i] === "/" && text[i + 1] === "*") {
i += 2;
while (i < len && !(text[i] === "*" && text[i + 1] === "/")) i++;
i += 2;
continue;
}
result += text[i];
i++;
}
return result;
}
/**
* Read a JSONC config file. Returns parsed data and the original header,
* or null if the file doesn't exist.
*/
export async function readJsonConfig(
path: string,
): Promise<{ data: Record<string, unknown>; header: string } | null> {
const file = Bun.file(path);
if (!(await file.exists())) return null;
const text = await file.text();
const { header, json } = splitHeaderAndJson(text);
const clean = stripJsonComments(json);
const data = JSON.parse(clean) as Record<string, unknown>;
return { data, header };
}
/**
* Serialize a JSON value to JSONC with // comments above keys that have descriptions.
* Comments use the NpgsqlRest convention: // wrapped above and below the description text.
*/
function serializeValue(
value: unknown,
descriptions: Record<string, string>,
depth: number,
path: string,
): string {
if (value === null) return "null";
if (typeof value === "boolean" || typeof value === "number") return String(value);
if (typeof value === "string") return JSON.stringify(value);
if (Array.isArray(value)) {
if (value.length === 0) return "[]";
const pad = " ".repeat(depth + 1);
const outer = " ".repeat(depth);
const items = value.map((v) =>
`${pad}${serializeValue(v, descriptions, depth + 1, path)}`,
);
return `[\n${items.join(",\n")}\n${outer}]`;
}
if (typeof value === "object" && value !== null) {
const entries = Object.entries(value as Record<string, unknown>);
if (entries.length === 0) return "{}";
const pad = " ".repeat(depth + 1);
const outer = " ".repeat(depth);
const lines: string[] = [];
for (let i = 0; i < entries.length; i++) {
const [key, val] = entries[i];
const keyPath = path ? `${path}.${key}` : key;
const desc = descriptions[keyPath];
const comma = i < entries.length - 1 ? "," : "";
if (desc) {
lines.push(`${pad}//`);
for (const line of desc.split("\n")) {
lines.push(`${pad}// ${line}`);
}
lines.push(`${pad}//`);
}
const valStr = serializeValue(val, descriptions, depth + 1, keyPath);
lines.push(`${pad}${JSON.stringify(key)}: ${valStr}${comma}`);
}
return `{\n${lines.join("\n")}\n${outer}}`;
}
return JSON.stringify(value);
}
export function serializeJsonc(
data: Record<string, unknown>,
descriptions: Record<string, string>,
): string {
return serializeValue(data, descriptions, 0, "") + "\n";
}
/**
* Write a JSON config file, prepending the original comment header.
* If descriptions are provided, writes JSONC with inline // comments.
* Pretty-prints with 2-space indentation.
*/
export async function writeJsonConfig(
path: string,
data: Record<string, unknown>,
header: string,
descriptions?: Record<string, string>,
): Promise<void> {
const body = descriptions
? serializeJsonc(data, descriptions)
: JSON.stringify(data, null, 2) + "\n";
await Bun.write(path, header + body);
}