import * as fs from 'fs'; import * as nodePath from 'path'; import { ConfigSettingsArg, EnvConfigSettings } from '../types'; /** * Returns full file path for file with base name and passed extension. Returns null if file does not exist * @param rootDir Root directory to look for file * @param baseName base name (file name without extension) to match * @param extension file extension to check for */ export const getFileWithBaseNameAndExtension = ( rootDir: string, baseName: string, extension: string | string[] ): string | null => { const exts = Array.isArray(extension) ? extension : [extension]; for (const ext of exts) { const fullPath = nodePath.join(rootDir, `${baseName}.${ext}`); if (fs.existsSync(fullPath)) { return fullPath; } } return null; }; export const isEnvConfigSettings = ( val: Array> | EnvConfigSettings | TSettings ) => { if (val) { const testVal = Array.isArray(val) ? (val.length > 0 ? val[0] : undefined) : val; if (testVal) { return ['base', 'nodeEnv'].some(p => testVal.hasOwnProperty(p)); } } return false; }; export const isBuildConfigSettingsBuilderPathOption = (val: Array> | ConfigSettingsArg) => { if (val) { const testVal = Array.isArray(val) ? (val.length > 0 ? val[0] : undefined) : val; if (testVal) { return ['rootPath'].every(p => testVal.hasOwnProperty(p)); } } return false; }; export const toEnvConfigSettings = ( settings: EnvConfigSettings | TSettings ): EnvConfigSettings => { return (isEnvConfigSettings(settings) ? settings : { base: settings }) as EnvConfigSettings; };