@@ -28,7 +28,6 @@ const writeAsync = promisify(write);
2828
2929@Injectable ( )
3030export class FileManagerService {
31- private configFile = '.cortexrc' ;
3231 private cortexDirectoryName = 'cortex' ;
3332 private modelFolderName = 'models' ;
3433 private presetFolderName = 'presets' ;
@@ -44,7 +43,10 @@ export class FileManagerService {
4443 */
4544 async getConfig ( dataFolderPath ?: string ) : Promise < Config & object > {
4645 const homeDir = os . homedir ( ) ;
47- const configPath = join ( homeDir , this . configFile ) ;
46+ const configPath = join (
47+ homeDir ,
48+ this . getConfigFileName ( this . configProfile ) ,
49+ ) ;
4850 const config = this . defaultConfig ( ) ;
4951 const dataFolderPathUsed = dataFolderPath || config . dataFolderPath ;
5052 if ( ! existsSync ( configPath ) || ! existsSync ( dataFolderPathUsed ) ) {
@@ -55,8 +57,7 @@ export class FileManagerService {
5557
5658 try {
5759 const content = await promises . readFile ( configPath , 'utf8' ) ;
58- const configs = yaml . load ( content ) as Record < string , Config > ;
59- const config = configs ?. [ this . configProfile ] ?? { } ;
60+ const config = yaml . load ( content ) as Config & object ;
6061 return {
6162 ...this . defaultConfig ( ) ,
6263 ...config ,
@@ -72,17 +73,20 @@ export class FileManagerService {
7273
7374 async writeConfigFile ( config : Config & object ) : Promise < void > {
7475 const homeDir = os . homedir ( ) ;
75- const configPath = join ( homeDir , this . configFile ) ;
76+ const configPath = join (
77+ homeDir ,
78+ this . getConfigFileName ( this . configProfile ) ,
79+ ) ;
7680
7781 // write config to file as yaml
7882 if ( ! existsSync ( configPath ) ) {
7983 await promises . writeFile ( configPath , '' , 'utf8' ) ;
8084 }
8185 const content = await promises . readFile ( configPath , 'utf8' ) ;
82- const currentConfig = yaml . load ( content ) as Record < string , Config > ;
86+ const currentConfig = yaml . load ( content ) as Config & object ;
8387 const configString = yaml . dump ( {
8488 ...currentConfig ,
85- [ this . configProfile ] : config ,
89+ ... config ,
8690 } ) ;
8791 await promises . writeFile ( configPath , configString , 'utf8' ) ;
8892 }
@@ -345,12 +349,17 @@ export class FileManagerService {
345349 */
346350 getServerConfig ( ) : { host : string ; port : number } {
347351 const homeDir = os . homedir ( ) ;
348- const configPath = join ( homeDir , this . configFile ) ;
352+ const configPath = join (
353+ homeDir ,
354+ this . getConfigFileName ( this . configProfile ) ,
355+ ) ;
349356 let config = this . defaultConfig ( ) ;
350357 try {
351358 const content = readFileSync ( configPath , 'utf8' ) ;
352- const configs = ( yaml . load ( content ) as Record < string , Config > ) ?? { } ;
353- config = configs ?. [ this . configProfile ] ?? config ;
359+ const currentConfig = ( yaml . load ( content ) as Config & object ) ?? { } ;
360+ if ( currentConfig ) {
361+ config = currentConfig ;
362+ }
354363 } catch { }
355364 return {
356365 host : config . apiServerHost ?? '127.0.0.1' ,
@@ -366,15 +375,22 @@ export class FileManagerService {
366375 }
367376 public profileConfigExists ( profile : string ) : boolean {
368377 const homeDir = os . homedir ( ) ;
369- const configPath = join ( homeDir , this . configFile ) ;
378+ const configPath = join ( homeDir , this . getConfigFileName ( profile ) ) ;
370379 try {
371380 const content = readFileSync ( configPath , 'utf8' ) ;
372- const configs = ( yaml . load ( content ) as Record < string , Config > ) ?? { } ;
373- return ! ! configs [ profile ] ;
381+ const config = yaml . load ( content ) as Config & object ;
382+ return ! ! config ;
374383 } catch {
375384 return false ;
376385 }
377386 }
387+
388+ private getConfigFileName ( configProfile : string ) : string {
389+ if ( configProfile === 'default' ) {
390+ return '.cortexrc' ;
391+ }
392+ return `.${ configProfile } rc` ;
393+ }
378394}
379395
380396export const fileManagerService = new FileManagerService ( ) ;
0 commit comments