@databricks/appkit 0.25.0 → 0.26.0

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.
@@ -1,6 +1,6 @@
1
1
  //#region package.json
2
2
  var name = "@databricks/appkit";
3
- var version = "0.25.0";
3
+ var version = "0.26.0";
4
4
 
5
5
  //#endregion
6
6
  export { name, version };
@@ -0,0 +1,16 @@
1
+ import { onPluginsReadyCommand } from "./on-plugins-ready.js";
2
+ import { Command } from "commander";
3
+
4
+ //#region src/cli/commands/codemod/index.ts
5
+ /**
6
+ * Parent command for codemod operations.
7
+ * Subcommands:
8
+ * - on-plugins-ready: Migrate from autoStart/extend/start to onPluginsReady callback
9
+ */
10
+ const codemodCommand = new Command("codemod").description("Run codemods to migrate to newer AppKit APIs").addCommand(onPluginsReadyCommand).addHelpText("after", `
11
+ Examples:
12
+ $ appkit codemod on-plugins-ready --write`);
13
+
14
+ //#endregion
15
+ export { codemodCommand };
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../../../src/cli/commands/codemod/index.ts"],"sourcesContent":["import { Command } from \"commander\";\nimport { onPluginsReadyCommand } from \"./on-plugins-ready\";\n\n/**\n * Parent command for codemod operations.\n * Subcommands:\n * - on-plugins-ready: Migrate from autoStart/extend/start to onPluginsReady callback\n */\nexport const codemodCommand = new Command(\"codemod\")\n .description(\"Run codemods to migrate to newer AppKit APIs\")\n .addCommand(onPluginsReadyCommand)\n .addHelpText(\n \"after\",\n `\nExamples:\n $ appkit codemod on-plugins-ready --write`,\n );\n"],"mappings":";;;;;;;;;AAQA,MAAa,iBAAiB,IAAI,QAAQ,UAAU,CACjD,YAAY,+CAA+C,CAC3D,WAAW,sBAAsB,CACjC,YACC,SACA;;6CAGD"}
@@ -0,0 +1,364 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { Command } from "commander";
4
+ import { Lang, parse } from "@ast-grep/napi";
5
+
6
+ //#region src/cli/commands/codemod/on-plugins-ready.ts
7
+ const SEARCH_DIRS = [
8
+ "server",
9
+ "src",
10
+ "."
11
+ ];
12
+ const CANDIDATE_NAMES = ["server.ts", "index.ts"];
13
+ const SKIP_DIRS = new Set([
14
+ "node_modules",
15
+ "dist",
16
+ "build",
17
+ ".git"
18
+ ]);
19
+ function findServerEntryFiles(rootDir) {
20
+ const results = [];
21
+ for (const dir of SEARCH_DIRS) {
22
+ const absDir = path.resolve(rootDir, dir);
23
+ if (!fs.existsSync(absDir)) continue;
24
+ const files = dir === "." ? CANDIDATE_NAMES.map((n) => path.join(absDir, n)).filter(fs.existsSync) : findTsFiles(absDir);
25
+ for (const file of files) {
26
+ const content = fs.readFileSync(file, "utf-8");
27
+ if (content.includes("createApp") && content.includes("@databricks/appkit")) results.push(file);
28
+ }
29
+ }
30
+ return [...new Set(results)];
31
+ }
32
+ function findTsFiles(dir, files = []) {
33
+ let entries;
34
+ try {
35
+ entries = fs.readdirSync(dir, { withFileTypes: true });
36
+ } catch {
37
+ return files;
38
+ }
39
+ for (const entry of entries) {
40
+ const fullPath = path.join(dir, entry.name);
41
+ if (entry.isDirectory()) {
42
+ if (SKIP_DIRS.has(entry.name)) continue;
43
+ findTsFiles(fullPath, files);
44
+ } else if (entry.isFile() && entry.name.endsWith(".ts")) files.push(fullPath);
45
+ }
46
+ return files;
47
+ }
48
+ function isAlreadyMigrated(content) {
49
+ return parse(Lang.TypeScript, content).root().findAll("createApp({ $$$PROPS })").some((match) => {
50
+ const text = match.text();
51
+ return /\bonPluginsReady\s*[(:]/.test(text);
52
+ });
53
+ }
54
+ /**
55
+ * Find the index of the matching closing delimiter for an opening one.
56
+ * Supports (), {}, and [].
57
+ */
58
+ function findMatchingClose(content, openIdx) {
59
+ const open = content[openIdx];
60
+ const close = {
61
+ "(": ")",
62
+ "{": "}",
63
+ "[": "]"
64
+ }[open];
65
+ if (!close) return -1;
66
+ let depth = 1;
67
+ let i = openIdx + 1;
68
+ while (i < content.length && depth > 0) {
69
+ const ch = content[i];
70
+ if (ch === open) depth++;
71
+ else if (ch === close) depth--;
72
+ if (ch === "\"" || ch === "'" || ch === "`") {
73
+ i = skipString(content, i);
74
+ continue;
75
+ }
76
+ i++;
77
+ }
78
+ return depth === 0 ? i - 1 : -1;
79
+ }
80
+ function skipString(content, startIdx) {
81
+ const quote = content[startIdx];
82
+ let i = startIdx + 1;
83
+ while (i < content.length) {
84
+ if (content[i] === "\\") {
85
+ i += 2;
86
+ continue;
87
+ }
88
+ if (content[i] === quote) return i + 1;
89
+ i++;
90
+ }
91
+ return i;
92
+ }
93
+ function stripAutoStartFromServerCalls(content) {
94
+ return content.replace(/server\(\{([^}]*)\}\)/g, (_fullMatch, propsStr) => {
95
+ const cleaned = propsStr.replace(/autoStart\s*:\s*(true|false)\s*,?\s*/g, "").replace(/,\s*$/, "").trim();
96
+ if (!cleaned) return "server()";
97
+ return `server({ ${cleaned} })`;
98
+ });
99
+ }
100
+ function migratePatternA(content) {
101
+ const warnings = [];
102
+ const createAppIdx = content.indexOf("createApp(");
103
+ if (createAppIdx === -1) return {
104
+ migrated: false,
105
+ content,
106
+ warnings
107
+ };
108
+ const configOpenParen = content.indexOf("(", createAppIdx);
109
+ const configCloseParen = findMatchingClose(content, configOpenParen);
110
+ if (configCloseParen === -1) return {
111
+ migrated: false,
112
+ content,
113
+ warnings
114
+ };
115
+ const afterCreateApp = content.slice(configCloseParen + 1);
116
+ if (!afterCreateApp.match(/^\s*\.then\s*\(/)) return {
117
+ migrated: false,
118
+ content,
119
+ warnings
120
+ };
121
+ const thenStart = configCloseParen + 1 + afterCreateApp.indexOf(".then");
122
+ const thenOpenParen = content.indexOf("(", thenStart + 4);
123
+ const thenCloseParen = findMatchingClose(content, thenOpenParen);
124
+ if (thenCloseParen === -1) return {
125
+ migrated: false,
126
+ content,
127
+ warnings
128
+ };
129
+ const thenRaw = content.slice(thenOpenParen + 1, thenCloseParen);
130
+ const thenInner = thenRaw.trim();
131
+ const callbackMatch = thenInner.match(/^(?:async\s+)?\(\s*(\w+)\s*\)\s*=>\s*\{/);
132
+ if (!callbackMatch) return {
133
+ migrated: false,
134
+ content,
135
+ warnings
136
+ };
137
+ const paramName = callbackMatch[1];
138
+ const bodyOpenBrace = thenOpenParen + 1 + thenRaw.indexOf("{");
139
+ const bodyCloseBrace = findMatchingClose(content, bodyOpenBrace);
140
+ if (bodyCloseBrace === -1) return {
141
+ migrated: false,
142
+ content,
143
+ warnings
144
+ };
145
+ let callbackBody = content.slice(bodyOpenBrace + 1, bodyCloseBrace).trim();
146
+ callbackBody = callbackBody.replace(/^\s*(?:await\s+)?\w+\.server\s*\.\s*start\(\s*\)\s*;?\s*$/gm, "").replace(/\n\s*\.start\(\s*\)\s*;?/g, ";").replace(/\.start\(\s*\)/g, "").replace(/\n\s*\n\s*\n/g, "\n\n").trim();
147
+ if (callbackBody.endsWith(";")) {} else if (!callbackBody.endsWith("}")) callbackBody += ";";
148
+ const isAsync = /^async\s/.test(thenInner.trim());
149
+ const catchPatternMatch = content.slice(thenCloseParen + 1).match(/^\s*(?:\)\s*)?\.catch\s*\(/);
150
+ let catchSuffix;
151
+ let consumeAfterThen;
152
+ if (catchPatternMatch) {
153
+ const catchOpenParen = thenCloseParen + 1 + catchPatternMatch[0].length - 1;
154
+ const catchCloseParen = findMatchingClose(content, catchOpenParen);
155
+ if (catchCloseParen !== -1) {
156
+ catchSuffix = `.catch(${content.slice(catchOpenParen + 1, catchCloseParen).trim()})`;
157
+ consumeAfterThen = catchCloseParen + 1 - (thenCloseParen + 1);
158
+ } else {
159
+ catchSuffix = ".catch(console.error)";
160
+ consumeAfterThen = 0;
161
+ }
162
+ } else {
163
+ catchSuffix = ".catch(console.error)";
164
+ consumeAfterThen = 0;
165
+ }
166
+ const configStr = content.slice(configOpenParen + 1, configCloseParen);
167
+ const lastBraceIdx = configStr.lastIndexOf("}");
168
+ if (lastBraceIdx === -1) return {
169
+ migrated: false,
170
+ content,
171
+ warnings
172
+ };
173
+ const beforeLastBrace = configStr.slice(0, lastBraceIdx).trimEnd();
174
+ const needsComma = beforeLastBrace.endsWith(",") ? "" : ",";
175
+ const indentedBody = callbackBody.split("\n").map((line) => ` ${line.trimStart()}`).join("\n");
176
+ const newConfig = `${beforeLastBrace}${`${needsComma}\n ${isAsync ? "async " : ""}onPluginsReady(${paramName}) {\n${indentedBody}\n },`}\n}`;
177
+ let finalEnd = thenCloseParen + 1 + consumeAfterThen;
178
+ const trailing = content.slice(finalEnd).match(/^\s*\)?\s*;?\s*/);
179
+ if (trailing) finalEnd += trailing[0].length;
180
+ return {
181
+ migrated: true,
182
+ content: content.slice(0, createAppIdx) + `createApp(${newConfig})${catchSuffix};` + content.slice(finalEnd),
183
+ warnings
184
+ };
185
+ }
186
+ function migratePatternB(content) {
187
+ const warnings = [];
188
+ const match = content.match(/(?:const|let)\s+(\w+)\s*=\s*await\s+createApp\s*\(/);
189
+ if (!match) return {
190
+ migrated: false,
191
+ content,
192
+ warnings
193
+ };
194
+ const varName = match[1];
195
+ const matchIdx = content.indexOf(match[0]);
196
+ const configOpenParen = matchIdx + match[0].length - 1;
197
+ const configCloseParen = findMatchingClose(content, configOpenParen);
198
+ if (configCloseParen === -1) return {
199
+ migrated: false,
200
+ content,
201
+ warnings
202
+ };
203
+ const semiMatch = content.slice(configCloseParen + 1).match(/^\s*;/);
204
+ const createAppEnd = configCloseParen + 1 + (semiMatch ? semiMatch[0].length : 0);
205
+ const afterCreateApp = content.slice(createAppEnd);
206
+ const varUsagePattern = new RegExp(`\\b${varName}\\.(\\w+)`, "g");
207
+ const usages = [];
208
+ for (const usageMatch of afterCreateApp.matchAll(varUsagePattern)) usages.push({
209
+ plugin: usageMatch[1],
210
+ index: usageMatch.index
211
+ });
212
+ if (usages.filter((u) => u.plugin !== "server").length > 0) {
213
+ warnings.push(`Found additional usage of '${varName}' handle outside server.extend/start. Please migrate manually.`);
214
+ return {
215
+ migrated: false,
216
+ content,
217
+ warnings
218
+ };
219
+ }
220
+ const extendPattern = new RegExp(`\\b${varName}\\.server\\.extend\\s*\\(`, "g");
221
+ const startPattern = new RegExp(`(?:await\\s+)?${varName}\\.server\\.start\\s*\\(\\s*\\)\\s*;`);
222
+ const extendMatches = [...afterCreateApp.matchAll(extendPattern)];
223
+ if (extendMatches.length > 1) {
224
+ warnings.push(`Found ${extendMatches.length} server.extend() calls. Please migrate manually.`);
225
+ return {
226
+ migrated: false,
227
+ content,
228
+ warnings
229
+ };
230
+ }
231
+ const extendExec = extendMatches[0] ?? null;
232
+ const startExec = startPattern.exec(afterCreateApp);
233
+ if (!startExec) return {
234
+ migrated: false,
235
+ content,
236
+ warnings
237
+ };
238
+ let extendArg = "";
239
+ let extendFullStatement = "";
240
+ if (extendExec) {
241
+ const extendOpenParen = createAppEnd + extendExec.index + extendExec[0].length - 1;
242
+ const extendCloseParen = findMatchingClose(content, extendOpenParen);
243
+ if (extendCloseParen !== -1) {
244
+ extendArg = content.slice(extendOpenParen + 1, extendCloseParen).trim();
245
+ const stmtStart = createAppEnd + extendExec.index;
246
+ let stmtEnd = extendCloseParen + 1;
247
+ const trailingSemi = content.slice(stmtEnd).match(/^\s*;/);
248
+ if (trailingSemi) stmtEnd += trailingSemi[0].length;
249
+ extendFullStatement = content.slice(stmtStart, stmtEnd);
250
+ }
251
+ }
252
+ const startFullStatement = startExec[0];
253
+ const configStr = content.slice(configOpenParen + 1, configCloseParen);
254
+ const lastBraceIdx = configStr.lastIndexOf("}");
255
+ if (lastBraceIdx === -1) return {
256
+ migrated: false,
257
+ content,
258
+ warnings
259
+ };
260
+ const beforeLastBrace = configStr.slice(0, lastBraceIdx).trimEnd();
261
+ const needsComma = beforeLastBrace.endsWith(",") ? "" : ",";
262
+ let onPluginsReadyProp;
263
+ if (extendArg) onPluginsReadyProp = `${needsComma}\n onPluginsReady(${varName}) {\n ${varName}.server.extend(${extendArg});\n },`;
264
+ else onPluginsReadyProp = "";
265
+ const newCreateApp = `await createApp(${`${beforeLastBrace}${onPluginsReadyProp}\n}`});`;
266
+ let result = content.slice(0, matchIdx) + newCreateApp;
267
+ let remaining = afterCreateApp;
268
+ if (extendFullStatement) remaining = remaining.replace(extendFullStatement, "");
269
+ remaining = remaining.replace(startFullStatement, "");
270
+ remaining = remaining.replace(/\n\s*\n\s*\n/g, "\n\n");
271
+ result += remaining;
272
+ return {
273
+ migrated: true,
274
+ content: result,
275
+ warnings
276
+ };
277
+ }
278
+ function migrateFile(filePath) {
279
+ const original = fs.readFileSync(filePath, "utf-8");
280
+ if (isAlreadyMigrated(original)) return {
281
+ migrated: false,
282
+ content: original,
283
+ warnings: ["Already migrated -- no changes needed."]
284
+ };
285
+ const content = stripAutoStartFromServerCalls(original);
286
+ const allWarnings = [];
287
+ const patternA = migratePatternA(content);
288
+ if (patternA.migrated) {
289
+ allWarnings.push(...patternA.warnings);
290
+ return {
291
+ migrated: true,
292
+ content: patternA.content,
293
+ warnings: allWarnings
294
+ };
295
+ }
296
+ allWarnings.push(...patternA.warnings);
297
+ const patternB = migratePatternB(content);
298
+ if (patternB.migrated) {
299
+ allWarnings.push(...patternB.warnings);
300
+ return {
301
+ migrated: true,
302
+ content: patternB.content,
303
+ warnings: allWarnings
304
+ };
305
+ }
306
+ allWarnings.push(...patternB.warnings);
307
+ if (content !== original) return {
308
+ migrated: true,
309
+ content,
310
+ warnings: allWarnings
311
+ };
312
+ return {
313
+ migrated: false,
314
+ content: original,
315
+ warnings: allWarnings
316
+ };
317
+ }
318
+ function runCodemod(options) {
319
+ const rootDir = process.cwd();
320
+ const write = options.write ?? false;
321
+ let files;
322
+ if (options.path) {
323
+ const absPath = path.resolve(rootDir, options.path);
324
+ if (!fs.existsSync(absPath)) {
325
+ console.error(`File not found: ${absPath}`);
326
+ process.exit(1);
327
+ }
328
+ files = [absPath];
329
+ } else files = findServerEntryFiles(rootDir);
330
+ if (files.length === 0) {
331
+ console.log("No files found importing createApp from @databricks/appkit.");
332
+ console.log("Use --path to specify a file explicitly.");
333
+ process.exit(0);
334
+ }
335
+ let hasChanges = false;
336
+ for (const file of files) {
337
+ const relPath = path.relative(rootDir, file);
338
+ const result = migrateFile(file);
339
+ for (const warning of result.warnings) console.log(` ${relPath}: ${warning}`);
340
+ if (!result.migrated) {
341
+ if (result.warnings.length === 0) console.log(` ${relPath}: No migration needed.`);
342
+ continue;
343
+ }
344
+ hasChanges = true;
345
+ if (write) {
346
+ fs.writeFileSync(file, result.content, "utf-8");
347
+ console.log(` ${relPath}: Migrated successfully.`);
348
+ } else {
349
+ console.log(`\n--- ${relPath} (dry run) ---`);
350
+ console.log(result.content);
351
+ console.log("---");
352
+ }
353
+ }
354
+ if (hasChanges && !write) console.log("\nDry run complete. Run with --write to apply changes.");
355
+ }
356
+ const onPluginsReadyCommand = new Command("on-plugins-ready").description("Migrate createApp usage from autoStart/extend/start pattern to onPluginsReady callback").option("--path <file>", "Path to the server entry file to migrate").option("--write", "Apply changes (default: dry-run)", false).addHelpText("after", `
357
+ Examples:
358
+ $ appkit codemod on-plugins-ready # dry-run, auto-detect files
359
+ $ appkit codemod on-plugins-ready --write # apply changes
360
+ $ appkit codemod on-plugins-ready --path server.ts # migrate a specific file`).action(runCodemod);
361
+
362
+ //#endregion
363
+ export { onPluginsReadyCommand };
364
+ //# sourceMappingURL=on-plugins-ready.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"on-plugins-ready.js","names":[],"sources":["../../../../src/cli/commands/codemod/on-plugins-ready.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport path from \"node:path\";\nimport { Lang, parse } from \"@ast-grep/napi\";\nimport { Command } from \"commander\";\n\nconst SEARCH_DIRS = [\"server\", \"src\", \".\"];\nconst CANDIDATE_NAMES = [\"server.ts\", \"index.ts\"];\nconst SKIP_DIRS = new Set([\"node_modules\", \"dist\", \"build\", \".git\"]);\n\nfunction findServerEntryFiles(rootDir: string): string[] {\n const results: string[] = [];\n\n for (const dir of SEARCH_DIRS) {\n const absDir = path.resolve(rootDir, dir);\n if (!fs.existsSync(absDir)) continue;\n\n const files =\n dir === \".\"\n ? CANDIDATE_NAMES.map((n) => path.join(absDir, n)).filter(fs.existsSync)\n : findTsFiles(absDir);\n\n for (const file of files) {\n const content = fs.readFileSync(file, \"utf-8\");\n if (\n content.includes(\"createApp\") &&\n content.includes(\"@databricks/appkit\")\n ) {\n results.push(file);\n }\n }\n }\n\n return [...new Set(results)];\n}\n\nfunction findTsFiles(dir: string, files: string[] = []): string[] {\n let entries: fs.Dirent[];\n try {\n entries = fs.readdirSync(dir, { withFileTypes: true });\n } catch {\n return files;\n }\n\n for (const entry of entries) {\n const fullPath = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n if (SKIP_DIRS.has(entry.name)) continue;\n findTsFiles(fullPath, files);\n } else if (entry.isFile() && entry.name.endsWith(\".ts\")) {\n files.push(fullPath);\n }\n }\n\n return files;\n}\n\nfunction isAlreadyMigrated(content: string): boolean {\n const ast = parse(Lang.TypeScript, content);\n const root = ast.root();\n return root.findAll(\"createApp({ $$$PROPS })\").some((match) => {\n const text = match.text();\n return /\\bonPluginsReady\\s*[(:]/.test(text);\n });\n}\n\n/**\n * Find the index of the matching closing delimiter for an opening one.\n * Supports (), {}, and [].\n */\nfunction findMatchingClose(content: string, openIdx: number): number {\n const open = content[openIdx];\n const closeMap: Record<string, string> = {\n \"(\": \")\",\n \"{\": \"}\",\n \"[\": \"]\",\n };\n const close = closeMap[open];\n if (!close) return -1;\n\n let depth = 1;\n let i = openIdx + 1;\n while (i < content.length && depth > 0) {\n const ch = content[i];\n if (ch === open) depth++;\n else if (ch === close) depth--;\n\n // skip string literals\n if (ch === '\"' || ch === \"'\" || ch === \"`\") {\n i = skipString(content, i);\n continue;\n }\n i++;\n }\n return depth === 0 ? i - 1 : -1;\n}\n\nfunction skipString(content: string, startIdx: number): number {\n const quote = content[startIdx];\n let i = startIdx + 1;\n while (i < content.length) {\n if (content[i] === \"\\\\\") {\n i += 2;\n continue;\n }\n if (content[i] === quote) return i + 1;\n i++;\n }\n return i;\n}\n\nfunction stripAutoStartFromServerCalls(content: string): string {\n return content.replace(\n /server\\(\\{([^}]*)\\}\\)/g,\n (_fullMatch, propsStr: string) => {\n const cleaned = propsStr\n .replace(/autoStart\\s*:\\s*(true|false)\\s*,?\\s*/g, \"\")\n .replace(/,\\s*$/, \"\")\n .trim();\n if (!cleaned) return \"server()\";\n return `server({ ${cleaned} })`;\n },\n );\n}\n\ninterface MigrationResult {\n migrated: boolean;\n content: string;\n warnings: string[];\n}\n\nfunction migratePatternA(content: string): MigrationResult {\n const warnings: string[] = [];\n\n // Find createApp(...).then(\n const createAppIdx = content.indexOf(\"createApp(\");\n if (createAppIdx === -1) return { migrated: false, content, warnings };\n\n // Find the opening paren of createApp(\n const configOpenParen = content.indexOf(\"(\", createAppIdx);\n const configCloseParen = findMatchingClose(content, configOpenParen);\n if (configCloseParen === -1) return { migrated: false, content, warnings };\n\n // Check for .then( after the closing paren\n const afterCreateApp = content.slice(configCloseParen + 1);\n const thenMatch = afterCreateApp.match(/^\\s*\\.then\\s*\\(/);\n if (!thenMatch) return { migrated: false, content, warnings };\n\n const thenStart = configCloseParen + 1 + afterCreateApp.indexOf(\".then\");\n const thenOpenParen = content.indexOf(\"(\", thenStart + 4);\n const thenCloseParen = findMatchingClose(content, thenOpenParen);\n if (thenCloseParen === -1) return { migrated: false, content, warnings };\n\n // Extract the callback inside .then(...)\n const thenRaw = content.slice(thenOpenParen + 1, thenCloseParen);\n const thenInner = thenRaw.trim();\n\n // Parse callback: (param) => { body } or async (param) => { body }\n const callbackMatch = thenInner.match(\n /^(?:async\\s+)?\\(\\s*(\\w+)\\s*\\)\\s*=>\\s*\\{/,\n );\n if (!callbackMatch) return { migrated: false, content, warnings };\n\n const paramName = callbackMatch[1];\n const bodyOpenBrace = thenOpenParen + 1 + thenRaw.indexOf(\"{\");\n const bodyCloseBrace = findMatchingClose(content, bodyOpenBrace);\n if (bodyCloseBrace === -1) return { migrated: false, content, warnings };\n\n let callbackBody = content.slice(bodyOpenBrace + 1, bodyCloseBrace).trim();\n\n // Remove entire statements that are just .start() calls (e.g. `await appkit.server.start();`)\n callbackBody = callbackBody\n .replace(/^\\s*(?:await\\s+)?\\w+\\.server\\s*\\.\\s*start\\(\\s*\\)\\s*;?\\s*$/gm, \"\")\n .replace(/\\n\\s*\\.start\\(\\s*\\)\\s*;?/g, \";\")\n .replace(/\\.start\\(\\s*\\)/g, \"\")\n .replace(/\\n\\s*\\n\\s*\\n/g, \"\\n\\n\")\n .trim();\n\n // Clean up trailing semicolons\n if (callbackBody.endsWith(\";\")) {\n // fine\n } else if (!callbackBody.endsWith(\"}\")) {\n callbackBody += \";\";\n }\n\n // Detect if the callback was async\n const isAsync = /^async\\s/.test(thenInner.trim());\n\n // Check for .catch() after .then(...) using brace-aware parsing\n const afterThenClose = content.slice(thenCloseParen + 1);\n const catchPatternMatch = afterThenClose.match(/^\\s*(?:\\)\\s*)?\\.catch\\s*\\(/);\n\n let catchSuffix: string;\n let consumeAfterThen: number;\n\n if (catchPatternMatch) {\n const catchOpenParen = thenCloseParen + 1 + catchPatternMatch[0].length - 1;\n const catchCloseParen = findMatchingClose(content, catchOpenParen);\n if (catchCloseParen !== -1) {\n const catchArg = content\n .slice(catchOpenParen + 1, catchCloseParen)\n .trim();\n catchSuffix = `.catch(${catchArg})`;\n consumeAfterThen = catchCloseParen + 1 - (thenCloseParen + 1);\n } else {\n catchSuffix = \".catch(console.error)\";\n consumeAfterThen = 0;\n }\n } else {\n catchSuffix = \".catch(console.error)\";\n consumeAfterThen = 0;\n }\n\n // Build the onPluginsReady property\n const configStr = content.slice(configOpenParen + 1, configCloseParen);\n const lastBraceIdx = configStr.lastIndexOf(\"}\");\n if (lastBraceIdx === -1) return { migrated: false, content, warnings };\n\n const beforeLastBrace = configStr.slice(0, lastBraceIdx).trimEnd();\n const needsComma = beforeLastBrace.endsWith(\",\") ? \"\" : \",\";\n\n // Indent the body properly\n const bodyLines = callbackBody.split(\"\\n\");\n const indentedBody = bodyLines\n .map((line) => ` ${line.trimStart()}`)\n .join(\"\\n\");\n\n const asyncPrefix = isAsync ? \"async \" : \"\";\n const onPluginsReadyProp = `${needsComma}\\n ${asyncPrefix}onPluginsReady(${paramName}) {\\n${indentedBody}\\n },`;\n const newConfig = `${beforeLastBrace}${onPluginsReadyProp}\\n}`;\n\n // Build the replacement\n const endIdx = thenCloseParen + 1 + consumeAfterThen;\n // Consume trailing ) ; and whitespace\n let finalEnd = endIdx;\n const trailing = content.slice(finalEnd).match(/^\\s*\\)?\\s*;?\\s*/);\n if (trailing) finalEnd += trailing[0].length;\n\n const newContent =\n content.slice(0, createAppIdx) +\n `createApp(${newConfig})${catchSuffix};` +\n content.slice(finalEnd);\n\n return { migrated: true, content: newContent, warnings };\n}\n\nfunction migratePatternB(content: string): MigrationResult {\n const warnings: string[] = [];\n\n // Match: const/let varName = await createApp({...});\n const awaitPattern = /(?:const|let)\\s+(\\w+)\\s*=\\s*await\\s+createApp\\s*\\(/;\n\n const match = content.match(awaitPattern);\n if (!match) return { migrated: false, content, warnings };\n\n const varName = match[1];\n const matchIdx = content.indexOf(match[0]);\n\n // Find the createApp(...) closing paren\n const configOpenParen = matchIdx + match[0].length - 1;\n const configCloseParen = findMatchingClose(content, configOpenParen);\n if (configCloseParen === -1) return { migrated: false, content, warnings };\n\n // Find the semicolon after the createApp call\n const afterCall = content.slice(configCloseParen + 1);\n const semiMatch = afterCall.match(/^\\s*;/);\n const createAppEnd =\n configCloseParen + 1 + (semiMatch ? semiMatch[0].length : 0);\n\n // Find all uses of varName after the createApp call\n const afterCreateApp = content.slice(createAppEnd);\n const varUsagePattern = new RegExp(`\\\\b${varName}\\\\.(\\\\w+)`, \"g\");\n\n const usages: { plugin: string; index: number }[] = [];\n for (const usageMatch of afterCreateApp.matchAll(varUsagePattern)) {\n usages.push({ plugin: usageMatch[1], index: usageMatch.index });\n }\n\n // Check for non-server usage\n const nonServerUsage = usages.filter((u) => u.plugin !== \"server\");\n if (nonServerUsage.length > 0) {\n warnings.push(\n `Found additional usage of '${varName}' handle outside server.extend/start. Please migrate manually.`,\n );\n return { migrated: false, content, warnings };\n }\n\n // Find the extend call(s) and start call in the after-createApp region\n const extendPattern = new RegExp(\n `\\\\b${varName}\\\\.server\\\\.extend\\\\s*\\\\(`,\n \"g\",\n );\n const startPattern = new RegExp(\n `(?:await\\\\s+)?${varName}\\\\.server\\\\.start\\\\s*\\\\(\\\\s*\\\\)\\\\s*;`,\n );\n\n const extendMatches = [...afterCreateApp.matchAll(extendPattern)];\n if (extendMatches.length > 1) {\n warnings.push(\n `Found ${extendMatches.length} server.extend() calls. Please migrate manually.`,\n );\n return { migrated: false, content, warnings };\n }\n\n const extendExec = extendMatches[0] ?? null;\n const startExec = startPattern.exec(afterCreateApp);\n\n if (!startExec) return { migrated: false, content, warnings };\n\n // Extract the extend call's argument\n let extendArg = \"\";\n let extendFullStatement = \"\";\n if (extendExec) {\n const extendOpenParen =\n createAppEnd + extendExec.index + extendExec[0].length - 1;\n const extendCloseParen = findMatchingClose(content, extendOpenParen);\n if (extendCloseParen !== -1) {\n extendArg = content.slice(extendOpenParen + 1, extendCloseParen).trim();\n // Find the full statement including trailing semicolon\n const stmtStart = createAppEnd + extendExec.index;\n let stmtEnd = extendCloseParen + 1;\n const afterExtend = content.slice(stmtEnd);\n const trailingSemi = afterExtend.match(/^\\s*;/);\n if (trailingSemi) stmtEnd += trailingSemi[0].length;\n extendFullStatement = content.slice(stmtStart, stmtEnd);\n }\n }\n\n const startFullStatement = startExec[0];\n\n // Build the onPluginsReady callback\n const configStr = content.slice(configOpenParen + 1, configCloseParen);\n const lastBraceIdx = configStr.lastIndexOf(\"}\");\n if (lastBraceIdx === -1) return { migrated: false, content, warnings };\n\n const beforeLastBrace = configStr.slice(0, lastBraceIdx).trimEnd();\n const needsComma = beforeLastBrace.endsWith(\",\") ? \"\" : \",\";\n\n let onPluginsReadyProp: string;\n if (extendArg) {\n onPluginsReadyProp =\n `${needsComma}\\n onPluginsReady(${varName}) {\\n` +\n ` ${varName}.server.extend(${extendArg});\\n` +\n \" },\";\n } else {\n onPluginsReadyProp = \"\";\n }\n\n const newConfig = `${beforeLastBrace}${onPluginsReadyProp}\\n}`;\n const newCreateApp = `await createApp(${newConfig});`;\n\n // Replace: remove const declaration, replace with plain await, remove extend + start\n let result = content.slice(0, matchIdx) + newCreateApp;\n let remaining = afterCreateApp;\n\n if (extendFullStatement) {\n remaining = remaining.replace(extendFullStatement, \"\");\n }\n remaining = remaining.replace(startFullStatement, \"\");\n\n // Clean up consecutive blank lines\n remaining = remaining.replace(/\\n\\s*\\n\\s*\\n/g, \"\\n\\n\");\n\n result += remaining;\n\n return { migrated: true, content: result, warnings };\n}\n\nexport function migrateFile(filePath: string): MigrationResult {\n const original = fs.readFileSync(filePath, \"utf-8\");\n\n if (isAlreadyMigrated(original)) {\n return {\n migrated: false,\n content: original,\n warnings: [\"Already migrated -- no changes needed.\"],\n };\n }\n\n const content = stripAutoStartFromServerCalls(original);\n const allWarnings: string[] = [];\n\n // Try Pattern A first\n const patternA = migratePatternA(content);\n if (patternA.migrated) {\n allWarnings.push(...patternA.warnings);\n return {\n migrated: true,\n content: patternA.content,\n warnings: allWarnings,\n };\n }\n allWarnings.push(...patternA.warnings);\n\n // Try Pattern B\n const patternB = migratePatternB(content);\n if (patternB.migrated) {\n allWarnings.push(...patternB.warnings);\n return {\n migrated: true,\n content: patternB.content,\n warnings: allWarnings,\n };\n }\n allWarnings.push(...patternB.warnings);\n\n // Check if autoStart was stripped (content changed but no pattern matched)\n if (content !== original) {\n return { migrated: true, content, warnings: allWarnings };\n }\n\n return { migrated: false, content: original, warnings: allWarnings };\n}\n\nfunction runCodemod(options: { path?: string; write?: boolean }) {\n const rootDir = process.cwd();\n const write = options.write ?? false;\n\n let files: string[];\n if (options.path) {\n const absPath = path.resolve(rootDir, options.path);\n if (!fs.existsSync(absPath)) {\n console.error(`File not found: ${absPath}`);\n process.exit(1);\n }\n files = [absPath];\n } else {\n files = findServerEntryFiles(rootDir);\n }\n\n if (files.length === 0) {\n console.log(\"No files found importing createApp from @databricks/appkit.\");\n console.log(\"Use --path to specify a file explicitly.\");\n process.exit(0);\n }\n\n let hasChanges = false;\n\n for (const file of files) {\n const relPath = path.relative(rootDir, file);\n const result = migrateFile(file);\n\n for (const warning of result.warnings) {\n console.log(` ${relPath}: ${warning}`);\n }\n\n if (!result.migrated) {\n if (result.warnings.length === 0) {\n console.log(` ${relPath}: No migration needed.`);\n }\n continue;\n }\n\n hasChanges = true;\n\n if (write) {\n fs.writeFileSync(file, result.content, \"utf-8\");\n console.log(` ${relPath}: Migrated successfully.`);\n } else {\n console.log(`\\n--- ${relPath} (dry run) ---`);\n console.log(result.content);\n console.log(\"---\");\n }\n }\n\n if (hasChanges && !write) {\n console.log(\"\\nDry run complete. Run with --write to apply changes.\");\n }\n}\n\nexport const onPluginsReadyCommand = new Command(\"on-plugins-ready\")\n .description(\n \"Migrate createApp usage from autoStart/extend/start pattern to onPluginsReady callback\",\n )\n .option(\"--path <file>\", \"Path to the server entry file to migrate\")\n .option(\"--write\", \"Apply changes (default: dry-run)\", false)\n .addHelpText(\n \"after\",\n `\nExamples:\n $ appkit codemod on-plugins-ready # dry-run, auto-detect files\n $ appkit codemod on-plugins-ready --write # apply changes\n $ appkit codemod on-plugins-ready --path server.ts # migrate a specific file`,\n )\n .action(runCodemod);\n"],"mappings":";;;;;;AAKA,MAAM,cAAc;CAAC;CAAU;CAAO;CAAI;AAC1C,MAAM,kBAAkB,CAAC,aAAa,WAAW;AACjD,MAAM,YAAY,IAAI,IAAI;CAAC;CAAgB;CAAQ;CAAS;CAAO,CAAC;AAEpE,SAAS,qBAAqB,SAA2B;CACvD,MAAM,UAAoB,EAAE;AAE5B,MAAK,MAAM,OAAO,aAAa;EAC7B,MAAM,SAAS,KAAK,QAAQ,SAAS,IAAI;AACzC,MAAI,CAAC,GAAG,WAAW,OAAO,CAAE;EAE5B,MAAM,QACJ,QAAQ,MACJ,gBAAgB,KAAK,MAAM,KAAK,K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,CAC5D,YACC,SACA;;;;kFAKD,CACA,OAAO,WAAW"}
package/dist/cli/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ import { codemodCommand } from "./commands/codemod/index.js";
2
3
  import { docsCommand } from "./commands/docs.js";
3
4
  import { generateTypesCommand } from "./commands/generate-types.js";
4
5
  import { lintCommand } from "./commands/lint.js";
@@ -20,6 +21,7 @@ cmd.addCommand(generateTypesCommand);
20
21
  cmd.addCommand(lintCommand);
21
22
  cmd.addCommand(docsCommand);
22
23
  cmd.addCommand(pluginCommand);
24
+ cmd.addCommand(codemodCommand);
23
25
  cmd.parse();
24
26
 
25
27
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../src/cli/index.ts"],"sourcesContent":["#!/usr/bin/env node\nimport \"dotenv/config\";\nimport { readFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { Command } from \"commander\";\nimport { docsCommand } from \"./commands/docs.js\";\nimport { generateTypesCommand } from \"./commands/generate-types.js\";\nimport { lintCommand } from \"./commands/lint.js\";\nimport { pluginCommand } from \"./commands/plugin/index.js\";\nimport { setupCommand } from \"./commands/setup.js\";\n\nconst __dirname = dirname(fileURLToPath(import.meta.url));\nconst pkgPath = join(__dirname, \"../../package.json\");\nconst pkg = JSON.parse(readFileSync(pkgPath, \"utf-8\"));\n\nconst cmd = new Command();\n\ncmd\n .name(\"appkit\")\n .description(\"CLI tools for Databricks AppKit\")\n .version(pkg.version);\n\ncmd.addCommand(setupCommand);\ncmd.addCommand(generateTypesCommand);\ncmd.addCommand(lintCommand);\ncmd.addCommand(docsCommand);\ncmd.addCommand(pluginCommand);\n\ncmd.parse();\n"],"mappings":";;;;;;;;;;;;;AAaA,MAAM,UAAU,KADE,QAAQ,cAAc,OAAO,KAAK,IAAI,CAAC,EACzB,qBAAqB;AACrD,MAAM,MAAM,KAAK,MAAM,aAAa,SAAS,QAAQ,CAAC;AAEtD,MAAM,MAAM,IAAI,SAAS;AAEzB,IACG,KAAK,SAAS,CACd,YAAY,kCAAkC,CAC9C,QAAQ,IAAI,QAAQ;AAEvB,IAAI,WAAW,aAAa;AAC5B,IAAI,WAAW,qBAAqB;AACpC,IAAI,WAAW,YAAY;AAC3B,IAAI,WAAW,YAAY;AAC3B,IAAI,WAAW,cAAc;AAE7B,IAAI,OAAO"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/cli/index.ts"],"sourcesContent":["#!/usr/bin/env node\nimport \"dotenv/config\";\nimport { readFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { Command } from \"commander\";\nimport { codemodCommand } from \"./commands/codemod/index.js\";\nimport { docsCommand } from \"./commands/docs.js\";\nimport { generateTypesCommand } from \"./commands/generate-types.js\";\nimport { lintCommand } from \"./commands/lint.js\";\nimport { pluginCommand } from \"./commands/plugin/index.js\";\nimport { setupCommand } from \"./commands/setup.js\";\n\nconst __dirname = dirname(fileURLToPath(import.meta.url));\nconst pkgPath = join(__dirname, \"../../package.json\");\nconst pkg = JSON.parse(readFileSync(pkgPath, \"utf-8\"));\n\nconst cmd = new Command();\n\ncmd\n .name(\"appkit\")\n .description(\"CLI tools for Databricks AppKit\")\n .version(pkg.version);\n\ncmd.addCommand(setupCommand);\ncmd.addCommand(generateTypesCommand);\ncmd.addCommand(lintCommand);\ncmd.addCommand(docsCommand);\ncmd.addCommand(pluginCommand);\ncmd.addCommand(codemodCommand);\n\ncmd.parse();\n"],"mappings":";;;;;;;;;;;;;;AAcA,MAAM,UAAU,KADE,QAAQ,cAAc,OAAO,KAAK,IAAI,CAAC,EACzB,qBAAqB;AACrD,MAAM,MAAM,KAAK,MAAM,aAAa,SAAS,QAAQ,CAAC;AAEtD,MAAM,MAAM,IAAI,SAAS;AAEzB,IACG,KAAK,SAAS,CACd,YAAY,kCAAkC,CAC9C,QAAQ,IAAI,QAAQ;AAEvB,IAAI,WAAW,aAAa;AAC5B,IAAI,WAAW,qBAAqB;AACpC,IAAI,WAAW,YAAY;AAC3B,IAAI,WAAW,YAAY;AAC3B,IAAI,WAAW,cAAc;AAC7B,IAAI,WAAW,eAAe;AAE9B,IAAI,OAAO"}
@@ -11,6 +11,9 @@ import { WorkspaceClient } from "@databricks/sdk-experimental";
11
11
  *
12
12
  * Initializes telemetry, cache, and service context, then registers plugins
13
13
  * in phase order (core, normal, deferred) and awaits their setup.
14
+ * If a `onPluginsReady` callback is provided it runs after plugin setup but
15
+ * before the server starts, giving you access to the full appkit handle
16
+ * for registering custom routes or performing async setup.
14
17
  * The returned object maps each plugin name to its `exports()` API,
15
18
  * with an `asUser(req)` method for user-scoped execution.
16
19
  *
@@ -25,18 +28,18 @@ import { WorkspaceClient } from "@databricks/sdk-experimental";
25
28
  * });
26
29
  * ```
27
30
  *
28
- * @example Extended Server with analytics and custom endpoint
31
+ * @example Server with custom routes via onPluginsReady
29
32
  * ```ts
30
33
  * import { createApp, server, analytics } from "@databricks/appkit";
31
34
  *
32
- * const appkit = await createApp({
33
- * plugins: [server({ autoStart: false }), analytics({})],
34
- * });
35
- *
36
- * appkit.server.extend((app) => {
37
- * app.get("/custom", (_req, res) => res.json({ ok: true }));
35
+ * await createApp({
36
+ * plugins: [server(), analytics({})],
37
+ * onPluginsReady(appkit) {
38
+ * appkit.server.extend((app) => {
39
+ * app.get("/custom", (_req, res) => res.json({ ok: true }));
40
+ * });
41
+ * },
38
42
  * });
39
- * await appkit.server.start();
40
43
  * ```
41
44
  */
42
45
  declare function createApp<T extends PluginData<PluginConstructor, unknown, string>[]>(config?: {
@@ -44,6 +47,7 @@ declare function createApp<T extends PluginData<PluginConstructor, unknown, stri
44
47
  telemetry?: TelemetryConfig;
45
48
  cache?: CacheConfig;
46
49
  client?: WorkspaceClient;
50
+ onPluginsReady?: (appkit: PluginMap<T>) => void | Promise<void>;
47
51
  }): Promise<PluginMap<T>>;
48
52
  //#endregion
49
53
  export { createApp };
@@ -1 +1 @@
1
- {"version":3,"file":"appkit.d.ts","names":[],"sources":["../../src/core/appkit.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA4PsB,SAAA,WACV,UAAA,CAAW,iBAAA,qBAAA,CAErB,MAAA;EACE,OAAA,GAAU,CAAA;EACV,SAAA,GAAY,eAAA;EACZ,KAAA,GAAQ,WAAA;EACR,MAAA,GAAS,eAAA;AAAA,IAEV,OAAA,CAAQ,SAAA,CAAU,CAAA"}
1
+ {"version":3,"file":"appkit.d.ts","names":[],"sources":["../../src/core/appkit.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgRsB,SAAA,WACV,UAAA,CAAW,iBAAA,qBAAA,CAErB,MAAA;EACE,OAAA,GAAU,CAAA;EACV,SAAA,GAAY,eAAA;EACZ,KAAA,GAAQ,WAAA;EACR,MAAA,GAAS,eAAA;EACT,cAAA,IAAkB,MAAA,EAAQ,SAAA,CAAU,CAAA,aAAc,OAAA;AAAA,IAEnD,OAAA,CAAQ,SAAA,CAAU,CAAA"}
@@ -1,3 +1,4 @@
1
+ import { createLogger } from "../logging/logger.js";
1
2
  import { TelemetryManager } from "../telemetry/telemetry-manager.js";
2
3
  import "../telemetry/index.js";
3
4
  import { CacheManager } from "../cache/index.js";
@@ -9,6 +10,7 @@ import "../registry/index.js";
9
10
 
10
11
  //#region src/core/appkit.ts
11
12
  init_context();
13
+ const logger = createLogger("appkit");
12
14
  var AppKit = class AppKit {
13
15
  #pluginInstances = {};
14
16
  #setupPromises = [];
@@ -99,7 +101,15 @@ var AppKit = class AppKit {
99
101
  registry.enforceValidation();
100
102
  const instance = new AppKit({ plugins: AppKit.preparePlugins(rawPlugins) });
101
103
  await Promise.all(instance.#setupPromises);
102
- return instance;
104
+ const handle = instance;
105
+ if (config.onPluginsReady) {
106
+ logger.debug("Running onPluginsReady hook");
107
+ await config.onPluginsReady(handle);
108
+ logger.debug("onPluginsReady hook completed");
109
+ }
110
+ const serverPlugin = instance.#pluginInstances.server;
111
+ if (serverPlugin && typeof serverPlugin.start === "function") await serverPlugin.start();
112
+ return handle;
103
113
  }
104
114
  static preparePlugins(plugins) {
105
115
  const result = {};
@@ -115,6 +125,9 @@ var AppKit = class AppKit {
115
125
  *
116
126
  * Initializes telemetry, cache, and service context, then registers plugins
117
127
  * in phase order (core, normal, deferred) and awaits their setup.
128
+ * If a `onPluginsReady` callback is provided it runs after plugin setup but
129
+ * before the server starts, giving you access to the full appkit handle
130
+ * for registering custom routes or performing async setup.
118
131
  * The returned object maps each plugin name to its `exports()` API,
119
132
  * with an `asUser(req)` method for user-scoped execution.
120
133
  *
@@ -129,18 +142,18 @@ var AppKit = class AppKit {
129
142
  * });
130
143
  * ```
131
144
  *
132
- * @example Extended Server with analytics and custom endpoint
145
+ * @example Server with custom routes via onPluginsReady
133
146
  * ```ts
134
147
  * import { createApp, server, analytics } from "@databricks/appkit";
135
148
  *
136
- * const appkit = await createApp({
137
- * plugins: [server({ autoStart: false }), analytics({})],
138
- * });
139
- *
140
- * appkit.server.extend((app) => {
141
- * app.get("/custom", (_req, res) => res.json({ ok: true }));
149
+ * await createApp({
150
+ * plugins: [server(), analytics({})],
151
+ * onPluginsReady(appkit) {
152
+ * appkit.server.extend((app) => {
153
+ * app.get("/custom", (_req, res) => res.json({ ok: true }));
154
+ * });
155
+ * },
142
156
  * });
143
- * await appkit.server.start();
144
157
  * ```
145
158
  */
146
159
  async function createApp(config = {}) {
@@ -1 +1 @@
1
- {"version":3,"file":"appkit.js","names":["#pluginInstances","#setupPromises"],"sources":["../../src/core/appkit.ts"],"sourcesContent":["import type { WorkspaceClient } from \"@databricks/sdk-experimental\";\nimport type {\n BasePlugin,\n CacheConfig,\n InputPluginMap,\n OptionalConfigPluginDef,\n PluginConstructor,\n PluginData,\n PluginMap,\n} from \"shared\";\nimport { CacheManager } from \"../cache\";\nimport { ServiceContext } from \"../context\";\nimport { ResourceRegistry, ResourceType } from \"../registry\";\nimport type { TelemetryConfig } from \"../telemetry\";\nimport { TelemetryManager } from \"../telemetry\";\n\nexport class AppKit<TPlugins extends InputPluginMap> {\n #pluginInstances: Record<string, BasePlugin> = {};\n #setupPromises: Promise<void>[] = [];\n\n private constructor(config: { plugins: TPlugins }) {\n const { plugins, ...globalConfig } = config;\n\n const pluginEntries = Object.entries(plugins);\n\n const corePlugins = pluginEntries.filter(([_, p]) => {\n return (p?.plugin?.phase ?? \"normal\") === \"core\";\n });\n const normalPlugins = pluginEntries.filter(\n ([_, p]) => (p?.plugin?.phase ?? \"normal\") === \"normal\",\n );\n const deferredPlugins = pluginEntries.filter(\n ([_, p]) => (p?.plugin?.phase ?? \"normal\") === \"deferred\",\n );\n\n for (const [name, pluginData] of corePlugins) {\n if (pluginData) {\n this.createAndRegisterPlugin(globalConfig, name, pluginData);\n }\n }\n\n for (const [name, pluginData] of normalPlugins) {\n if (pluginData) {\n this.createAndRegisterPlugin(globalConfig, name, pluginData);\n }\n }\n\n for (const [name, pluginData] of deferredPlugins) {\n if (pluginData) {\n this.createAndRegisterPlugin(globalConfig, name, pluginData, {\n plugins: this.#pluginInstances,\n });\n }\n }\n }\n\n private createAndRegisterPlugin<T extends PluginConstructor>(\n config: Omit<{ plugins: TPlugins }, \"plugins\">,\n name: string,\n pluginData: OptionalConfigPluginDef<T>,\n extraData?: Record<string, unknown>,\n ) {\n const { plugin: Plugin, config: pluginConfig } = pluginData;\n const baseConfig = {\n ...config,\n ...Plugin.DEFAULT_CONFIG,\n ...pluginConfig,\n name,\n ...extraData,\n };\n const pluginInstance = new Plugin(baseConfig);\n\n this.#pluginInstances[name] = pluginInstance;\n\n this.#setupPromises.push(pluginInstance.setup());\n\n const self = this;\n\n Object.defineProperty(this, name, {\n get() {\n const plugin = self.#pluginInstances[name];\n return self.wrapWithAsUser(plugin);\n },\n enumerable: true,\n });\n }\n\n /**\n * Binds all function properties in an exports object to the given context.\n * Recurses into plain objects to handle nested APIs (e.g., volume APIs).\n */\n private bindExportMethods(\n exports: Record<string, unknown>,\n context: BasePlugin,\n ) {\n for (const key in exports) {\n if (!Object.hasOwn(exports, key)) continue;\n const val = exports[key];\n if (typeof val === \"function\") {\n exports[key] = (val as (...args: unknown[]) => unknown).bind(context);\n } else if (AppKit.isPlainObject(val)) {\n this.bindExportMethods(val as Record<string, unknown>, context);\n }\n }\n }\n\n /**\n * Wraps a plugin's exports with an `asUser` method that returns\n * a user-scoped version of the exports.\n *\n * When `exports()` returns a callable (function), it is returned as-is\n * since the plugin manages its own `asUser` per-call (e.g. files plugin).\n * When it returns a plain object, the standard `asUser` wrapper is added.\n */\n private wrapWithAsUser<T extends BasePlugin>(plugin: T) {\n // If plugin doesn't implement exports(), return empty object\n const pluginExports = plugin.exports?.() ?? {};\n\n // If exports is a function, the plugin manages its own asUser pattern\n if (typeof pluginExports === \"function\") {\n return pluginExports;\n }\n\n const objExports = pluginExports as Record<string, unknown>;\n this.bindExportMethods(objExports, plugin);\n\n // If plugin doesn't support asUser (no asUser method), return exports as-is\n if (typeof (plugin as any).asUser !== \"function\") {\n return objExports;\n }\n\n return {\n ...objExports,\n /**\n * Execute operations using the user's identity from the request.\n * Returns user-scoped exports where all methods execute with the\n * user's Databricks credentials instead of the service principal.\n */\n asUser: (req: import(\"express\").Request) => {\n const userPlugin = (plugin as any).asUser(req);\n const userExports = (userPlugin.exports?.() ?? {}) as Record<\n string,\n unknown\n >;\n this.bindExportMethods(userExports, userPlugin);\n return userExports;\n },\n };\n }\n\n /**\n * Returns true if the value is a plain object (not an array, Date, etc.).\n */\n private static isPlainObject(\n value: unknown,\n ): value is Record<string, unknown> {\n if (typeof value !== \"object\" || value === null) return false;\n const proto = Object.getPrototypeOf(value);\n return proto === Object.prototype || proto === null;\n }\n\n static async _createApp<\n T extends PluginData<PluginConstructor, unknown, string>[],\n >(\n config: {\n plugins?: T;\n telemetry?: TelemetryConfig;\n cache?: CacheConfig;\n client?: WorkspaceClient;\n } = {},\n ): Promise<PluginMap<T>> {\n // Initialize core services\n TelemetryManager.initialize(config?.telemetry);\n await CacheManager.getInstance(config?.cache);\n\n const rawPlugins = config.plugins as T;\n\n // Collect manifest resources via registry\n const registry = new ResourceRegistry();\n registry.collectResources(rawPlugins);\n\n // Derive ServiceContext needs from what manifests declared\n const needsWarehouse = registry\n .getRequired()\n .some((r) => r.type === ResourceType.SQL_WAREHOUSE);\n await ServiceContext.initialize(\n { warehouseId: needsWarehouse },\n config?.client,\n );\n\n // Validate env vars\n registry.enforceValidation();\n\n const preparedPlugins = AppKit.preparePlugins(rawPlugins);\n const mergedConfig = {\n plugins: preparedPlugins,\n };\n\n const instance = new AppKit(mergedConfig);\n\n await Promise.all(instance.#setupPromises);\n\n return instance as unknown as PluginMap<T>;\n }\n\n private static preparePlugins(\n plugins: PluginData<PluginConstructor, unknown, string>[],\n ) {\n const result: InputPluginMap = {};\n for (const currentPlugin of plugins) {\n result[currentPlugin.name] = {\n plugin: currentPlugin.plugin,\n config: currentPlugin.config as Record<string, unknown>,\n };\n }\n return result;\n }\n}\n\n/**\n * Bootstraps AppKit with the provided configuration.\n *\n * Initializes telemetry, cache, and service context, then registers plugins\n * in phase order (core, normal, deferred) and awaits their setup.\n * The returned object maps each plugin name to its `exports()` API,\n * with an `asUser(req)` method for user-scoped execution.\n *\n * @returns A `PluginMap` keyed by plugin name with typed exports\n *\n * @example Minimal server\n * ```ts\n * import { createApp, server } from \"@databricks/appkit\";\n *\n * await createApp({\n * plugins: [server()],\n * });\n * ```\n *\n * @example Extended Server with analytics and custom endpoint\n * ```ts\n * import { createApp, server, analytics } from \"@databricks/appkit\";\n *\n * const appkit = await createApp({\n * plugins: [server({ autoStart: false }), analytics({})],\n * });\n *\n * appkit.server.extend((app) => {\n * app.get(\"/custom\", (_req, res) => res.json({ ok: true }));\n * });\n * await appkit.server.start();\n * ```\n */\nexport async function createApp<\n T extends PluginData<PluginConstructor, unknown, string>[],\n>(\n config: {\n plugins?: T;\n telemetry?: TelemetryConfig;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,SAFsB,OAAO,eAAe,WAAW,EAGxD,CAEwC;AAEzC,QAAM,QAAQ,IAAI,UAASC,cAAe;AAE1C,SAAO;;CAGT,OAAe,eACb,SACA;EACA,MAAM,SAAyB,EAAE;AACjC,OAAK,MAAM,iBAAiB,QAC1B,QAAO,cAAc,QAAQ;GAC3B,QAAQ,cAAc;GACtB,QAAQ,cAAc;GACvB;AAEH,SAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCX,eAAsB,UAGpB,SAKI,EAAE,EACiB;AACvB,QAAO,OAAO,WAAW,OAAO"}
1
+ {"version":3,"file":"appkit.js","names":["#pluginInstances","#setupPromises"],"sources":["../../src/core/appkit.ts"],"sourcesContent":["import type { WorkspaceClient } from \"@databricks/sdk-experimental\";\nimport type {\n BasePlugin,\n CacheConfig,\n InputPluginMap,\n OptionalConfigPluginDef,\n PluginConstructor,\n PluginData,\n PluginMap,\n} from \"shared\";\nimport { CacheManager } from \"../cache\";\nimport { ServiceContext } from \"../context\";\nimport { createLogger } from \"../logging/logger\";\nimport { ResourceRegistry, ResourceType } from \"../registry\";\nimport type { TelemetryConfig } from \"../telemetry\";\nimport { TelemetryManager } from \"../telemetry\";\n\nconst logger = createLogger(\"appkit\");\n\nexport class AppKit<TPlugins extends InputPluginMap> {\n #pluginInstances: Record<string, BasePlugin> = {};\n #setupPromises: Promise<void>[] = [];\n\n private constructor(config: { plugins: TPlugins }) {\n const { plugins, ...globalConfig } = config;\n\n const pluginEntries = Object.entries(plugins);\n\n const corePlugins = pluginEntries.filter(([_, p]) => {\n return (p?.plugin?.phase ?? \"normal\") === \"core\";\n });\n const normalPlugins = pluginEntries.filter(\n ([_, p]) => (p?.plugin?.phase ?? \"normal\") === \"normal\",\n );\n const deferredPlugins = pluginEntries.filter(\n ([_, p]) => (p?.plugin?.phase ?? \"normal\") === \"deferred\",\n );\n\n for (const [name, pluginData] of corePlugins) {\n if (pluginData) {\n this.createAndRegisterPlugin(globalConfig, name, pluginData);\n }\n }\n\n for (const [name, pluginData] of normalPlugins) {\n if (pluginData) {\n this.createAndRegisterPlugin(globalConfig, name, pluginData);\n }\n }\n\n for (const [name, pluginData] of deferredPlugins) {\n if (pluginData) {\n this.createAndRegisterPlugin(globalConfig, name, pluginData, {\n plugins: this.#pluginInstances,\n });\n }\n }\n }\n\n private createAndRegisterPlugin<T extends PluginConstructor>(\n config: Omit<{ plugins: TPlugins }, \"plugins\">,\n name: string,\n pluginData: OptionalConfigPluginDef<T>,\n extraData?: Record<string, unknown>,\n ) {\n const { plugin: Plugin, config: pluginConfig } = pluginData;\n const baseConfig = {\n ...config,\n ...Plugin.DEFAULT_CONFIG,\n ...pluginConfig,\n name,\n ...extraData,\n };\n const pluginInstance = new Plugin(baseConfig);\n\n this.#pluginInstances[name] = pluginInstance;\n\n this.#setupPromises.push(pluginInstance.setup());\n\n const self = this;\n\n Object.defineProperty(this, name, {\n get() {\n const plugin = self.#pluginInstances[name];\n return self.wrapWithAsUser(plugin);\n },\n enumerable: true,\n });\n }\n\n /**\n * Binds all function properties in an exports object to the given context.\n * Recurses into plain objects to handle nested APIs (e.g., volume APIs).\n */\n private bindExportMethods(\n exports: Record<string, unknown>,\n context: BasePlugin,\n ) {\n for (const key in exports) {\n if (!Object.hasOwn(exports, key)) continue;\n const val = exports[key];\n if (typeof val === \"function\") {\n exports[key] = (val as (...args: unknown[]) => unknown).bind(context);\n } else if (AppKit.isPlainObject(val)) {\n this.bindExportMethods(val as Record<string, unknown>, context);\n }\n }\n }\n\n /**\n * Wraps a plugin's exports with an `asUser` method that returns\n * a user-scoped version of the exports.\n *\n * When `exports()` returns a callable (function), it is returned as-is\n * since the plugin manages its own `asUser` per-call (e.g. files plugin).\n * When it returns a plain object, the standard `asUser` wrapper is added.\n */\n private wrapWithAsUser<T extends BasePlugin>(plugin: T) {\n // If plugin doesn't implement exports(), return empty object\n const pluginExports = plugin.exports?.() ?? {};\n\n // If exports is a function, the plugin manages its own asUser pattern\n if (typeof pluginExports === \"function\") {\n return pluginExports;\n }\n\n const objExports = pluginExports as Record<string, unknown>;\n this.bindExportMethods(objExports, plugin);\n\n // If plugin doesn't support asUser (no asUser method), return exports as-is\n if (typeof (plugin as any).asUser !== \"function\") {\n return objExports;\n }\n\n return {\n ...objExports,\n /**\n * Execute operations using the user's identity from the request.\n * Returns user-scoped exports where all methods execute with the\n * user's Databricks credentials instead of the service principal.\n */\n asUser: (req: import(\"express\").Request) => {\n const userPlugin = (plugin as any).asUser(req);\n const userExports = (userPlugin.exports?.() ?? {}) as Record<\n string,\n unknown\n >;\n this.bindExportMethods(userExports, userPlugin);\n return userExports;\n },\n };\n }\n\n /**\n * Returns true if the value is a plain object (not an array, Date, etc.).\n */\n private static isPlainObject(\n value: unknown,\n ): value is Record<string, unknown> {\n if (typeof value !== \"object\" || value === null) return false;\n const proto = Object.getPrototypeOf(value);\n return proto === Object.prototype || proto === null;\n }\n\n static async _createApp<\n T extends PluginData<PluginConstructor, unknown, string>[],\n >(\n config: {\n plugins?: T;\n telemetry?: TelemetryConfig;\n cache?: CacheConfig;\n client?: WorkspaceClient;\n onPluginsReady?: (appkit: PluginMap<T>) => void | Promise<void>;\n } = {},\n ): Promise<PluginMap<T>> {\n // Initialize core services\n TelemetryManager.initialize(config?.telemetry);\n await CacheManager.getInstance(config?.cache);\n\n const rawPlugins = config.plugins as T;\n\n // Collect manifest resources via registry\n const registry = new ResourceRegistry();\n registry.collectResources(rawPlugins);\n\n // Derive ServiceContext needs from what manifests declared\n const needsWarehouse = registry\n .getRequired()\n .some((r) => r.type === ResourceType.SQL_WAREHOUSE);\n await ServiceContext.initialize(\n { warehouseId: needsWarehouse },\n config?.client,\n );\n\n // Validate env vars\n registry.enforceValidation();\n\n const preparedPlugins = AppKit.preparePlugins(rawPlugins);\n const mergedConfig = {\n plugins: preparedPlugins,\n };\n\n const instance = new AppKit(mergedConfig);\n\n await Promise.all(instance.#setupPromises);\n\n const handle = instance as unknown as PluginMap<T>;\n\n if (config.onPluginsReady) {\n logger.debug(\"Running onPluginsReady hook\");\n await config.onPluginsReady(handle);\n logger.debug(\"onPluginsReady hook completed\");\n }\n\n const serverPlugin = instance.#pluginInstances.server;\n if (serverPlugin && typeof (serverPlugin as any).start === \"function\") {\n await (serverPlugin as any).start();\n }\n\n return handle;\n }\n\n private static preparePlugins(\n plugins: PluginData<PluginConstructor, unknown, string>[],\n ) {\n const result: InputPluginMap = {};\n for (const currentPlugin of plugins) {\n result[currentPlugin.name] = {\n plugin: currentPlugin.plugin,\n config: currentPlugin.config as Record<string, unknown>,\n };\n }\n return result;\n }\n}\n\n/**\n * Bootstraps AppKit with the provided configuration.\n *\n * Initializes telemetry, cache, and service context, then registers plugins\n * in phase order (core, normal, deferred) and awaits their setup.\n * If a `onPluginsReady` callback is provided it runs after plugin setup but\n * before the server starts, giving you access to the full appkit handle\n * for registering custom routes or performing async setup.\n * The returned object maps each plugin name to its `exports()` API,\n * with an `asUser(req)` method for user-scoped execution.\n *\n * @returns A `PluginMap` keyed by plugin name with typed exports\n *\n * @example Minimal server\n * ```ts\n * import { createApp, server } from \"@databricks/appkit\";\n *\n * await createApp({\n * plugins: [server()],\n * });\n * ```\n *\n * @example Server with custom routes via onPluginsReady\n * ```ts\n * import { createApp, server, analytics } from \"@databricks/appkit\";\n *\n * await createApp({\n * plugins: [server(), analytics({})],\n * onPluginsReady(appkit) {\n * appkit.server.extend((app) => {\n * app.get(\"/custom\", (_req, res) => res.json({ ok: true }));\n * });\n * },\n * });\n * ```\n */\nexport async function createApp<\n T extends PluginData<PluginConstructor, unknown, string>[],\n>(\n config: {\n plugins?: T;\n telemetry?: TelemetryConfig;\n cache?: CacheConfig;\n client?: WorkspaceClient;\n onPluginsReady?: (appkit: PluginMap<T>) => void | Promise<void>;\n } = {},\n): Promise<PluginMap<T>> {\n return AppKit._createApp(config);\n}\n"],"mappings":";;;;;;;;;;;cAW4C;AAM5C,MAAM,SAAS,aAAa,SAAS;AAErC,IAAa,SAAb,MAAa,OAAwC;CACnD,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