add feature discovery service

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 11:55:25 +02:00
committed by Philipp Hugenroth
parent 73c4bc0033
commit 0c450c0e9e
3 changed files with 62 additions and 3 deletions
@@ -27,6 +27,7 @@ import { EnumerableServiceHolder, ServiceOrExtensionPoint } from './types';
// eslint-disable-next-line @backstage/no-forbidden-package-imports
import { InternalBackendFeature } from '@backstage/backend-plugin-api/src/wiring/types';
import { ForwardedError } from '@backstage/errors';
import { featureDiscoveryServiceRef } from '@backstage/backend-plugin-api/alpha';
export interface BackendRegisterInit {
consumes: Set<ServiceOrExtensionPoint>;
@@ -87,6 +88,10 @@ export class BackendInitializer {
if (this.#startPromise) {
throw new Error('feature can not be added after the backend has started');
}
this.#addFeature(feature);
}
#addFeature(feature: BackendFeature) {
if (feature.$$type !== '@backstage/BackendFeature') {
throw new Error(
`Failed to add feature, invalid type '${feature.$$type}'`,
@@ -129,6 +134,18 @@ export class BackendInitializer {
}
async #doStart(): Promise<void> {
const featureDiscovery = await this.#serviceHolder.get(
featureDiscoveryServiceRef,
'root',
);
if (featureDiscovery) {
const { features } = await featureDiscovery.getBackendFeatures();
for (const plugin of features) {
this.#addFeature(plugin);
}
}
// Initialize all root scoped services
for (const ref of this.#serviceHolder.getServiceRefs()) {
if (ref.scope === 'root') {
+16 -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",
+29
View File
@@ -0,0 +1,29 @@
/*
* 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 { createServiceRef } from './services';
import { BackendFeature } from './wiring';
/** @alpha */
export interface FeatureDiscoveryService {
getBackendFeatures(): Promise<{ features: Array<BackendFeature> }>;
}
/** @alpha */
export const featureDiscoveryServiceRef =
createServiceRef<FeatureDiscoveryService>({
id: 'core.featureDiscovery',
});