Fix review comments: more information in schemaLocator

Signed-off-by: David Festal <dfestal@redhat.com>
This commit is contained in:
David Festal
2024-01-22 15:37:36 +01:00
parent d7adbbf455
commit 62ba5f4044
3 changed files with 16 additions and 18 deletions
@@ -118,7 +118,7 @@ export const dynamicPluginsFeatureDiscoveryServiceFactory: () => ServiceFactory<
// @public (undocumented)
export interface DynamicPluginsSchemaDiscoveryOptions {
schemaLocator?: (platform: PackagePlatform) => string;
schemaLocator?: (pluginPackage: ScannedPluginPackage) => string;
}
// @public (undocumented)
@@ -329,13 +329,15 @@ export class PluginScanner {
*/
export interface DynamicPluginsSchemaDiscoveryOptions {
/**
* Function that returns the plugin-relative path to the Json schema file for a given platform.
* Default behavior is to look for the `configSchema.json` file in the package `dist` sub-directory.
* Function that returns the path to the Json schema file for a given scanned plugin package.
* The path is either absolute, or relative to the plugin package root directory.
*
* @param platform - The platform of the plugin.
* @returns the plugin-relative path to the Json schema file.
* Default behavior is to look for the `dist/configSchema.json` relative path.
*
* @param pluginPackage - The scanned plugin package.
* @returns the absolute or plugin-relative path to the Json schema file.
*/
schemaLocator?: (platform: PackagePlatform) => string;
schemaLocator?: (pluginPackage: ScannedPluginPackage) => string;
}
/**
@@ -21,29 +21,25 @@ import * as path from 'path';
import * as url from 'url';
import { isEmpty } from 'lodash';
import { LoggerService } from '@backstage/backend-plugin-api';
import { PackagePlatform, PackageRoles } from '@backstage/cli-node';
export async function gatherDynamicPluginsSchemas(
packages: ScannedPluginPackage[],
logger: LoggerService,
schemaLocator: (platform: PackagePlatform) => string = () =>
schemaLocator: (pluginPackage: ScannedPluginPackage) => string = () =>
path.join('dist', 'configSchema.json'),
): Promise<ConfigSchemaPackageEntry[]> {
const allSchemas: { value: any; path: string }[] = [];
for (const pluginPackage of packages) {
const platform = PackageRoles.getRoleInfo(
pluginPackage.manifest.backstage.role,
).platform;
let schemaLocation = schemaLocator(pluginPackage);
let pluginLocation = url.fileURLToPath(pluginPackage.location);
if (path.basename(pluginLocation) === 'alpha') {
pluginLocation = path.dirname(pluginLocation);
if (!path.isAbsolute(schemaLocation)) {
let pluginLocation = url.fileURLToPath(pluginPackage.location);
if (path.basename(pluginLocation) === 'alpha') {
pluginLocation = path.dirname(pluginLocation);
}
schemaLocation = path.resolve(pluginLocation, schemaLocation);
}
const schemaLocation: string = path.resolve(
pluginLocation,
schemaLocator(platform),
);
if (!(await fs.pathExists(schemaLocation))) {
continue;