From aa2b055e82e202cba38658d54930784652fac3a9 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 30 Mar 2023 17:53:52 +0200 Subject: [PATCH] config-loader: add .defaultForTargets helper + load default paths Signed-off-by: Patrik Oldsberg --- .../src/sources/ConfigSources.ts | 45 ++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/packages/config-loader/src/sources/ConfigSources.ts b/packages/config-loader/src/sources/ConfigSources.ts index 6e90994c6e..c98f7c894a 100644 --- a/packages/config-loader/src/sources/ConfigSources.ts +++ b/packages/config-loader/src/sources/ConfigSources.ts @@ -14,6 +14,8 @@ * limitations under the License. */ +import { resolve as resolvePath } from 'path'; +import fs from 'fs-extra'; import { Config, ConfigReader } from '@backstage/config'; import parseArgs from 'minimist'; import { EnvConfigSource } from './EnvConfigSource'; @@ -23,6 +25,7 @@ import { RemoteConfigSource } from './RemoteConfigSource'; import { ConfigSource } from './types'; import { ObservableConfigProxy } from './ObservableConfigProxy'; import { LoadConfigOptionsRemote } from '../loader'; +import { EnvFunc } from '../lib/transform/types'; export class ConfigSources { static parseArgs( @@ -40,13 +43,13 @@ export class ConfigSources { }); } - static default(options: { - logger: Logger; - argv?: string[]; + static defaultForTargets(options: { + rootDir: string; + targets: Array<{ type: 'url' | 'path'; target: string }>; remote?: LoadConfigOptionsRemote; - env?: Record; + envFunc?: EnvFunc; }): ConfigSource { - const argSources = this.parseArgs(options.argv).map(arg => { + const argSources = options.targets.map(arg => { if (arg.type === 'url') { if (!options.remote) { throw new Error( @@ -57,9 +60,39 @@ export class ConfigSources { } return FileConfigSource.create({ ...options, path: arg.target }); }); + + if (argSources.length === 0) { + const defaultPath = resolvePath(options.rootDir, 'app-config.yaml'); + const localPath = resolvePath(options.rootDir, 'app-config.local.yaml'); + + argSources.push( + FileConfigSource.create({ ...options, path: defaultPath }), + ); + if (fs.pathExistsSync(localPath)) { + argSources.push( + FileConfigSource.create({ ...options, path: localPath }), + ); + } + } + + return this.merge(argSources); + } + + static default(options: { + rootDir: string; + argv?: string[]; + remote?: LoadConfigOptionsRemote; + env?: Record; + envFunc?: EnvFunc; + }): ConfigSource { + const argSource = this.defaultForTargets({ + ...options, + targets: this.parseArgs(options.argv), + }); + const envSource = EnvConfigSource.create(options); - return this.merge([...argSources, envSource]); + return this.merge([argSource, envSource]); } static merge(sources: ConfigSource[]): ConfigSource {