From 51c88eda22c2169ed2eb96552f29bd128c69b604 Mon Sep 17 00:00:00 2001 From: fraliv13 <5892139+fraliv13@users.noreply.github.com> Date: Tue, 5 Nov 2024 18:52:18 +0200 Subject: [PATCH] Fixed KeyPerFileConfig after upgrade --- .../__mocks__/fs/promises.ts | 5 +++ .../__tests__/key-per-file-config.test.ts | 10 +++--- .../src/keyPerFileConfiguration.ts | 32 +++++++++++-------- .../key-per-file-configuration/src/types.ts | 2 +- 4 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 packages/key-per-file-configuration/__mocks__/fs/promises.ts diff --git a/packages/key-per-file-configuration/__mocks__/fs/promises.ts b/packages/key-per-file-configuration/__mocks__/fs/promises.ts new file mode 100644 index 0000000..e7950fa --- /dev/null +++ b/packages/key-per-file-configuration/__mocks__/fs/promises.ts @@ -0,0 +1,5 @@ +// Copyright (c) TotalSoft. +// This source code is licensed under the MIT license. + +import { fs } from 'memfs' +export = fs.promises diff --git a/packages/key-per-file-configuration/__tests__/key-per-file-config.test.ts b/packages/key-per-file-configuration/__tests__/key-per-file-config.test.ts index dfeade7..fe4002e 100644 --- a/packages/key-per-file-configuration/__tests__/key-per-file-config.test.ts +++ b/packages/key-per-file-configuration/__tests__/key-per-file-config.test.ts @@ -39,7 +39,7 @@ describe('key-per-file configuration tests:', () => { expect(process.env.ENV2).toBe(env2Value) }) - it('reads config from custom glob location', async () => { + it('fails to reads config from custom glob location', async () => { //arrange const env1Value = 'ENV1-value' const env2Value = 'ENV2-value' @@ -50,12 +50,12 @@ describe('key-per-file configuration tests:', () => { }) //act - const watcher = load({ configPath: 'customLocation/**', logger: emptyLogger }) - await watcher.close() + const tryLoad = () => { + const _watcher = load({ configPath: 'customLocation/**', logger: emptyLogger }) + } //assert - expect(process.env.ENV1).toBe(env1Value) - expect(process.env.ENV2).toBe(env2Value) + expect(tryLoad).toThrow('Glob patterns are not supported in the config path') }) it('reads config from custom direcotry location', async () => { diff --git a/packages/key-per-file-configuration/src/keyPerFileConfiguration.ts b/packages/key-per-file-configuration/src/keyPerFileConfiguration.ts index c0e201c..2448cc4 100644 --- a/packages/key-per-file-configuration/src/keyPerFileConfiguration.ts +++ b/packages/key-per-file-configuration/src/keyPerFileConfiguration.ts @@ -11,19 +11,19 @@ const { KEY_PER_FILE_CONFIG_PATH } = process.env const defaultConfigPath = 'runtime' export function load(options?: Options): ConfigWatcher { - const cleanConfigPath = _cleanConfigPath(options?.configPath) + const configFolderPath = _getAbsolutePath(options?.configPath) const logger = options?.logger ?? console - logger.info(`Loading "Key per File" configuration at: ${cleanConfigPath}`) + logger.info(`Loading "Key per File" configuration at: ${configFolderPath}`) // Load files to env - const filePaths = glob.sync(cleanConfigPath, { nodir: true }) + const filePaths = _getFilePaths(configFolderPath) const loadValue = _loadValue(logger) filePaths.forEach(loadValue) // Watch for file changes const watcher = chokidar - .watch(cleanConfigPath, { awaitWriteFinish: true /*, usePolling: true*/ }) + .watch(configFolderPath, { awaitWriteFinish: true /*, usePolling: true*/ }) .on('unlink', _removeValue) .on('add', loadValue) .on('change', loadValue) @@ -31,21 +31,27 @@ export function load(options?: Options): ConfigWatcher { return { close: () => watcher.close() } } -function _cleanConfigPath(configPath = KEY_PER_FILE_CONFIG_PATH || defaultConfigPath): string { - let cleanConfigPath = path.join(process.cwd(), configPath) +function _getAbsolutePath(configPath = KEY_PER_FILE_CONFIG_PATH || defaultConfigPath): string { + const cleanConfigPath = path.join(process.cwd(), configPath) - if (!glob.hasMagic(cleanConfigPath)) { - if (!fs.existsSync(cleanConfigPath) || fs.lstatSync(cleanConfigPath).isDirectory()) { - cleanConfigPath = path.join(cleanConfigPath, '**') - } + if (glob.hasMagic(cleanConfigPath)) { + throw 'Glob patterns are not supported in the config path' } - // glob only works with forward slashes - cleanConfigPath = cleanConfigPath.replace(/\\/g, '/') - return cleanConfigPath } +function _getFilePaths(configPath: string) { + + if (!fs.existsSync(configPath) || fs.lstatSync(configPath).isDirectory()) { + configPath = path.join(configPath, '**') + } + + configPath = configPath.replace(/\\/g, '/') + + return glob.sync(configPath, { nodir: true }) +} + function _loadValue(logger: Logger) { return function (filePath: string) { const key = path.basename(filePath) diff --git a/packages/key-per-file-configuration/src/types.ts b/packages/key-per-file-configuration/src/types.ts index c7ae539..ddcdb04 100644 --- a/packages/key-per-file-configuration/src/types.ts +++ b/packages/key-per-file-configuration/src/types.ts @@ -12,5 +12,5 @@ export interface Options { } export interface ConfigWatcher { - close: () => Promise + close: () => Promise | undefined }