Skip to content

Commit

Permalink
config: return undefined if file does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
aussedatlo committed Nov 2, 2023
1 parent a1c056b commit 5d2fc7c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/config/__tests__/config.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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<IConfig>(TYPES.ConfigService)
.toConstantValue(new FileConfigService(loggerMock, options));
const { config } = container.get<IConfig>(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: {} };
Expand Down
12 changes: 7 additions & 5 deletions src/config/config.service.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -9,27 +9,29 @@ 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(
@inject(TYPES.LoggerService) logger: ILogger,
@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) {
Expand Down
2 changes: 2 additions & 0 deletions src/services/exchange/nexo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
2 changes: 2 additions & 0 deletions src/services/ghostfolio/ghostfolio.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5d2fc7c

Please sign in to comment.