1
- import fs from 'fs' ;
2
- import path from 'path' ;
3
- import { Plugin } from './types' ;
4
- import { DatabasePlugin } from '../databases/types' ;
1
+ import { BaseDatabasePlugin } from '../databases/core/base-plugin' ;
2
+ import { DatabaseAnswers , Question } from '../databases/core/types' ;
3
+ import { PostgreSQLPlugin } from '../databases/postgresql' ;
4
+ import { MySQLPlugin } from '../databases/mysql' ;
5
+ import { MariaDBPlugin } from '../databases/mariadb' ;
6
+ import { SQLitePlugin } from '../databases/sqlite' ;
5
7
6
8
export class PluginManager {
7
- private plugins : Map < string , Plugin > ;
8
- private pluginsPath : string ;
9
+ private plugins : Map < string , BaseDatabasePlugin > ;
9
10
10
- constructor ( pluginsPath : string ) {
11
+ constructor ( ) {
11
12
this . plugins = new Map ( ) ;
12
- this . pluginsPath = pluginsPath ;
13
+ this . loadBuiltinPlugins ( ) ;
13
14
}
14
15
15
- // Load all plugins from the plugins directory
16
- async loadPlugins ( ) : Promise < void > {
17
- try {
18
- // Get all database plugin directories
19
- const databasesPath = path . join ( this . pluginsPath , 'databases' ) ;
20
- const entries = fs . readdirSync ( databasesPath , { withFileTypes : true } ) ;
21
-
22
- for ( const entry of entries ) {
23
- if ( entry . isDirectory ( ) ) {
24
- const pluginPath = path . join ( databasesPath , entry . name ) ;
25
- await this . loadPlugin ( pluginPath ) ;
26
- }
27
- }
28
- } catch ( error ) {
29
- console . error ( 'Error loading plugins:' , error ) ;
30
- }
31
- }
16
+ private loadBuiltinPlugins ( ) : void {
17
+ // Load built-in plugins
18
+ const builtinPlugins = [
19
+ new PostgreSQLPlugin ( ) ,
20
+ new MySQLPlugin ( ) ,
21
+ new MariaDBPlugin ( ) ,
22
+ new SQLitePlugin ( )
23
+ ] as BaseDatabasePlugin [ ] ;
32
24
33
- // Load a specific plugin from a directory
34
- private async loadPlugin ( pluginPath : string ) : Promise < void > {
35
- try {
36
- const indexPath = path . join ( pluginPath , 'index.ts' ) ;
37
- if ( fs . existsSync ( indexPath ) ) {
38
- const plugin = await import ( indexPath ) ;
39
- const pluginInstance = this . instantiatePlugin ( plugin ) ;
40
- if ( pluginInstance ) {
41
- this . plugins . set ( pluginInstance . name , pluginInstance ) ;
42
- }
43
- }
44
- } catch ( error ) {
45
- console . error ( `Error loading plugin from ${ pluginPath } :` , error ) ;
25
+ for ( const plugin of builtinPlugins ) {
26
+ this . plugins . set ( plugin . constructor . name . toLowerCase ( ) . replace ( 'plugin' , '' ) , plugin ) ;
46
27
}
47
28
}
48
29
49
- // Create an instance of the plugin
50
- private instantiatePlugin ( plugin : any ) : Plugin | null {
51
- const PluginClass = Object . values ( plugin ) [ 0 ] ;
52
- if ( typeof PluginClass === 'function' ) {
53
- try {
54
- return new PluginClass ( ) ;
55
- } catch ( error ) {
56
- console . error ( 'Error instantiating plugin:' , error ) ;
57
- }
58
- }
59
- return null ;
30
+ /**
31
+ * Get a list of available database types
32
+ */
33
+ getDatabaseTypes ( ) : string [ ] {
34
+ return Array . from ( this . plugins . keys ( ) ) ;
60
35
}
61
36
62
- // Get a specific database plugin
63
- getDatabasePlugin ( name : string ) : DatabasePlugin | undefined {
64
- const plugin = this . plugins . get ( name ) ;
65
- if ( plugin ?. type === 'database' ) {
66
- return plugin as DatabasePlugin ;
67
- }
68
- return undefined ;
37
+ /**
38
+ * Get a plugin by database type
39
+ */
40
+ getPlugin ( type : string ) : BaseDatabasePlugin | undefined {
41
+ return this . plugins . get ( type . toLowerCase ( ) ) ;
69
42
}
70
43
71
- // Get all available database plugins
72
- getAllDatabasePlugins ( ) : DatabasePlugin [ ] {
73
- return Array . from ( this . plugins . values ( ) )
74
- . filter ( plugin => plugin . type === 'database' ) as DatabasePlugin [ ] ;
75
- }
76
-
77
- // Load templates for a specific plugin
78
- async loadTemplates ( pluginName : string ) : Promise < Record < string , string > > {
79
- const templates : Record < string , string > = { } ;
80
- const plugin = this . plugins . get ( pluginName ) ;
81
-
44
+ /**
45
+ * Get questions for a specific database type
46
+ */
47
+ async getQuestions ( type : string ) : Promise < Question [ ] > {
48
+ const plugin = this . getPlugin ( type ) ;
82
49
if ( ! plugin ) {
83
- return templates ;
84
- }
85
-
86
- const templatesPath = path . join ( this . pluginsPath , plugin . type + 's' , pluginName , 'templates' ) ;
87
- if ( ! fs . existsSync ( templatesPath ) ) {
88
- return templates ;
50
+ throw new Error ( `Unknown database type: ${ type } ` ) ;
89
51
}
52
+ return plugin . getQuestions ( ) ;
53
+ }
90
54
91
- const files = fs . readdirSync ( templatesPath ) ;
92
- for ( const file of files ) {
93
- if ( file . endsWith ( '.liquid' ) ) {
94
- const templateName = path . basename ( file , '.liquid' ) ;
95
- const templateContent = fs . readFileSync ( path . join ( templatesPath , file ) , 'utf-8' ) ;
96
- templates [ templateName ] = templateContent ;
97
- }
55
+ /**
56
+ * Process answers for a specific database type
57
+ */
58
+ processAnswers ( type : string , answers : Record < string , any > ) : DatabaseAnswers {
59
+ const plugin = this . getPlugin ( type ) ;
60
+ if ( ! plugin ) {
61
+ throw new Error ( `Unknown database type: ${ type } ` ) ;
98
62
}
99
-
100
- return templates ;
63
+ return plugin . processAnswers ( answers ) ;
101
64
}
102
65
103
- // Generate Docker configuration based on selected database
104
- async generateDockerConfig ( databaseType : string , answers : Record < string , any > ) : Promise < {
105
- templates : Record < string , string > ;
106
- variables : Record < string , any > ;
107
- } > {
108
- const plugin = this . getDatabasePlugin ( databaseType ) ;
66
+ /**
67
+ * Get template variables for a specific database type
68
+ */
69
+ getTemplateVariables ( type : string , answers : DatabaseAnswers ) : Record < string , any > {
70
+ const plugin = this . getPlugin ( type ) ;
109
71
if ( ! plugin ) {
110
- throw new Error ( `Database plugin ${ databaseType } not found ` ) ;
72
+ throw new Error ( `Unknown database type: ${ type } ` ) ;
111
73
}
112
-
113
- // Load templates
114
- const templates = await this . loadTemplates ( databaseType ) ;
115
-
116
- // Get template variables from plugin
117
- const templateData = plugin . getTemplates ( answers ) [ 0 ] ; // Get first template for now
118
-
119
- return {
120
- templates,
121
- variables : templateData . variables
122
- } ;
74
+ return plugin . getTemplateVariables ( answers ) ;
123
75
}
124
76
}
0 commit comments