From 811328ed1fd73322c122ecffffaf36e93a278155 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 7 Aug 2020 10:37:22 +0200 Subject: [PATCH] config-loader: use multiple config roots --- packages/backend-common/src/config.ts | 2 +- packages/cli/src/commands/app/build.ts | 4 +++- packages/cli/src/commands/app/serve.ts | 4 +++- packages/cli/src/commands/backend/dev.ts | 4 +++- packages/cli/src/commands/plugin/export.ts | 4 +++- packages/cli/src/commands/plugin/serve.ts | 4 +++- packages/config-loader/src/lib/resolver.ts | 16 ++++++++++++---- packages/config-loader/src/loader.ts | 4 ++-- 8 files changed, 30 insertions(+), 12 deletions(-) diff --git a/packages/backend-common/src/config.ts b/packages/backend-common/src/config.ts index 8391ed4104..d63f5171d3 100644 --- a/packages/backend-common/src/config.ts +++ b/packages/backend-common/src/config.ts @@ -23,7 +23,7 @@ import { loadConfig } from '@backstage/config-loader'; export async function loadBackendConfig() { const paths = findPaths(__dirname); const configs = await loadConfig({ - rootPath: paths.targetRoot, + rootPaths: [paths.targetDir, paths.targetRoot], shouldReadSecrets: true, }); return configs; diff --git a/packages/cli/src/commands/app/build.ts b/packages/cli/src/commands/app/build.ts index b3ec5ad2bf..81eec7a32b 100644 --- a/packages/cli/src/commands/app/build.ts +++ b/packages/cli/src/commands/app/build.ts @@ -21,7 +21,9 @@ import { paths } from '../../lib/paths'; import { buildBundle } from '../../lib/bundler'; export default async (cmd: Command) => { - const appConfigs = await loadConfig({ rootPath: paths.targetRoot }); + const appConfigs = await loadConfig({ + rootPaths: [paths.targetDir, paths.targetRoot], + }); await buildBundle({ entry: 'src/index', statsJsonEnabled: cmd.stats, diff --git a/packages/cli/src/commands/app/serve.ts b/packages/cli/src/commands/app/serve.ts index 732370ef3b..f4069ca9fa 100644 --- a/packages/cli/src/commands/app/serve.ts +++ b/packages/cli/src/commands/app/serve.ts @@ -21,7 +21,9 @@ import { paths } from '../../lib/paths'; import { serveBundle } from '../../lib/bundler'; export default async (cmd: Command) => { - const appConfigs = await loadConfig({ rootPath: paths.targetRoot }); + const appConfigs = await loadConfig({ + rootPaths: [paths.targetDir, paths.targetRoot], + }); const waitForExit = await serveBundle({ entry: 'src/index', checksEnabled: cmd.check, diff --git a/packages/cli/src/commands/backend/dev.ts b/packages/cli/src/commands/backend/dev.ts index 03d9464722..8d5e6339dc 100644 --- a/packages/cli/src/commands/backend/dev.ts +++ b/packages/cli/src/commands/backend/dev.ts @@ -21,7 +21,9 @@ import { paths } from '../../lib/paths'; import { serveBackend } from '../../lib/bundler/backend'; export default async (cmd: Command) => { - const appConfigs = await loadConfig({ rootPath: paths.targetRoot }); + const appConfigs = await loadConfig({ + rootPaths: [paths.targetDir, paths.targetRoot], + }); const waitForExit = await serveBackend({ entry: 'src/index', checksEnabled: cmd.check, diff --git a/packages/cli/src/commands/plugin/export.ts b/packages/cli/src/commands/plugin/export.ts index be6df707f3..c3beb8b23e 100644 --- a/packages/cli/src/commands/plugin/export.ts +++ b/packages/cli/src/commands/plugin/export.ts @@ -21,7 +21,9 @@ import { paths } from '../../lib/paths'; import { buildBundle } from '../../lib/bundler'; export default async (cmd: Command) => { - const appConfigs = await loadConfig({ rootPath: paths.targetRoot }); + const appConfigs = await loadConfig({ + rootPaths: [paths.targetDir, paths.targetRoot], + }); await buildBundle({ entry: 'dev/index', statsJsonEnabled: cmd.stats, diff --git a/packages/cli/src/commands/plugin/serve.ts b/packages/cli/src/commands/plugin/serve.ts index 5ee4b70a1e..989cf92523 100644 --- a/packages/cli/src/commands/plugin/serve.ts +++ b/packages/cli/src/commands/plugin/serve.ts @@ -21,7 +21,9 @@ import { paths } from '../../lib/paths'; import { serveBundle } from '../../lib/bundler'; export default async (cmd: Command) => { - const appConfigs = await loadConfig({ rootPath: paths.targetRoot }); + const appConfigs = await loadConfig({ + rootPaths: [paths.targetDir, paths.targetRoot], + }); const waitForExit = await serveBundle({ entry: 'dev/index', checksEnabled: cmd.check, diff --git a/packages/config-loader/src/lib/resolver.ts b/packages/config-loader/src/lib/resolver.ts index a3921f07aa..5ee3d2cf09 100644 --- a/packages/config-loader/src/lib/resolver.ts +++ b/packages/config-loader/src/lib/resolver.ts @@ -15,10 +15,11 @@ */ import { resolve as resolvePath } from 'path'; +import { pathExists } from 'fs-extra'; type ResolveOptions = { - // Root path for search for app-config.yaml - rootPath: string; + // Root paths to search for config files. Config from earlier paths has higher priority. + rootPaths: string[]; }; /** @@ -29,7 +30,14 @@ export async function resolveStaticConfig( ): Promise { // TODO: We'll want this to be a bit more elaborate, probably adding configs for // specific env, and maybe local config for plugins. - const configPath = resolvePath(options.rootPath, 'app-config.yaml'); + const resolvedPaths = []; - return [configPath]; + for (const rootPath of options.rootPaths) { + const path = resolvePath(rootPath, 'app-config.yaml'); + if (await pathExists(path)) { + resolvedPaths.push(path); + } + } + + return resolvedPaths; } diff --git a/packages/config-loader/src/loader.ts b/packages/config-loader/src/loader.ts index ce0e09e815..ba3c615ff7 100644 --- a/packages/config-loader/src/loader.ts +++ b/packages/config-loader/src/loader.ts @@ -25,8 +25,8 @@ import { } from './lib'; export type LoadConfigOptions = { - // Root path for search for app-config.yaml - rootPath: string; + // Root paths to search for config files. Config from earlier paths has higher priority. + rootPaths: string[]; // Whether to read secrets or omit them, defaults to false. shouldReadSecrets?: boolean;