Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 71ed05e

Browse files
feat: support to change config path (#1009)
1 parent f38d9dc commit 71ed05e

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

cortex-js/src/infrastructure/commanders/cortex-command.commander.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type ServeOptions = {
3232
dataFolder?: string;
3333
version?: boolean;
3434
name?: string;
35+
configPath?: string;
3536
enginePort?: string;
3637
};
3738

@@ -67,6 +68,12 @@ export class CortexCommand extends CommandRunner {
6768
}
6869

6970
async run(passedParams: string[], options?: ServeOptions): Promise<void> {
71+
if (options?.configPath) {
72+
fileManagerService.setConfigPath(options.configPath);
73+
}
74+
if (options?.name) {
75+
fileManagerService.setConfigProfile(options.name);
76+
}
7077
if (options?.name) {
7178
const isProfileConfigExists = fileManagerService.profileConfigExists(
7279
options.name,
@@ -192,13 +199,21 @@ export class CortexCommand extends CommandRunner {
192199
}
193200

194201
@Option({
195-
flags: '-df, --dataFolder <dataFolderPath>',
202+
flags: '-df, --dataFolder <dataFolder>',
196203
description: 'Set the data folder directory',
197204
})
198205
parseDataFolder(value: string) {
199206
return value;
200207
}
201208

209+
@Option({
210+
flags: '-cp, --configPath <configPath>',
211+
description: 'Set the config folder directory',
212+
})
213+
parseConfigFolder(value: string) {
214+
return value;
215+
}
216+
202217
@Option({
203218
flags: '-v, --version',
204219
description: 'Show version',
@@ -212,7 +227,6 @@ export class CortexCommand extends CommandRunner {
212227
description: 'Name of the process',
213228
})
214229
parseName(value: string) {
215-
fileManagerService.setConfigProfile(value);
216230
return value;
217231
}
218232

cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,26 @@ export class FileManagerService {
3636
private cortexEnginesFolderName = 'engines';
3737
private cortexTelemetryFolderName = 'telemetry';
3838
private configProfile = process.env.CORTEX_PROFILE || 'default';
39+
private configPath = process.env.CORTEX_CONFIG_PATH || os.homedir();
3940

4041
/**
4142
* Get cortex configs
4243
* @returns the config object
4344
*/
4445
async getConfig(dataFolderPath?: string): Promise<Config & object> {
45-
const homeDir = os.homedir();
4646
const configPath = join(
47-
homeDir,
47+
this.configPath,
4848
this.getConfigFileName(this.configProfile),
4949
);
5050
const config = this.defaultConfig();
5151
const dataFolderPathUsed = dataFolderPath || config.dataFolderPath;
5252
if (!existsSync(configPath) || !existsSync(dataFolderPathUsed)) {
53-
await this.createFolderIfNotExist(dataFolderPathUsed);
54-
await this.writeConfigFile(config);
53+
if (!existsSync(dataFolderPathUsed)) {
54+
await this.createFolderIfNotExist(dataFolderPathUsed);
55+
}
56+
if (!existsSync(configPath)) {
57+
await this.writeConfigFile(config);
58+
}
5559
return config;
5660
}
5761

@@ -72,9 +76,8 @@ export class FileManagerService {
7276
}
7377

7478
async writeConfigFile(config: Config & object): Promise<void> {
75-
const homeDir = os.homedir();
7679
const configPath = join(
77-
homeDir,
80+
this.configPath,
7881
this.getConfigFileName(this.configProfile),
7982
);
8083

@@ -348,9 +351,8 @@ export class FileManagerService {
348351
* @returns the server configurations
349352
*/
350353
getServerConfig(): { host: string; port: number } {
351-
const homeDir = os.homedir();
352354
const configPath = join(
353-
homeDir,
355+
this.configPath,
354356
this.getConfigFileName(this.configProfile),
355357
);
356358
let config = this.defaultConfig();
@@ -370,12 +372,12 @@ export class FileManagerService {
370372
public setConfigProfile(profile: string) {
371373
this.configProfile = profile;
372374
}
375+
373376
public getConfigProfile() {
374377
return this.configProfile;
375378
}
376379
public profileConfigExists(profile: string): boolean {
377-
const homeDir = os.homedir();
378-
const configPath = join(homeDir, this.getConfigFileName(profile));
380+
const configPath = join(this.configPath, this.getConfigFileName(profile));
379381
try {
380382
const content = readFileSync(configPath, 'utf8');
381383
const config = yaml.load(content) as Config & object;
@@ -391,6 +393,14 @@ export class FileManagerService {
391393
}
392394
return `.${configProfile}rc`;
393395
}
396+
397+
public setConfigPath(configPath: string) {
398+
this.configPath = configPath;
399+
}
400+
401+
public getConfigPath(): string {
402+
return this.configPath;
403+
}
394404
}
395405

396406
export const fileManagerService = new FileManagerService();

cortex-js/src/usecases/cortex/cortex.usecases.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ export class CortexUsecases implements BeforeApplicationShutdown {
177177
CORTEX_JS_HOST: host,
178178
CORTEX_JS_PORT: port.toString(),
179179
CORTEX_PROFILE: fileManagerService.getConfigProfile(),
180+
CORTEX_CONFIG_PATH: fileManagerService.getConfigPath(),
180181
...process.env,
181182
},
182183
});

0 commit comments

Comments
 (0)