cli: allow running with no configuration files

Co-authored-by: Patrik Oldsberg <poldsberg@gmail.com>
Signed-off-by: MT Lewis <mtlewis@users.noreply.github.com>
This commit is contained in:
MT Lewis
2024-07-18 11:10:18 +01:00
parent f93ae496ce
commit 493feaca4d
6 changed files with 47 additions and 12 deletions
+7
View File
@@ -0,0 +1,7 @@
---
'@backstage/config-loader': patch
---
Add boolean `allowMissingDefaultConfig` option to `ConfigSources.default` and
`ConfigSources.defaultForTargets`, which results in omission of a ConfigSource
for the default app-config.yaml configuration file if it's not present.
+5 -1
View File
@@ -48,8 +48,12 @@ const BUILD_CACHE_ENV_VAR = 'BACKSTAGE_CLI_EXPERIMENTAL_BUILD_CACHE';
export function resolveBaseUrl(config: Config): URL {
const baseUrl = config.getOptionalString('app.baseUrl');
try {
return new URL(baseUrl ?? '/', 'http://localhost:3000');
return new URL(
baseUrl ?? '/',
`http://localhost:${process.env.PORT ?? '3000'}`,
);
} catch (error) {
throw new Error(`Invalid app.baseUrl, ${error}`);
}
+5 -3
View File
@@ -96,11 +96,13 @@ export async function loadCliConfig(options: Options) {
},
});
const configurationLoadedMessage = appConfigs.length
? `Loaded config from ${appConfigs.map(c => c.context).join(', ')}`
: `No configuration files found, running without config`;
// printing to stderr to not clobber stdout in case the cli command
// outputs structured data (e.g. as config:schema does)
process.stderr.write(
`Loaded config from ${appConfigs.map(c => c.context).join(', ')}\n`,
);
process.stderr.write(`${configurationLoadedMessage}\n`);
try {
const frontendAppConfigs = schema.process(appConfigs, {
+1
View File
@@ -21,6 +21,7 @@ export type AsyncConfigSourceGenerator = AsyncGenerator<
// @public
export interface BaseConfigSourcesOptions {
allowMissingDefaultConfig?: boolean;
// (undocumented)
remote?: Pick<RemoteConfigSourceOptions, 'reloadInterval'>;
// (undocumented)
@@ -69,13 +69,18 @@ describe('ConfigSources', () => {
});
it('should create default sources for targets', () => {
const fsSpy = jest.spyOn(fs, 'pathExistsSync').mockImplementation(path => {
return path === `${root}app-config.yaml`;
});
expect(
mergeSources(
ConfigSources.defaultForTargets({ rootDir: '/', targets: [] }),
),
).toEqual([{ name: 'FileConfigSource', path: `${root}app-config.yaml` }]);
const fsSpy = jest.spyOn(fs, 'pathExistsSync').mockReturnValue(true);
fsSpy.mockReturnValue(true);
expect(
mergeSources(
ConfigSources.defaultForTargets({ rootDir: '/', targets: [] }),
@@ -159,6 +164,10 @@ describe('ConfigSources', () => {
});
it('should create a default source', () => {
const fsSpy = jest.spyOn(fs, 'pathExistsSync').mockImplementation(path => {
return path === `${root}app-config.yaml`;
});
expect(
mergeSources(
ConfigSources.default({
@@ -184,6 +193,8 @@ describe('ConfigSources', () => {
{ name: 'FileConfigSource', path: resolvePath('b.yaml') },
{ name: 'EnvConfigSource', env: { HOME: '/' } },
]);
fsSpy.mockRestore();
});
it('should merge sources', () => {
@@ -74,6 +74,11 @@ export interface BaseConfigSourcesOptions {
watch?: boolean;
rootDir?: string;
remote?: Pick<RemoteConfigSourceOptions, 'reloadInterval'>;
/**
* Allow the default app-config.yaml to be missing, in which case the source
* will not be created.
*/
allowMissingDefaultConfig?: boolean;
/**
* A custom substitution function that overrides the default one.
@@ -177,14 +182,19 @@ export class ConfigSources {
if (argSources.length === 0) {
const defaultPath = resolvePath(rootDir, 'app-config.yaml');
const localPath = resolvePath(rootDir, 'app-config.local.yaml');
const alwaysIncludeDefaultConfigSource =
!options.allowMissingDefaultConfig;
if (alwaysIncludeDefaultConfigSource || fs.pathExistsSync(defaultPath)) {
argSources.push(
FileConfigSource.create({
watch: options.watch,
path: defaultPath,
substitutionFunc: options.substitutionFunc,
}),
);
}
argSources.push(
FileConfigSource.create({
watch: options.watch,
path: defaultPath,
substitutionFunc: options.substitutionFunc,
}),
);
if (fs.pathExistsSync(localPath)) {
argSources.push(
FileConfigSource.create({