Skip to content

Commit

Permalink
fix: Avoid crash if run without config directory (#137)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukas Jost <[email protected]>
  • Loading branch information
meyfa and lusu007 authored Oct 12, 2024
1 parent 6042482 commit 1986b55
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea
.fleet
.vscode
node_modules/
.DS_Store
npm-debug.log*
Expand Down
13 changes: 12 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,18 @@ export const defaultConfig: Config = Object.freeze({
})

export async function readConfigDirectory (directory: string): Promise<Config> {
const entries = await fs.promises.readdir(directory, { withFileTypes: true })
let entries: fs.Dirent[]
try {
entries = await fs.promises.readdir(directory, { withFileTypes: true })
} catch (err: unknown) {
if (typeof err === 'object' && err != null && 'code' in err && err.code === 'ENOENT') {
// ignore missing config directory
entries = []
} else {
throw err
}
}

const configFiles = entries.filter((entry) => entry.isFile() && entry.name.endsWith('.yaml'))

// sort by name, so that 01-foo.yaml is read before 02-bar.yaml
Expand Down
9 changes: 8 additions & 1 deletion test/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { createConfig } from '../src/config.js'
import { createConfig, defaultConfig, readConfigDirectory } from '../src/config.js'
import assert from 'node:assert'

describe('config.ts', () => {
describe('readConfigDirectory()', () => {
it('returns the default config if the directory is missing', async () => {
const config = await readConfigDirectory('missing-dir')
assert.deepStrictEqual(config, defaultConfig)
})
})

describe('createConfig()', () => {
it('creates a valid config object', () => {
assert.doesNotThrow(() => createConfig())
Expand Down

0 comments on commit 1986b55

Please sign in to comment.