@beignet/cli 0.0.6 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +40 -3
- package/dist/inspect.d.ts.map +1 -1
- package/dist/inspect.js +404 -44
- package/dist/inspect.js.map +1 -1
- package/dist/make.d.ts.map +1 -1
- package/dist/make.js +267 -96
- package/dist/make.js.map +1 -1
- package/dist/registry-edits.d.ts +77 -0
- package/dist/registry-edits.d.ts.map +1 -0
- package/dist/registry-edits.js +255 -0
- package/dist/registry-edits.js.map +1 -0
- package/package.json +2 -2
- package/src/inspect.ts +648 -58
- package/src/make.ts +433 -121
- package/src/registry-edits.ts +352 -0
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared string-edit helpers for registry files.
|
|
3
|
+
*
|
|
4
|
+
* `beignet make` generators and `beignet doctor --fix` both append entries to
|
|
5
|
+
* app-owned registry files such as `server/schedules.ts`, `server/tasks.ts`,
|
|
6
|
+
* and `server/outbox.ts`. Keeping the import insertion, array append, and
|
|
7
|
+
* membership primitives in one module keeps both write paths byte-compatible.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Source slice for an initializer expression, with absolute offsets.
|
|
12
|
+
*/
|
|
13
|
+
export type InitializerInfo = {
|
|
14
|
+
text: string;
|
|
15
|
+
start: number;
|
|
16
|
+
end: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Result of appending an entry to a named registry.
|
|
21
|
+
*
|
|
22
|
+
* `missing` means the registry anchor could not be found or is not an array
|
|
23
|
+
* literal; callers should bail without writing.
|
|
24
|
+
*/
|
|
25
|
+
export type AppendResult =
|
|
26
|
+
| { kind: "updated"; source: string }
|
|
27
|
+
| { kind: "unchanged" }
|
|
28
|
+
| { kind: "missing" };
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Find the index of the delimiter closing the one at `openIndex`, skipping
|
|
32
|
+
* string literals.
|
|
33
|
+
*/
|
|
34
|
+
export function matchingDelimiterIndex(
|
|
35
|
+
source: string,
|
|
36
|
+
openIndex: number,
|
|
37
|
+
open: string,
|
|
38
|
+
close: string,
|
|
39
|
+
): number {
|
|
40
|
+
let depth = 0;
|
|
41
|
+
let inString: string | undefined;
|
|
42
|
+
let escaped = false;
|
|
43
|
+
|
|
44
|
+
for (let index = openIndex; index < source.length; index++) {
|
|
45
|
+
const char = source[index];
|
|
46
|
+
if (inString) {
|
|
47
|
+
if (escaped) {
|
|
48
|
+
escaped = false;
|
|
49
|
+
} else if (char === "\\") {
|
|
50
|
+
escaped = true;
|
|
51
|
+
} else if (char === inString) {
|
|
52
|
+
inString = undefined;
|
|
53
|
+
}
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
58
|
+
inString = char;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (char === open) {
|
|
63
|
+
depth++;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (char === close) {
|
|
68
|
+
depth--;
|
|
69
|
+
if (depth === 0) return index;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return -1;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Insert a line directly after the last top-level import statement.
|
|
78
|
+
*/
|
|
79
|
+
export function insertAfterImports(source: string, line: string): string {
|
|
80
|
+
const lines = source.split("\n");
|
|
81
|
+
let lastImportIndex = -1;
|
|
82
|
+
|
|
83
|
+
for (let index = 0; index < lines.length; index++) {
|
|
84
|
+
if (lines[index].startsWith("import ")) {
|
|
85
|
+
lastImportIndex = index;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (lastImportIndex === -1) {
|
|
90
|
+
return `${line}\n${source}`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
lines.splice(lastImportIndex + 1, 0, line);
|
|
94
|
+
return lines.join("\n");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Collect every identifier referenced inside an array (or object) expression.
|
|
99
|
+
*/
|
|
100
|
+
export function identifiersFromArrayExpression(
|
|
101
|
+
expression: string,
|
|
102
|
+
): Set<string> {
|
|
103
|
+
const withoutBrackets = expression.replace(/^\[/, "").replace(/\]$/, "");
|
|
104
|
+
|
|
105
|
+
return new Set(
|
|
106
|
+
Array.from(
|
|
107
|
+
withoutBrackets.matchAll(/\b[A-Za-z_$][\w$]*\b/g),
|
|
108
|
+
([name]) => name,
|
|
109
|
+
),
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Append entries to an array literal, preserving single-line and multiline
|
|
115
|
+
* formatting plus trailing commas.
|
|
116
|
+
*/
|
|
117
|
+
export function appendToArrayExpression(
|
|
118
|
+
expression: string,
|
|
119
|
+
names: string[],
|
|
120
|
+
): string {
|
|
121
|
+
const closingBracket = /\]\s*$/.exec(expression);
|
|
122
|
+
if (!closingBracket) return expression;
|
|
123
|
+
|
|
124
|
+
const beforeClosingBracket = expression.slice(0, closingBracket.index);
|
|
125
|
+
const closingBracketText = expression.slice(closingBracket.index);
|
|
126
|
+
const inner = beforeClosingBracket.replace(/^\[/, "");
|
|
127
|
+
if (!inner.trim()) return `[${names.join(", ")}]`;
|
|
128
|
+
|
|
129
|
+
if (!expression.includes("\n")) {
|
|
130
|
+
const separator = /,\s*$/.test(inner) ? " " : ", ";
|
|
131
|
+
return `${beforeClosingBracket}${separator}${names.join(", ")}${closingBracketText}`;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const itemIndent =
|
|
135
|
+
inner
|
|
136
|
+
.split("\n")
|
|
137
|
+
.find((line) => line.trim())
|
|
138
|
+
?.match(/^[\t ]*/)?.[0] ?? "\t";
|
|
139
|
+
const closingIndent = inner.match(/\n([\t ]*)$/)?.[1] ?? "";
|
|
140
|
+
const trimmedBeforeClosingBracket = beforeClosingBracket.replace(/\s*$/, "");
|
|
141
|
+
const appendedNames = names.join(`,\n${itemIndent}`);
|
|
142
|
+
|
|
143
|
+
if (/,\s*$/.test(inner)) {
|
|
144
|
+
return `${trimmedBeforeClosingBracket}\n${itemIndent}${appendedNames},\n${closingIndent}${closingBracketText}`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return `${trimmedBeforeClosingBracket},\n${itemIndent}${appendedNames}${closingBracketText}`;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Locate the array literal initializing `const <constName> = ...`.
|
|
152
|
+
*
|
|
153
|
+
* Wrapping calls such as `defineTasks([...])` are tolerated because the first
|
|
154
|
+
* `[` after the assignment is used.
|
|
155
|
+
*/
|
|
156
|
+
export function arrayInitializerInfo(
|
|
157
|
+
source: string,
|
|
158
|
+
constName: string,
|
|
159
|
+
): InitializerInfo | undefined {
|
|
160
|
+
const match = new RegExp(`\\bconst\\s+${constName}\\s*=`).exec(source);
|
|
161
|
+
if (!match) return undefined;
|
|
162
|
+
|
|
163
|
+
const openBracket = source.indexOf("[", match.index);
|
|
164
|
+
if (openBracket === -1) return undefined;
|
|
165
|
+
|
|
166
|
+
const closeBracket = matchingDelimiterIndex(source, openBracket, "[", "]");
|
|
167
|
+
if (closeBracket === -1) return undefined;
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
text: source.slice(openBracket, closeBracket + 1),
|
|
171
|
+
start: openBracket,
|
|
172
|
+
end: closeBracket + 1,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Append `entry` to the array initializing `const <constName> = [...]` and
|
|
178
|
+
* insert `importLine` after the imports when it is not already present.
|
|
179
|
+
*
|
|
180
|
+
* Generalizes the `server/schedules.ts` and `server/tasks.ts` registry update
|
|
181
|
+
* used by `beignet make`. Returns `missing` without touching the source when
|
|
182
|
+
* the array cannot be found, so callers never partially write.
|
|
183
|
+
*/
|
|
184
|
+
export function appendToNamedArray(
|
|
185
|
+
source: string,
|
|
186
|
+
constName: string,
|
|
187
|
+
entry: string,
|
|
188
|
+
importLine?: string,
|
|
189
|
+
): AppendResult {
|
|
190
|
+
const registry = arrayInitializerInfo(source, constName);
|
|
191
|
+
if (!registry) return { kind: "missing" };
|
|
192
|
+
|
|
193
|
+
return appendEntry(source, registry, entry, importLine, (next) =>
|
|
194
|
+
arrayInitializerInfo(next, constName),
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Locate the `events` or `jobs` value inside `defineOutboxRegistry({...})`.
|
|
200
|
+
*/
|
|
201
|
+
export function outboxRegistryValueInfo(
|
|
202
|
+
source: string,
|
|
203
|
+
property: "events" | "jobs",
|
|
204
|
+
): InitializerInfo | undefined {
|
|
205
|
+
const call = /\bdefineOutboxRegistry\s*(?:<[^>]*>\s*)?\(/.exec(source);
|
|
206
|
+
if (!call) return undefined;
|
|
207
|
+
|
|
208
|
+
const openBrace = source.indexOf("{", call.index);
|
|
209
|
+
if (openBrace === -1) return undefined;
|
|
210
|
+
|
|
211
|
+
const closeBrace = matchingDelimiterIndex(source, openBrace, "{", "}");
|
|
212
|
+
if (closeBrace === -1) return undefined;
|
|
213
|
+
|
|
214
|
+
const objectText = source.slice(openBrace, closeBrace + 1);
|
|
215
|
+
const propertyMatch = new RegExp(`\\b${property}\\s*:`).exec(objectText);
|
|
216
|
+
if (!propertyMatch) return undefined;
|
|
217
|
+
|
|
218
|
+
const valueStart = openBrace + propertyMatch.index + propertyMatch[0].length;
|
|
219
|
+
const value = topLevelValueSlice(source, valueStart, closeBrace);
|
|
220
|
+
if (!value) return undefined;
|
|
221
|
+
|
|
222
|
+
return value;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Append `entry` to the `events` or `jobs` array of
|
|
227
|
+
* `defineOutboxRegistry({...})` and insert `importLine` when needed.
|
|
228
|
+
*
|
|
229
|
+
* Returns `missing` without touching the source when the registry call or
|
|
230
|
+
* property is absent, or when the property value is not an array literal.
|
|
231
|
+
*/
|
|
232
|
+
export function appendToOutboxRegistryArray(
|
|
233
|
+
source: string,
|
|
234
|
+
property: "events" | "jobs",
|
|
235
|
+
entry: string,
|
|
236
|
+
importLine?: string,
|
|
237
|
+
): AppendResult {
|
|
238
|
+
const value = outboxRegistryValueInfo(source, property);
|
|
239
|
+
if (!value) return { kind: "missing" };
|
|
240
|
+
|
|
241
|
+
const entryName = entryIdentifier(entry);
|
|
242
|
+
if (identifiersFromArrayExpression(value.text).has(entryName)) {
|
|
243
|
+
return appendEntry(source, value, entry, importLine, () => undefined, {
|
|
244
|
+
alreadyListed: true,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (!value.text.startsWith("[")) return { kind: "missing" };
|
|
249
|
+
|
|
250
|
+
return appendEntry(source, value, entry, importLine, (next) =>
|
|
251
|
+
outboxRegistryValueInfo(next, property),
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function entryIdentifier(entry: string): string {
|
|
256
|
+
return entry.replace(/^\.{3}/, "").trim();
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function appendEntry(
|
|
260
|
+
source: string,
|
|
261
|
+
info: InitializerInfo,
|
|
262
|
+
entry: string,
|
|
263
|
+
importLine: string | undefined,
|
|
264
|
+
relocate: (next: string) => InitializerInfo | undefined,
|
|
265
|
+
options: { alreadyListed?: boolean } = {},
|
|
266
|
+
): AppendResult {
|
|
267
|
+
const alreadyListed =
|
|
268
|
+
options.alreadyListed ??
|
|
269
|
+
identifiersFromArrayExpression(info.text).has(entryIdentifier(entry));
|
|
270
|
+
let next = source;
|
|
271
|
+
|
|
272
|
+
if (importLine && !next.includes(importLine)) {
|
|
273
|
+
next = insertAfterImports(next, importLine);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (!alreadyListed) {
|
|
277
|
+
const target = next === source ? info : relocate(next);
|
|
278
|
+
if (!target) return { kind: "missing" };
|
|
279
|
+
const nextArray = appendToArrayExpression(target.text, [entry]);
|
|
280
|
+
next = `${next.slice(0, target.start)}${nextArray}${next.slice(target.end)}`;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (next === source) return { kind: "unchanged" };
|
|
284
|
+
return { kind: "updated", source: next };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function topLevelValueSlice(
|
|
288
|
+
source: string,
|
|
289
|
+
valueStart: number,
|
|
290
|
+
hardEnd: number,
|
|
291
|
+
): InitializerInfo | undefined {
|
|
292
|
+
let start = valueStart;
|
|
293
|
+
while (start < hardEnd && /\s/.test(source[start])) start++;
|
|
294
|
+
if (start >= hardEnd) return undefined;
|
|
295
|
+
|
|
296
|
+
let depth = 0;
|
|
297
|
+
let inString: string | undefined;
|
|
298
|
+
let escaped = false;
|
|
299
|
+
|
|
300
|
+
for (let index = start; index <= hardEnd; index++) {
|
|
301
|
+
const char = source[index];
|
|
302
|
+
if (inString) {
|
|
303
|
+
if (escaped) {
|
|
304
|
+
escaped = false;
|
|
305
|
+
} else if (char === "\\") {
|
|
306
|
+
escaped = true;
|
|
307
|
+
} else if (char === inString) {
|
|
308
|
+
inString = undefined;
|
|
309
|
+
}
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
314
|
+
inString = char;
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (char === "[" || char === "(" || char === "{") {
|
|
319
|
+
depth++;
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (char === "]" || char === ")" || char === "}") {
|
|
324
|
+
if (depth === 0) {
|
|
325
|
+
return trimmedInfo(source, start, index);
|
|
326
|
+
}
|
|
327
|
+
depth--;
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (char === "," && depth === 0) {
|
|
332
|
+
return trimmedInfo(source, start, index);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return undefined;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function trimmedInfo(
|
|
340
|
+
source: string,
|
|
341
|
+
start: number,
|
|
342
|
+
end: number,
|
|
343
|
+
): InitializerInfo {
|
|
344
|
+
let trimmedEnd = end;
|
|
345
|
+
while (trimmedEnd > start && /\s/.test(source[trimmedEnd - 1])) trimmedEnd--;
|
|
346
|
+
|
|
347
|
+
return {
|
|
348
|
+
text: source.slice(start, trimmedEnd),
|
|
349
|
+
start,
|
|
350
|
+
end: trimmedEnd,
|
|
351
|
+
};
|
|
352
|
+
}
|