config-loader,backend-common: refactor config loading to pass in path and use common util for backend

This commit is contained in:
Patrik Oldsberg
2020-08-03 18:21:03 +02:00
parent aa265769d2
commit 62222b543c
20 changed files with 50 additions and 100 deletions
@@ -1,24 +0,0 @@
/*
* 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 { findRootPath } from './paths';
describe('findRootPath', () => {
it('should find root path', () => {
const rootPath = findRootPath(process.cwd());
expect(typeof rootPath).toBe('string');
});
});
-57
View File
@@ -1,57 +0,0 @@
/*
* 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 { dirname, resolve as resolvePath } from 'path';
/**
* Looks for a package.json that has name: "root" to identify the root of the monorepo
*
* This is a copy of the same function in the CLI
*/
export function findRootPath(topPath: string): string {
let path = topPath;
// Some sanity check to avoid infinite loop
for (let i = 0; i < 1000; i++) {
const packagePath = resolvePath(path, 'package.json');
const exists = fs.pathExistsSync(packagePath);
if (exists) {
try {
const data = fs.readJsonSync(packagePath);
if (data.name === 'root' || data.name.includes('backstage-e2e')) {
return path;
}
} catch (error) {
throw new Error(
`Failed to parse package.json file while searching for root, ${error}`,
);
}
}
const newPath = dirname(path);
if (newPath === path) {
throw new Error(
`No package.json with name "root" found as a parent of ${topPath}`,
);
}
path = newPath;
}
throw new Error(
`Iteration limit reached when searching for root package.json at ${topPath}`,
);
}
+3 -11
View File
@@ -14,13 +14,11 @@
* 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;
// Root path for search for app-config.yaml
rootPath: string;
};
/**
@@ -31,13 +29,7 @@ export async function resolveStaticConfig(
): Promise<string[]> {
// 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',
);
}
const configPath = resolvePath(options.rootPath, 'app-config.yaml');
return [configPath];
}
+3 -3
View File
@@ -25,8 +25,8 @@ import {
} from './lib';
export type LoadConfigOptions = {
// Config path, defaults to app-config.yaml in project root
configPath?: string;
// Root path for search for app-config.yaml
rootPath: string;
// Whether to read secrets or omit them, defaults to false.
shouldReadSecrets?: boolean;
@@ -59,7 +59,7 @@ class Context {
}
export async function loadConfig(
options: LoadConfigOptions = {},
options: LoadConfigOptions,
): Promise<AppConfig[]> {
const configs = [];