diff --git a/packages/config-loader/src/lib/index.ts b/packages/config-loader/src/lib/index.ts index 9c4a2d9f91..c0b6fbc93b 100644 --- a/packages/config-loader/src/lib/index.ts +++ b/packages/config-loader/src/lib/index.ts @@ -14,6 +14,6 @@ * limitations under the License. */ -export { findRootPath } from './paths'; +export { resolveStaticConfig } from './resolver'; export { readConfigFile } from './reader'; export { readEnv } from './env'; diff --git a/packages/config-loader/src/lib/resolver.ts b/packages/config-loader/src/lib/resolver.ts new file mode 100644 index 0000000000..d7a1a7f764 --- /dev/null +++ b/packages/config-loader/src/lib/resolver.ts @@ -0,0 +1,43 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import fs from 'fs-extra'; +import { resolve as resolvePath } from 'path'; +import { findRootPath } from './paths'; + +type ResolveOptions = { + // Same as configPath in LoadConfigOptions + configPath?: string; +}; + +/** + * Resolves all configuration files that should be loaded. + */ +export async function resolveStaticConfig( + options: ResolveOptions, +): Promise { + // TODO: We'll want this to be a bit more elaborate, probably adding configs for + // specific env, and maybe local config for plugins. + let { configPath } = options; + if (!configPath) { + configPath = resolvePath( + findRootPath(fs.realpathSync(process.cwd())), + 'app-config.yaml', + ); + } + + return [configPath]; +} diff --git a/packages/config-loader/src/loader.ts b/packages/config-loader/src/loader.ts index a8adbde32c..8f4ffdcecf 100644 --- a/packages/config-loader/src/loader.ts +++ b/packages/config-loader/src/loader.ts @@ -17,7 +17,7 @@ import fs from 'fs-extra'; import { resolve as resolvePath, dirname } from 'path'; import { AppConfig } from '@backstage/config'; -import { findRootPath, readConfigFile, readEnv } from './lib'; +import { resolveStaticConfig, readConfigFile, readEnv } from './lib'; export type LoadConfigOptions = { // Config path, defaults to app-config.yaml in project root @@ -27,45 +27,34 @@ export type LoadConfigOptions = { shouldReadSecrets?: boolean; }; -export async function loadStaticConfig( - options: LoadConfigOptions, -): Promise { - const { shouldReadSecrets = false } = options; - - // TODO: We'll want this to be a bit more elaborate, probably adding configs for - // specific env, and maybe local config for plugins. - let { configPath } = options; - if (!configPath) { - configPath = resolvePath( - findRootPath(fs.realpathSync(process.cwd())), - 'app-config.yaml', - ); - } - - try { - const rootPath = dirname(configPath); - const config = await readConfigFile(configPath, { - env: process.env, - shouldReadSecrets, - readFile: (path: string) => { - return fs.readFile(resolvePath(rootPath, path), 'utf8'); - }, - }); - return [config]; - } catch (error) { - throw new Error( - `Failed to read static configuration file: ${error.message}`, - ); - } -} - export async function loadConfig( options: LoadConfigOptions = {}, ): Promise { const configs = []; configs.push(...readEnv(process.env)); - configs.push(...(await loadStaticConfig(options))); + + const configPaths = await resolveStaticConfig(options); + + try { + for (const configPath of configPaths) { + const rootPath = dirname(configPath); + + const config = await readConfigFile(configPath, { + env: process.env, + shouldReadSecrets: Boolean(options.shouldReadSecrets), + readFile: (path: string) => { + return fs.readFile(resolvePath(rootPath, path), 'utf8'); + }, + }); + + configs.push(config); + } + } catch (error) { + throw new Error( + `Failed to read static configuration file: ${error.message}`, + ); + } return configs; }