11import fs from "node:fs" ;
2- import path from "node:path" ;
32import dotenv from "dotenv" ;
4- import ts from "typescript" ;
5- import {
6- extractRoutesFromFile ,
7- findPluginFiles ,
8- getAllImportedFiles ,
9- type PluginRoutes ,
10- } from "./plugin-registry" ;
113import { generateQueriesFromExplain , type QuerySchema } from "./query-registry" ;
124
135dotenv . config ( ) ;
146
157/**
16- * Generate type declarations for PluginRegistry and QueryRegistry
8+ * Generate type declarations for QueryRegistry
179 * Create the d.ts file from the plugin routes and query schemas
18- * @param plugins - the list of plugin routes
1910 * @param querySchemas - the list of query schemas
2011 * @returns - the type declarations as a string
2112 */
22- function generateTypeDeclarations (
23- plugins : PluginRoutes [ ] ,
24- querySchemas : QuerySchema [ ] = [ ] ,
25- ) : string {
26- const registryEntries = plugins
27- . map ( ( { pluginName, routes } ) => {
28- const routeEntries = routes
29- . map ( ( { path, responseType } ) => {
30- const indentedType = responseType
31- . split ( "\n" )
32- . map ( ( line , i ) => ( i === 0 ? line : ` ${ line } ` ) )
33- . join ( "\n" ) ;
34- return ` "${ path } ": ${ indentedType } ` ;
35- } )
36- . join ( ";\n" ) ;
37-
38- return ` "${ pluginName } ": {\n${ routeEntries } ;\n }` ;
39- } )
40- . join ( ";\n" ) ;
41-
13+ function generateTypeDeclarations ( querySchemas : QuerySchema [ ] = [ ] ) : string {
4214 const queryEntries = querySchemas
4315 . map ( ( { name, type } ) => {
4416 const indentedType = type
@@ -56,9 +28,6 @@ function generateTypeDeclarations(
5628import "@databricks/app-kit-ui/react";
5729
5830declare module "@databricks/app-kit-ui/react" {
59- interface PluginRegistry {
60- ${ registryEntries } ;
61- }
6231 interface QueryRegistry {${ querySection } }
6332}
6433` ;
@@ -72,61 +41,22 @@ ${registryEntries};
7241 * @param options.querySchemaFile - optional path to query schema file (e.g. config/queries/schema.ts)
7342 */
7443export async function generateFromEntryPoint ( options : {
75- pluginEntryPoint : string ;
7644 outFile : string ;
7745 queryFolder ?: string ;
7846 warehouseId : string ;
7947 noCache ?: boolean ;
8048} ) {
81- const { pluginEntryPoint, outFile, queryFolder, warehouseId, noCache } =
82- options ;
49+ const { outFile, queryFolder, warehouseId, noCache } = options ;
8350
8451 console . log ( "\n[AppKit] Starting type generation...\n" ) ;
8552
86- const allFiles = getAllImportedFiles ( pluginEntryPoint ) ;
87-
88- const configPath = ts . findConfigFile (
89- path . dirname ( pluginEntryPoint ) ,
90- ts . sys . fileExists ,
91- "tsconfig.json" ,
92- ) ;
93-
94- const configFile = configPath
95- ? ts . readConfigFile ( configPath , ts . sys . readFile )
96- : { config : { } } ;
97-
98- const parsedConfig = ts . parseJsonConfigFileContent (
99- configFile . config ,
100- ts . sys ,
101- path . dirname ( configPath || pluginEntryPoint ) ,
102- ) ;
103-
104- const program = ts . createProgram ( allFiles , {
105- ...parsedConfig . options ,
106- noEmit : true ,
107- } ) ;
108-
109- const pluginFiles = findPluginFiles ( allFiles , program ) ;
110-
111- const allPluginRoutes : PluginRoutes [ ] = [ ] ;
112-
113- for ( const pluginFile of pluginFiles ) {
114- const routes = extractRoutesFromFile ( pluginFile , program ) ;
115- if ( routes ) {
116- allPluginRoutes . push ( routes ) ;
117- }
118- }
119-
12053 let queryRegistry : QuerySchema [ ] = [ ] ;
12154 if ( queryFolder )
12255 queryRegistry = await generateQueriesFromExplain ( queryFolder , warehouseId , {
12356 noCache,
12457 } ) ;
12558
126- const typeDeclarations = generateTypeDeclarations (
127- allPluginRoutes ,
128- queryRegistry ,
129- ) ;
59+ const typeDeclarations = generateTypeDeclarations ( queryRegistry ) ;
13060
13161 fs . writeFileSync ( outFile , typeDeclarations , "utf-8" ) ;
13262
0 commit comments