@@ -13,7 +13,7 @@ export type SSHConfig = {
1313 passphrase ?: string
1414 identityFile ?: string
1515}
16- export type Settings = {
16+ export type Connection = {
1717 name : string | null ,
1818 adapter : 'mysql' | 'postgresql' | 'postgres' | 'sqlite3' | null ,
1919 host : string | null
@@ -27,7 +27,7 @@ export type Settings = {
2727}
2828
2929type PersonalConfig = {
30- connections : Settings [ ]
30+ connections : Connection [ ]
3131}
3232
3333function fileExists ( path : string ) {
@@ -47,7 +47,7 @@ function readFile(filePath: string) {
4747
4848export default class SettingStore extends EventEmitter . EventEmitter {
4949 private personalConfig : PersonalConfig = { connections : [ ] }
50- private state : Settings = {
50+ private state : Connection = {
5151 name : null ,
5252 adapter : null ,
5353 host : null ,
@@ -83,7 +83,7 @@ export default class SettingStore extends EventEmitter.EventEmitter {
8383 const config = this . personalConfig . connections . find ( v => v . name === connectionName )
8484 if ( ! config ) {
8585 const errorMessage = `not find connection name: ${ connectionName } `
86- logger . error ( `not find connection name: ${ connectionName } ` )
86+ logger . error ( errorMessage )
8787 throw new Error ( errorMessage )
8888 }
8989 this . setSetting ( config )
@@ -93,8 +93,8 @@ export default class SettingStore extends EventEmitter.EventEmitter {
9393 personalConfigPath : string ,
9494 projectConfigPath : string ,
9595 projectPath : string
96- ) : Promise < Settings | null > {
97- let personalConfig = { connections : [ ] } as PersonalConfig , projectConfig = { } as Settings
96+ ) : Promise < Connection | null > {
97+ let personalConfig = { connections : [ ] } as PersonalConfig , projectConfig = { } as Connection
9898 if ( fileExists ( personalConfigPath ) ) {
9999 personalConfig = JSON . parse ( readFile ( personalConfigPath ) )
100100 this . personalConfig = personalConfig
@@ -107,21 +107,25 @@ export default class SettingStore extends EventEmitter.EventEmitter {
107107 logger . debug ( `There isn't project config file., ${ projectConfigPath } ` )
108108 }
109109 const extractedPersonalConfig = projectConfig . name
110- ? personalConfig . connections . find ( ( v : Settings ) => v . name === projectConfig . name )
111- : personalConfig . connections . find ( ( v : Settings ) => v . projectPaths ?. includes ( projectPath ) )
112- if ( ! extractedPersonalConfig ) {
113- logger . debug ( `Failed to extract personal config, { path: ${ projectPath } , projectName: ${ projectConfig . name } }` )
114- }
110+ ? personalConfig . connections . find ( ( v : Connection ) => v . name === projectConfig . name )
111+ : this . extractPersonalConfigMatchedProjectPath ( projectPath )
115112
116113 const sshConfig = { ...extractedPersonalConfig ?. ssh || { } , ...projectConfig ?. ssh || { } } as SSHConfig
117114 const config = { ...extractedPersonalConfig , ...projectConfig }
118115 config . ssh = sshConfig
119- logger . debug ( `Set config: ${ JSON . stringify ( config ) } ` )
120116 this . setSetting ( config )
121117 return this . getSetting ( )
122118 }
123119
124- setSetting ( setting : Partial < Settings > ) {
120+ async setSettingFromWorkspaceConfig ( connections : Connection [ ] , projectPath : string = '' ) {
121+ this . personalConfig = { connections }
122+ const extractedPersonalConfig = this . extractPersonalConfigMatchedProjectPath ( projectPath )
123+ this . setSetting ( extractedPersonalConfig || { } )
124+ return this . getSetting ( )
125+ }
126+
127+ setSetting ( setting : Partial < Connection > ) {
128+ logger . debug ( `Set config: ${ JSON . stringify ( setting ) } ` )
125129 const replaceEnv = ( v : { [ key : string ] : any } ) => {
126130 for ( const k in v ) {
127131 if ( v [ k ] && typeof v [ k ] === 'object' ) {
@@ -141,4 +145,12 @@ export default class SettingStore extends EventEmitter.EventEmitter {
141145 logger . debug ( 'setting store, emit "change"' )
142146 this . emit ( 'change' , this . state )
143147 }
148+
149+ private extractPersonalConfigMatchedProjectPath ( projectPath : string ) {
150+ const con = this . personalConfig . connections . find ( ( v : Connection ) => v . projectPaths ?. includes ( projectPath ) )
151+ if ( ! con ) {
152+ logger . debug ( `Not to extract personal config, { path: ${ projectPath } , projectName: ${ projectPath } }` )
153+ }
154+ return con
155+ }
144156}
0 commit comments