diff --git a/src/config.ts b/src/config.ts index 96eebcdc..eb1c0077 100644 --- a/src/config.ts +++ b/src/config.ts @@ -11,8 +11,7 @@ import ConnectionsFileSchema from './domain/ConnectionsFileSchema'; import { prepareInjectionConfigVars } from './service/configInjection'; import { DEFAULT_CONFIG, CONFIG_FILENAME, CONNECTIONS_FILENAME, REQUIRED_ENV_KEYS } from './constants'; -const CONFIG_FILE_CONVENTION = /^sync-db-[\w]+\.yml$/; - +const CONFIG_FILE_CONVENTION = /^sync-db-\w+\.yml$/; interface ConnectionResolver { resolve: (config: Configuration) => Promise; } @@ -45,15 +44,15 @@ export function getSqlBasePath(config: Configuration): string { */ export async function loadConfig(configFilename: string = CONFIG_FILENAME): Promise { log('Resolving config file.'); - const isConfigFilePathAbsolute = path.isAbsolute(configFilename); - const filename = isConfigFilePathAbsolute ? path.parse(configFilename).base : configFilename; - const filenameMatched = filename.match(CONFIG_FILE_CONVENTION) || filename === CONFIG_FILENAME; + const isAbsolutePath = path.isAbsolute(configFilename); + const filename = isAbsolutePath ? path.parse(configFilename).base : configFilename; + const match = filename.match(CONFIG_FILE_CONVENTION) || filename === CONFIG_FILENAME; - if (!filenameMatched) { - throw new Error(`The config filename doesn't meet the pattern (sync-db.yml or sync-db-*.yml)`); + if (!match) { + throw new Error(`The config filename doesn't match the pattern sync-db.yml or sync-db-*.yml`); } - const filepath = isConfigFilePathAbsolute ? configFilename : path.join(process.cwd(), configFilename); + const filepath = isAbsolutePath ? configFilename : path.join(process.cwd(), configFilename); const loadedConfig = (await yaml.load(filepath)) as Configuration; log('Resolved config file.'); diff --git a/test/unit/util/config.test.ts b/test/unit/util/config.test.ts index 45b765a5..13fb9025 100644 --- a/test/unit/util/config.test.ts +++ b/test/unit/util/config.test.ts @@ -162,7 +162,7 @@ describe('CONFIG:', () => { await expect(loadConfig('sync-db-test.yml')).not.to.be.rejectedWith( Error, - `The config filename doesn't meet the pattern (sync-db.yml or sync-db-*.yml)` + `The config filename doesn't match the pattern sync-db.yml or sync-db-*.yml` ); const config = await loadConfig('sync-db-test.yml'); @@ -172,27 +172,27 @@ describe('CONFIG:', () => { it(`should throw an error if the config file doesn't match the naming convention.`, async () => { await expect(loadConfig('sync-db.txt')).to.be.rejectedWith( Error, - `The config filename doesn't meet the pattern (sync-db.yml or sync-db-*.yml)` + `The config filename doesn't match the pattern sync-db.yml or sync-db-*.yml` ); await expect(loadConfig('sync-db-test.js')).to.be.rejectedWith( Error, - `The config filename doesn't meet the pattern (sync-db.yml or sync-db-*.yml)` + `The config filename doesn't match the pattern sync-db.yml or sync-db-*.yml` ); await expect(loadConfig('sync.yml')).to.be.rejectedWith( Error, - `The config filename doesn't meet the pattern (sync-db.yml or sync-db-*.yml)` + `The config filename doesn't match the pattern sync-db.yml or sync-db-*.yml` ); await expect(loadConfig('sync-db.yml.txt')).to.be.rejectedWith( Error, - `The config filename doesn't meet the pattern (sync-db.yml or sync-db-*.yml)` + `The config filename doesn't match the pattern sync-db.yml or sync-db-*.yml` ); await expect(loadConfig('sync-db-.yml')).to.be.rejectedWith( Error, - `The config filename doesn't meet the pattern (sync-db.yml or sync-db-*.yml)` + `The config filename doesn't match the pattern sync-db.yml or sync-db-*.yml` ); await expect(loadConfig('sync-dbasdfghjkl.yml')).to.be.rejectedWith( Error, - `The config filename doesn't meet the pattern (sync-db.yml or sync-db-*.yml)` + `The config filename doesn't match the pattern sync-db.yml or sync-db-*.yml` ); });