diff --git a/src/config/__tests__/config.service.test.ts b/src/config/__tests__/config.service.test.ts index d61364a..f67638a 100644 --- a/src/config/__tests__/config.service.test.ts +++ b/src/config/__tests__/config.service.test.ts @@ -41,6 +41,7 @@ describe('Config: File config service', () => { // Arrange const options: IAppOptions = { options: {} }; const fakeConfig = {} as Config; + jest.spyOn(fs, 'existsSync').mockReturnValue(true); jest.spyOn(fs, 'readFileSync').mockImplementation(fsMock.readFileSync); fsMock.readFileSync.mockReturnValue(JSON.stringify(fakeConfig)); @@ -79,6 +80,22 @@ describe('Config: File config service', () => { ); }); + it('should return undefined if the fine doesnt exist', async () => { + // Arrange + const options: IAppOptions = { options: {} }; + jest.spyOn(fs, 'existsSync').mockReturnValue(false); + + // Act + container + .bind(TYPES.ConfigService) + .toConstantValue(new FileConfigService(loggerMock, options)); + const { config } = container.get(TYPES.ConfigService); + + // Assert + expect(config).toBe(undefined); + expect(fsMock.readFileSync).not.toBeCalled(); + }); + it('should save into default config file', async () => { // Arrange const options: IAppOptions = { options: {} }; diff --git a/src/config/config.service.ts b/src/config/config.service.ts index 8473b86..4c3ec0a 100644 --- a/src/config/config.service.ts +++ b/src/config/config.service.ts @@ -1,4 +1,4 @@ -import { mkdirSync, readFileSync, writeFileSync } from 'fs'; +import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'; import { inject, injectable } from 'inversify'; import { dirname } from 'path'; @@ -9,13 +9,13 @@ import { Config } from '@app/types/config'; import { DEFAULT_CONFIG_FILE } from '@app/utils/constant'; export interface IConfig { - config: Config; + config: Config | undefined; saveConfig: (data: Config, file?: string) => void; } @injectable() class FileConfigService implements IConfig { - public config: Config; + public config: Config | undefined; private logger: ILogger; constructor( @@ -23,13 +23,15 @@ class FileConfigService implements IConfig { @inject(TYPES.AppOptions) options: IAppOptions ) { this.logger = logger; - this.readConfig(options.options.configFile); + const configFile = options.options.configFile ?? DEFAULT_CONFIG_FILE; + if (!existsSync(configFile)) return; + this.config = this.readConfig(configFile); } private readConfig(file: string = DEFAULT_CONFIG_FILE) { this.logger.debug(`using config file ${file}`); const config = readFileSync(file, 'utf-8'); - this.config = { ...JSON.parse(config), path: file }; + return { ...JSON.parse(config), path: file }; } public saveConfig(data: Config, file: string = DEFAULT_CONFIG_FILE) { diff --git a/src/services/exchange/nexo.service.ts b/src/services/exchange/nexo.service.ts index f12008f..ed2ff98 100644 --- a/src/services/exchange/nexo.service.ts +++ b/src/services/exchange/nexo.service.ts @@ -19,6 +19,8 @@ class NexoService implements IExchange { public client: NexoProClient; constructor(@inject(TYPES.ConfigService) configService: IConfig) { + if (!configService.config) return; + const { nexo } = configService.config; this.client = Client({ api_key: nexo.key, api_secret: nexo.secret }); } diff --git a/src/services/ghostfolio/ghostfolio.service.ts b/src/services/ghostfolio/ghostfolio.service.ts index 812d73f..89a9e5a 100644 --- a/src/services/ghostfolio/ghostfolio.service.ts +++ b/src/services/ghostfolio/ghostfolio.service.ts @@ -13,6 +13,8 @@ class GhostfolioService implements IGhostfolio { private config: GhostfolioConfig; constructor(@inject(TYPES.ConfigService) configService: IConfig) { + if (!configService.config) return; + this.config = configService.config.ghostfolio; this.client = ghostfolioApi( this.config.secret,