Skip to content

Commit

Permalink
Fixed KeyPerFileConfig after upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
fraliv13 committed Nov 5, 2024
1 parent 3d3b16f commit 51c88ed
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
5 changes: 5 additions & 0 deletions packages/key-per-file-configuration/__mocks__/fs/promises.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) TotalSoft.
// This source code is licensed under the MIT license.

import { fs } from 'memfs'
export = fs.promises
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 () => {
Expand Down
32 changes: 19 additions & 13 deletions packages/key-per-file-configuration/src/keyPerFileConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,47 @@ 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)

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)
Expand Down
2 changes: 1 addition & 1 deletion packages/key-per-file-configuration/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export interface Options {
}

export interface ConfigWatcher {
close: () => Promise<void>
close: () => Promise<void> | undefined
}

0 comments on commit 51c88ed

Please sign in to comment.