@donkeylabs/cli 2.0.20 → 2.0.22
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/package.json +2 -1
- package/src/commands/generate.ts +261 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@donkeylabs/cli",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.22",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "CLI for @donkeylabs/server - project scaffolding and code generation",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"picocolors": "^1.1.1",
|
|
31
31
|
"prompts": "^2.4.2",
|
|
32
32
|
"sharp": "^0.33.0",
|
|
33
|
+
"typescript": "^5.0.0",
|
|
33
34
|
"zod": "^3.23.0"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
package/src/commands/generate.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { Kysely, Migrator, FileMigrationProvider } from "kysely";
|
|
|
7
7
|
import { BunSqliteDialect } from "kysely-bun-sqlite";
|
|
8
8
|
import { Database } from "bun:sqlite";
|
|
9
9
|
import { generate, KyselyBunSqliteDialect } from "kysely-codegen";
|
|
10
|
+
import * as ts from "typescript";
|
|
10
11
|
|
|
11
12
|
interface DonkeylabsConfig {
|
|
12
13
|
plugins: string[];
|
|
@@ -69,20 +70,272 @@ async function extractHandlerNames(pluginPath: string): Promise<string[]> {
|
|
|
69
70
|
async function extractMiddlewareNames(pluginPath: string): Promise<string[]> {
|
|
70
71
|
try {
|
|
71
72
|
const content = await readFile(pluginPath, "utf-8");
|
|
73
|
+
const astNames = extractMiddlewareNamesFromAst(content, pluginPath);
|
|
74
|
+
if (astNames.length > 0) {
|
|
75
|
+
return astNames;
|
|
76
|
+
}
|
|
72
77
|
|
|
73
|
-
|
|
74
|
-
// This works for both old `middleware: { timing: createMiddleware(...) }`
|
|
75
|
-
// and new `middleware: (ctx) => ({ timing: createMiddleware(...) })`
|
|
76
|
-
const middlewareNames = [...content.matchAll(/(\w+)\s*:\s*createMiddleware\s*\(/g)]
|
|
77
|
-
.map((m) => m[1])
|
|
78
|
-
.filter((name): name is string => !!name);
|
|
79
|
-
|
|
80
|
-
return middlewareNames;
|
|
78
|
+
return extractMiddlewareNamesRegexFallback(content);
|
|
81
79
|
} catch {
|
|
82
80
|
return [];
|
|
83
81
|
}
|
|
84
82
|
}
|
|
85
83
|
|
|
84
|
+
function extractMiddlewareNamesFromAst(content: string, fileName: string): string[] {
|
|
85
|
+
const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
86
|
+
|
|
87
|
+
const names = new Set<string>();
|
|
88
|
+
|
|
89
|
+
const addFromObjectLiteral = (obj: ts.ObjectLiteralExpression) => {
|
|
90
|
+
for (const prop of obj.properties) {
|
|
91
|
+
if (!ts.isPropertyAssignment(prop) && !ts.isMethodDeclaration(prop)) continue;
|
|
92
|
+
const propName = getStaticPropertyName(prop.name);
|
|
93
|
+
if (propName && isSafeIdentifier(propName)) {
|
|
94
|
+
names.add(propName);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const collectFromMiddlewareInitializer = (initializer: ts.Expression) => {
|
|
100
|
+
if (ts.isObjectLiteralExpression(initializer)) {
|
|
101
|
+
addFromObjectLiteral(initializer);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer)) {
|
|
106
|
+
const body = initializer.body;
|
|
107
|
+
|
|
108
|
+
if (ts.isObjectLiteralExpression(body)) {
|
|
109
|
+
addFromObjectLiteral(body);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (ts.isParenthesizedExpression(body) && ts.isObjectLiteralExpression(body.expression)) {
|
|
114
|
+
addFromObjectLiteral(body.expression);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (ts.isBlock(body)) {
|
|
119
|
+
for (const stmt of body.statements) {
|
|
120
|
+
if (!ts.isReturnStatement(stmt) || !stmt.expression) continue;
|
|
121
|
+
|
|
122
|
+
if (ts.isObjectLiteralExpression(stmt.expression)) {
|
|
123
|
+
addFromObjectLiteral(stmt.expression);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (
|
|
128
|
+
ts.isParenthesizedExpression(stmt.expression) &&
|
|
129
|
+
ts.isObjectLiteralExpression(stmt.expression.expression)
|
|
130
|
+
) {
|
|
131
|
+
addFromObjectLiteral(stmt.expression.expression);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const visit = (node: ts.Node) => {
|
|
140
|
+
if (ts.isPropertyAssignment(node)) {
|
|
141
|
+
const propName = getStaticPropertyName(node.name);
|
|
142
|
+
if (propName === "middleware") {
|
|
143
|
+
collectFromMiddlewareInitializer(node.initializer);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
ts.forEachChild(node, visit);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
visit(sourceFile);
|
|
150
|
+
return [...names];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function getStaticPropertyName(name: ts.PropertyName): string | null {
|
|
154
|
+
if (ts.isIdentifier(name)) return name.text;
|
|
155
|
+
if (ts.isStringLiteral(name)) return name.text;
|
|
156
|
+
if (ts.isNoSubstitutionTemplateLiteral(name)) return name.text;
|
|
157
|
+
if (ts.isNumericLiteral(name)) return name.text;
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function isSafeIdentifier(name: string): boolean {
|
|
162
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function extractMiddlewareNamesRegexFallback(content: string): string[] {
|
|
166
|
+
const names = new Set<string>();
|
|
167
|
+
|
|
168
|
+
// Look for middleware definitions: `name: createMiddleware(...)`
|
|
169
|
+
// Supports generic config: `name: createMiddleware<Config>(...)`
|
|
170
|
+
for (const match of content.matchAll(/(\w+)\s*:\s*createMiddleware\s*(?:<[^>]+>)?\s*\(/g)) {
|
|
171
|
+
if (match[1]) names.add(match[1]);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Look for direct middleware objects: `middleware: { ... }`
|
|
175
|
+
for (const match of content.matchAll(/middleware\s*:\s*\{/g)) {
|
|
176
|
+
const openBracePos = (match.index ?? 0) + match[0].length - 1;
|
|
177
|
+
const block = extractBalancedBlock(content, openBracePos, "{", "}");
|
|
178
|
+
for (const key of extractTopLevelObjectKeys(block)) {
|
|
179
|
+
names.add(key);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Look for middleware factory returning object literal directly:
|
|
184
|
+
// `middleware: (ctx, service) => ({ ... })`
|
|
185
|
+
for (const match of content.matchAll(/middleware\s*:\s*\([^)]*\)\s*=>\s*\(\s*\{/g)) {
|
|
186
|
+
const openBracePos = (match.index ?? 0) + match[0].length - 1;
|
|
187
|
+
const block = extractBalancedBlock(content, openBracePos, "{", "}");
|
|
188
|
+
for (const key of extractTopLevelObjectKeys(block)) {
|
|
189
|
+
names.add(key);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Look for middleware factory with block body:
|
|
194
|
+
// `middleware: (...) => { return { ... } }`
|
|
195
|
+
for (const match of content.matchAll(/middleware\s*:\s*\([^)]*\)\s*=>\s*\{/g)) {
|
|
196
|
+
const openBracePos = (match.index ?? 0) + match[0].length - 1;
|
|
197
|
+
const fnBody = extractBalancedBlock(content, openBracePos, "{", "}");
|
|
198
|
+
if (!fnBody) continue;
|
|
199
|
+
|
|
200
|
+
const returnMatch = fnBody.match(/return\s*\{/);
|
|
201
|
+
if (!returnMatch || returnMatch.index === undefined) continue;
|
|
202
|
+
|
|
203
|
+
const returnObjStart = returnMatch.index + returnMatch[0].length - 1;
|
|
204
|
+
const returnObj = extractBalancedBlock(fnBody, returnObjStart, "{", "}");
|
|
205
|
+
for (const key of extractTopLevelObjectKeys(returnObj)) {
|
|
206
|
+
names.add(key);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return [...names];
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function extractTopLevelObjectKeys(objectBlock: string): string[] {
|
|
214
|
+
if (!objectBlock || !objectBlock.startsWith("{") || !objectBlock.endsWith("}")) {
|
|
215
|
+
return [];
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const keys: string[] = [];
|
|
219
|
+
const src = objectBlock.slice(1, -1);
|
|
220
|
+
const len = src.length;
|
|
221
|
+
|
|
222
|
+
let i = 0;
|
|
223
|
+
let braceDepth = 0;
|
|
224
|
+
let bracketDepth = 0;
|
|
225
|
+
let parenDepth = 0;
|
|
226
|
+
|
|
227
|
+
const isIdentifierStart = (ch: string) => /[A-Za-z_$]/.test(ch);
|
|
228
|
+
const isIdentifierPart = (ch: string) => /[A-Za-z0-9_$]/.test(ch);
|
|
229
|
+
|
|
230
|
+
while (i < len) {
|
|
231
|
+
const ch = src[i]!;
|
|
232
|
+
|
|
233
|
+
// Skip strings
|
|
234
|
+
if (ch === '"' || ch === "'" || ch === "`") {
|
|
235
|
+
const quote = ch;
|
|
236
|
+
i++;
|
|
237
|
+
while (i < len) {
|
|
238
|
+
const c = src[i]!;
|
|
239
|
+
if (c === "\\") {
|
|
240
|
+
i += 2;
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (c === quote) {
|
|
244
|
+
i++;
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
i++;
|
|
248
|
+
}
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Skip comments
|
|
253
|
+
if (ch === "/" && src[i + 1] === "/") {
|
|
254
|
+
i += 2;
|
|
255
|
+
while (i < len && src[i] !== "\n") i++;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
if (ch === "/" && src[i + 1] === "*") {
|
|
259
|
+
i += 2;
|
|
260
|
+
while (i + 1 < len && !(src[i] === "*" && src[i + 1] === "/")) i++;
|
|
261
|
+
i += 2;
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Track nesting
|
|
266
|
+
if (ch === "{") {
|
|
267
|
+
braceDepth++;
|
|
268
|
+
i++;
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
if (ch === "}") {
|
|
272
|
+
braceDepth = Math.max(0, braceDepth - 1);
|
|
273
|
+
i++;
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if (ch === "[") {
|
|
277
|
+
bracketDepth++;
|
|
278
|
+
i++;
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
if (ch === "]") {
|
|
282
|
+
bracketDepth = Math.max(0, bracketDepth - 1);
|
|
283
|
+
i++;
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (ch === "(") {
|
|
287
|
+
parenDepth++;
|
|
288
|
+
i++;
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
if (ch === ")") {
|
|
292
|
+
parenDepth = Math.max(0, parenDepth - 1);
|
|
293
|
+
i++;
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Parse top-level keys only
|
|
298
|
+
if (braceDepth === 0 && bracketDepth === 0 && parenDepth === 0) {
|
|
299
|
+
// Skip whitespace and commas
|
|
300
|
+
if (/\s|,/.test(ch)) {
|
|
301
|
+
i++;
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
let key = "";
|
|
306
|
+
const keyStart = i;
|
|
307
|
+
|
|
308
|
+
// Identifier key: authRequired: ... or authRequired(...) { ... }
|
|
309
|
+
if (isIdentifierStart(ch)) {
|
|
310
|
+
i++;
|
|
311
|
+
while (i < len && isIdentifierPart(src[i]!)) i++;
|
|
312
|
+
key = src.slice(keyStart, i);
|
|
313
|
+
} else {
|
|
314
|
+
i++;
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Skip optional marker and whitespace
|
|
319
|
+
while (i < len && /\s/.test(src[i]!)) i++;
|
|
320
|
+
if (src[i] === "?") {
|
|
321
|
+
i++;
|
|
322
|
+
while (i < len && /\s/.test(src[i]!)) i++;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Object property or method shorthand
|
|
326
|
+
if (src[i] === ":" || src[i] === "(") {
|
|
327
|
+
keys.push(key);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
i++;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return keys;
|
|
337
|
+
}
|
|
338
|
+
|
|
86
339
|
interface ServiceDefinitionInfo {
|
|
87
340
|
name: string;
|
|
88
341
|
exportName: string;
|
|
@@ -1270,4 +1523,3 @@ ${eventRegistryEntries.join("\n")}
|
|
|
1270
1523
|
|
|
1271
1524
|
await writeFile(join(outPath, "events.ts"), content);
|
|
1272
1525
|
}
|
|
1273
|
-
|