From cbfc69e4d4aea99d440f09a297e1d6959f1800be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Tue, 19 Nov 2024 13:32:34 +0100 Subject: [PATCH] remove the last src/run.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/real-shirts-cry.md | 5 + .changeset/slow-horses-destroy.md | 6 ++ packages/cli-node/src/git/GitUtils.ts | 3 +- .../src/monorepo/PackageGraph.test.ts | 2 +- .../cli-node/src/monorepo/PackageGraph.ts | 2 +- packages/cli-node/src/monorepo/isMonoRepo.ts | 2 +- .../cli-node/src/monorepo/isMonorepo.test.ts | 2 +- packages/cli-node/src/{paths.ts => util.ts} | 4 + .../dev/index.ts | 81 ++++++++++++++++ .../src/run.ts | 96 ------------------- .../kubernetes-backend/dev/index.ts | 9 +- plugins/kubernetes-backend/src/run.ts | 33 ------- .../src/service/standaloneApplication.ts | 68 ------------- .../src/service/standaloneServer.ts | 50 ---------- 14 files changed, 106 insertions(+), 257 deletions(-) create mode 100644 .changeset/real-shirts-cry.md create mode 100644 .changeset/slow-horses-destroy.md rename packages/cli-node/src/{paths.ts => util.ts} (84%) create mode 100644 plugins/catalog-backend-module-incremental-ingestion/dev/index.ts delete mode 100644 plugins/catalog-backend-module-incremental-ingestion/src/run.ts rename packages/cli-node/src/run.ts => plugins/kubernetes-backend/dev/index.ts (75%) delete mode 100644 plugins/kubernetes-backend/src/run.ts delete mode 100644 plugins/kubernetes-backend/src/service/standaloneApplication.ts delete mode 100644 plugins/kubernetes-backend/src/service/standaloneServer.ts diff --git a/.changeset/real-shirts-cry.md b/.changeset/real-shirts-cry.md new file mode 100644 index 0000000000..78056368ae --- /dev/null +++ b/.changeset/real-shirts-cry.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli-node': patch +--- + +Internal refactor diff --git a/.changeset/slow-horses-destroy.md b/.changeset/slow-horses-destroy.md new file mode 100644 index 0000000000..fe23d6cf27 --- /dev/null +++ b/.changeset/slow-horses-destroy.md @@ -0,0 +1,6 @@ +--- +'@backstage/plugin-catalog-backend-module-incremental-ingestion': patch +'@backstage/plugin-kubernetes-backend': patch +--- + +Create a `dev/index.ts` entrypoint for `yarn start` diff --git a/packages/cli-node/src/git/GitUtils.ts b/packages/cli-node/src/git/GitUtils.ts index df04b1c6a2..7b87487c56 100644 --- a/packages/cli-node/src/git/GitUtils.ts +++ b/packages/cli-node/src/git/GitUtils.ts @@ -15,8 +15,7 @@ */ import { assertError, ForwardedError } from '@backstage/errors'; -import { paths } from '../paths'; -import { execFile } from '../run'; +import { execFile, paths } from '../util'; /** * Run a git command, trimming the output splitting it into lines. diff --git a/packages/cli-node/src/monorepo/PackageGraph.test.ts b/packages/cli-node/src/monorepo/PackageGraph.test.ts index 9bc9cbf72b..e2378b91c0 100644 --- a/packages/cli-node/src/monorepo/PackageGraph.test.ts +++ b/packages/cli-node/src/monorepo/PackageGraph.test.ts @@ -23,7 +23,7 @@ import { GitUtils } from '../git'; const mockListChangedFiles = jest.spyOn(GitUtils, 'listChangedFiles'); const mockReadFileAtRef = jest.spyOn(GitUtils, 'readFileAtRef'); -jest.mock('../paths', () => ({ +jest.mock('../util', () => ({ paths: { targetRoot: '/', resolveTargetRoot: (...paths: string[]) => resolvePath('/', ...paths), diff --git a/packages/cli-node/src/monorepo/PackageGraph.ts b/packages/cli-node/src/monorepo/PackageGraph.ts index 69ec99c2dd..721aa79e50 100644 --- a/packages/cli-node/src/monorepo/PackageGraph.ts +++ b/packages/cli-node/src/monorepo/PackageGraph.ts @@ -16,7 +16,7 @@ import path from 'path'; import { getPackages, Package } from '@manypkg/get-packages'; -import { paths } from '../paths'; +import { paths } from '../util'; import { PackageRole } from '../roles'; import { GitUtils } from '../git'; import { Lockfile } from './Lockfile'; diff --git a/packages/cli-node/src/monorepo/isMonoRepo.ts b/packages/cli-node/src/monorepo/isMonoRepo.ts index da78c8dd53..f47ac4b9ad 100644 --- a/packages/cli-node/src/monorepo/isMonoRepo.ts +++ b/packages/cli-node/src/monorepo/isMonoRepo.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { paths } from '../paths'; +import { paths } from '../util'; import fs from 'fs-extra'; /** diff --git a/packages/cli-node/src/monorepo/isMonorepo.test.ts b/packages/cli-node/src/monorepo/isMonorepo.test.ts index de0dae0893..30c2d3ccbc 100644 --- a/packages/cli-node/src/monorepo/isMonorepo.test.ts +++ b/packages/cli-node/src/monorepo/isMonorepo.test.ts @@ -19,7 +19,7 @@ import { createMockDirectory } from '@backstage/backend-test-utils'; const mockDir = createMockDirectory(); -jest.mock('../paths', () => ({ +jest.mock('../util', () => ({ paths: { resolveTargetRoot: (...args: string[]) => mockDir.resolve(...args) }, })); diff --git a/packages/cli-node/src/paths.ts b/packages/cli-node/src/util.ts similarity index 84% rename from packages/cli-node/src/paths.ts rename to packages/cli-node/src/util.ts index 2c658c27b3..727c173b00 100644 --- a/packages/cli-node/src/paths.ts +++ b/packages/cli-node/src/util.ts @@ -14,7 +14,11 @@ * limitations under the License. */ +import { execFile as execFileCb } from 'child_process'; +import { promisify } from 'util'; import { findPaths } from '@backstage/cli-common'; +export const execFile = promisify(execFileCb); + /* eslint-disable-next-line no-restricted-syntax */ export const paths = findPaths(__dirname); diff --git a/plugins/catalog-backend-module-incremental-ingestion/dev/index.ts b/plugins/catalog-backend-module-incremental-ingestion/dev/index.ts new file mode 100644 index 0000000000..cc205c6865 --- /dev/null +++ b/plugins/catalog-backend-module-incremental-ingestion/dev/index.ts @@ -0,0 +1,81 @@ +/* + * Copyright 2024 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 { createBackend } from '@backstage/backend-defaults'; +import { + coreServices, + createBackendModule, +} from '@backstage/backend-plugin-api'; +import { mockServices } from '@backstage/backend-test-utils'; +import { + IncrementalEntityProvider, + incrementalIngestionProvidersExtensionPoint, +} from '../src'; + +const dummyProvider = createBackendModule({ + pluginId: 'catalog', + moduleId: 'incremental-test-provider', + register(reg) { + reg.registerInit({ + deps: { + logger: coreServices.logger, + providers: incrementalIngestionProvidersExtensionPoint, + }, + async init({ logger, providers }) { + const provider: IncrementalEntityProvider = { + getProviderName: () => 'test-provider', + around: burst => burst(0), + next: async (_context, cursor) => { + await new Promise(resolve => setTimeout(resolve, 500)); + if (cursor === undefined || cursor < 3) { + logger.info(`### Returning batch #${cursor}`); + return { done: false, entities: [], cursor: (cursor ?? 0) + 1 }; + } + + logger.info('### Last batch reached, stopping'); + return { done: true }; + }, + }; + + providers.addProvider({ + provider: provider, + options: { + burstInterval: { seconds: 1 }, + burstLength: { seconds: 10 }, + restLength: { seconds: 10 }, + }, + }); + }, + }); + }, +}); + +const backend = createBackend(); +backend.add( + mockServices.rootConfig.factory({ + data: { + backend: { + baseUrl: 'http://localhost:7007', + listen: ':7007', + database: { client: 'better-sqlite3', connection: ':memory:' }, + }, + }, + }), +); +backend.add(import('@backstage/plugin-catalog-backend')); +backend.add(import('../src')); +backend.add(dummyProvider); +backend.start(); diff --git a/plugins/catalog-backend-module-incremental-ingestion/src/run.ts b/plugins/catalog-backend-module-incremental-ingestion/src/run.ts deleted file mode 100644 index 89698da1de..0000000000 --- a/plugins/catalog-backend-module-incremental-ingestion/src/run.ts +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 2022 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. - */ - -// Think this file will probably go away once we move to backend system -// And restructure into the /dev folder -// eslint-disable-next-line @backstage/no-undeclared-imports -import { createBackend } from '@backstage/backend-defaults'; -import { - coreServices, - createBackendModule, - createServiceFactory, -} from '@backstage/backend-plugin-api'; -import { ConfigReader } from '@backstage/config'; -import { - IncrementalEntityProvider, - incrementalIngestionProvidersExtensionPoint, -} from '.'; -import catalogModuleIncrementalIngestionEntityProvider from './alpha'; - -const provider: IncrementalEntityProvider = { - getProviderName: () => 'test-provider', - around: burst => burst(0), - next: async (_context, cursor) => { - await new Promise(resolve => setTimeout(resolve, 500)); - if (cursor === undefined || cursor < 3) { - console.log(`### Returning batch #${cursor}`); - return { done: false, entities: [], cursor: (cursor ?? 0) + 1 }; - } - - console.log('### Last batch reached, stopping'); - return { done: true }; - }, -}; - -async function main() { - const config = { - backend: { - baseUrl: 'http://localhost:7007', - listen: ':7007', - database: { client: 'better-sqlite3', connection: ':memory:' }, - }, - }; - - const backend = createBackend(); - - backend.add( - createServiceFactory({ - service: coreServices.rootConfig, - deps: {}, - factory: () => new ConfigReader(config), - }), - ); - backend.add(import('@backstage/plugin-catalog-backend/alpha')); - backend.add(catalogModuleIncrementalIngestionEntityProvider); - backend.add( - createBackendModule({ - pluginId: 'catalog', - moduleId: 'incremental-test-provider', - register(reg) { - reg.registerInit({ - deps: { extension: incrementalIngestionProvidersExtensionPoint }, - async init({ extension }) { - extension.addProvider({ - provider: provider, - options: { - burstInterval: { seconds: 1 }, - burstLength: { seconds: 10 }, - restLength: { seconds: 10 }, - }, - }); - }, - }); - }, - }), - ); - - await backend.start(); -} - -main().catch(error => { - console.error(error.stack); - process.exit(1); -}); diff --git a/packages/cli-node/src/run.ts b/plugins/kubernetes-backend/dev/index.ts similarity index 75% rename from packages/cli-node/src/run.ts rename to plugins/kubernetes-backend/dev/index.ts index e924b5c5b8..dc287d46cc 100644 --- a/packages/cli-node/src/run.ts +++ b/plugins/kubernetes-backend/dev/index.ts @@ -1,5 +1,5 @@ /* - * Copyright 2020 The Backstage Authors + * Copyright 2024 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. @@ -14,7 +14,8 @@ * limitations under the License. */ -import { execFile as execFileCb } from 'child_process'; -import { promisify } from 'util'; +import { createBackend } from '@backstage/backend-defaults'; -export const execFile = promisify(execFileCb); +const backend = createBackend(); +backend.add(import('../src')); +backend.start(); diff --git a/plugins/kubernetes-backend/src/run.ts b/plugins/kubernetes-backend/src/run.ts deleted file mode 100644 index b17ffa9d43..0000000000 --- a/plugins/kubernetes-backend/src/run.ts +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2020 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 yn from 'yn'; -import { getRootLogger } from '@backstage/backend-common'; -import { startStandaloneServer } from './service/standaloneServer'; - -const port = process.env.PLUGIN_PORT ? Number(process.env.PLUGIN_PORT) : 3004; -const enableCors = yn(process.env.PLUGIN_CORS, { default: false }); -const logger = getRootLogger(); - -startStandaloneServer({ port, enableCors, logger }).catch(err => { - logger.error(err); - process.exit(1); -}); - -process.on('SIGINT', () => { - logger.info('CTRL+C pressed; exiting.'); - process.exit(0); -}); diff --git a/plugins/kubernetes-backend/src/service/standaloneApplication.ts b/plugins/kubernetes-backend/src/service/standaloneApplication.ts deleted file mode 100644 index dd87d33827..0000000000 --- a/plugins/kubernetes-backend/src/service/standaloneApplication.ts +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2020 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 { - errorHandler, - notFoundHandler, - requestLoggingHandler, - HostDiscovery, -} from '@backstage/backend-common'; -import compression from 'compression'; -import cors from 'cors'; -import express from 'express'; -import helmet from 'helmet'; -import { Logger } from 'winston'; -import { createRouter } from './router'; -import { ConfigReader } from '@backstage/config'; -import { CatalogClient } from '@backstage/catalog-client'; -import { PermissionEvaluator } from '@backstage/plugin-permission-common'; - -export interface ApplicationOptions { - enableCors: boolean; - logger: Logger; -} - -export async function createStandaloneApplication( - options: ApplicationOptions, -): Promise { - const { enableCors, logger } = options; - const config = new ConfigReader({}); - const discovery = HostDiscovery.fromConfig(config); - - const app = express(); - - const catalogApi = new CatalogClient({ - discoveryApi: HostDiscovery.fromConfig(config), - }); - - const permissions = {} as PermissionEvaluator; - - app.use(helmet()); - if (enableCors) { - app.use(cors()); - } - app.use(compression()); - app.use(express.json()); - app.use(requestLoggingHandler()); - app.use( - '/', - await createRouter({ logger, config, discovery, catalogApi, permissions }), - ); - app.use(notFoundHandler()); - app.use(errorHandler()); - - return app; -} diff --git a/plugins/kubernetes-backend/src/service/standaloneServer.ts b/plugins/kubernetes-backend/src/service/standaloneServer.ts deleted file mode 100644 index 9f9217fd4c..0000000000 --- a/plugins/kubernetes-backend/src/service/standaloneServer.ts +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2020 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 { Server } from 'http'; -import { Logger } from 'winston'; -import { createStandaloneApplication } from './standaloneApplication'; - -export interface ServerOptions { - port: number; - enableCors: boolean; - logger: Logger; -} - -export async function startStandaloneServer( - options: ServerOptions, -): Promise { - const logger = options.logger.child({ service: 'kubernetes-backend' }); - - logger.debug('Creating application...'); - const app = await createStandaloneApplication({ - enableCors: options.enableCors, - logger, - }); - - logger.debug('Starting application server...'); - return await new Promise((resolve, reject) => { - const server = app.listen(options.port, (err?: Error) => { - if (err) { - reject(err); - return; - } - - logger.info(`Listening on port ${options.port}`); - resolve(server); - }); - }); -}