discover backend feature factory

Co-authored-by: Patrik Oldsberg <poldsberg@gmail.com>
Signed-off-by: Vincenzo Scamporlino <vincenzos@spotify.com>
This commit is contained in:
Vincenzo Scamporlino
2023-08-01 16:52:56 +02:00
committed by Philipp Hugenroth
parent 0c450c0e9e
commit 0b4dbb4082
5 changed files with 147 additions and 4 deletions
+17 -3
View File
@@ -5,13 +5,26 @@
"main": "src/index.ts",
"types": "src/index.ts",
"publishConfig": {
"access": "public",
"main": "dist/index.cjs.js",
"types": "dist/index.d.ts"
"access": "public"
},
"backstage": {
"role": "node-library"
},
"exports": {
".": "./src/index.ts",
"./alpha": "./src/alpha.ts",
"./package.json": "./package.json"
},
"typesVersions": {
"*": {
"alpha": [
"src/alpha.ts"
],
"package.json": [
"package.json"
]
}
},
"homepage": "https://backstage.io",
"repository": {
"type": "git",
@@ -36,6 +49,7 @@
"@backstage/backend-plugin-api": "workspace:^",
"@backstage/backend-tasks": "workspace:^",
"@backstage/cli-common": "workspace:^",
"@backstage/cli-node": "workspace:^",
"@backstage/config": "workspace:^",
"@backstage/config-loader": "workspace:^",
"@backstage/errors": "workspace:^",
+118
View File
@@ -0,0 +1,118 @@
/*
* Copyright 2023 The Backstage Authors
*
* 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 {
BackendFeature,
RootConfigService,
coreServices,
createServiceFactory,
} from '@backstage/backend-plugin-api';
import {
featureDiscoveryServiceRef,
FeatureDiscoveryService,
} from '@backstage/backend-plugin-api/alpha';
import { resolve as resolvePath, dirname } from 'path';
import fs from 'fs-extra';
import { BackstagePackageJson } from '@backstage/cli-node';
const LOADED_PACKAGE_ROLES = ['backend-plugin', 'backend-module'];
/** @internal */
async function findClosestPackageDir(
searchDir: string,
): Promise<string | undefined> {
let path = searchDir;
// Some confidence check to avoid infinite loop
for (let i = 0; i < 1000; i++) {
const packagePath = resolvePath(path, 'package.json');
const exists = await fs.pathExists(packagePath);
if (exists) {
return path;
}
const newPath = dirname(path);
if (newPath === path) {
return undefined;
}
path = newPath;
}
throw new Error(
`Iteration limit reached when searching for root package.json at ${searchDir}`,
);
}
/** @alpha */
export class PackageDiscoveryService implements FeatureDiscoveryService {
constructor(private readonly config: RootConfigService) {}
async getBackendFeatures(): Promise<{ features: Array<BackendFeature> }> {
// if (this.config.getOptionalString('backend.packages') !== 'all') {
// return { features: [] };
// }
console.log(`DEBUG: executing backend feature discovery`);
const packageDir = await findClosestPackageDir(process.argv[1]);
if (!packageDir) {
throw new Error('NOOOOOOOOOOOOOOOOOOOOOOOOOO');
}
console.log(`DEBUG: packageDir=`, packageDir);
const { dependencies } = require(resolvePath(packageDir, 'package.json'));
const dependencyNames = Object.keys(dependencies);
console.log(`DEBUG: dependencyNames=`, dependencyNames);
const features: BackendFeature[] = [];
for (const name of dependencyNames) {
const depPkg = require(`${name}/package.json`) as BackstagePackageJson;
if (!LOADED_PACKAGE_ROLES.includes(depPkg?.backstage?.role ?? '')) {
continue;
}
const depModule = require(name); // @backstage/plugin-catalog-backend
console.log(`DEBUG: loaded ${name} depModule=`, depModule);
Object.values(depModule).filter(exportValue => {
if (
exportValue &&
typeof exportValue === 'object' &&
(exportValue as any).$$type === '@backstage/BackendFeature'
) {
features.push(exportValue as BackendFeature);
}
if (
typeof exportValue === 'function' &&
(exportValue as any).$$type === '@backstage/BackendFeatureFactory'
) {
features.push(exportValue() as BackendFeature);
}
});
}
console.log(`DEBUG: features=`, features);
return { features };
}
}
/** @alpha */
export const packageFeatureDiscoveryServiceFactory = createServiceFactory({
service: featureDiscoveryServiceRef,
deps: {
config: coreServices.rootConfig,
},
factory({ config }) {
return new PackageDiscoveryService(config);
},
});