From 5835f06524c6de4bed99a0291443b4e0add8f4db Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 18 Jun 2020 14:10:02 +0200 Subject: [PATCH] chore(scaffolder): Tidy up some things around the PR to make a litte cleaner and add some comments in places --- .../react-ssr-template/template-info.json | 6 --- plugins/scaffolder-backend/src/run.ts | 34 ------------- .../src/scaffolder/prepare/file.test.ts | 6 ++- .../scaffolder-backend/src/service/router.ts | 12 +++-- .../src/service/standaloneServer.ts | 51 ------------------- 5 files changed, 11 insertions(+), 98 deletions(-) delete mode 100644 plugins/scaffolder-backend/sample-templates/react-ssr-template/template-info.json delete mode 100644 plugins/scaffolder-backend/src/run.ts delete mode 100644 plugins/scaffolder-backend/src/service/standaloneServer.ts diff --git a/plugins/scaffolder-backend/sample-templates/react-ssr-template/template-info.json b/plugins/scaffolder-backend/sample-templates/react-ssr-template/template-info.json deleted file mode 100644 index a733b790fa..0000000000 --- a/plugins/scaffolder-backend/sample-templates/react-ssr-template/template-info.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "id": "react-ssr-template", - "name": "SSR React Website", - "description": "Next.js application skeleton for creating isomorphic web applications.", - "ownerId": "something" -} diff --git a/plugins/scaffolder-backend/src/run.ts b/plugins/scaffolder-backend/src/run.ts deleted file mode 100644 index 133aad163e..0000000000 --- a/plugins/scaffolder-backend/src/run.ts +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2020 Spotify AB - * - * 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 { getRootLogger } from '@backstage/backend-common'; -import { startStandaloneServer } from './service/standaloneServer'; - -const port = process.env.PLUGIN_PORT ? Number(process.env.PLUGIN_PORT) : 3004; -const enableCors = process.env.PLUGIN_CORS - ? Boolean(process.env.PLUGIN_CORS) - : 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/scaffolder-backend/src/scaffolder/prepare/file.test.ts b/plugins/scaffolder-backend/src/scaffolder/prepare/file.test.ts index b279b56ea3..b9472fbc8d 100644 --- a/plugins/scaffolder-backend/src/scaffolder/prepare/file.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/prepare/file.test.ts @@ -25,8 +25,10 @@ import { const setupTest = async (fixturePath: string) => { const locationForTemplateYaml = path.resolve( - __dirname, - '../../../fixtures', + path.dirname( + require.resolve('@backstage/plugin-scaffolder-backend/package'), + ), + 'fixtures', fixturePath, ); diff --git a/plugins/scaffolder-backend/src/service/router.ts b/plugins/scaffolder-backend/src/service/router.ts index 74507d528c..b05983e1b4 100644 --- a/plugins/scaffolder-backend/src/service/router.ts +++ b/plugins/scaffolder-backend/src/service/router.ts @@ -34,16 +34,17 @@ export async function createRouter( const logger = parentLogger.child({ plugin: 'scaffolder' }); router.post('/v1/jobs', async (_, res) => { - // TODO(blam): Actually make this function work + // TODO(blam): Create a unique job here and return the ID so that + // The end user can poll for updates on the current job res.status(201).json({ accepted: true }); + // TODO(blam): Take this entity from the post body sent from the frontend const mockEntity: TemplateEntityV1alpha1 = { apiVersion: 'backstage.io/v1alpha1', kind: 'Template', metadata: { annotations: { - 'backstage.io/managed-by-location': - 'file:/Users/blam/dev/spotify/backstage/plugins/scaffolder-backend/sample-templates/react-ssr-template/template.yaml', + 'backstage.io/managed-by-location': `file:${__dirname}/../../sample-templates/react-ssr-template/template.yaml`, }, name: 'react-ssr-template', title: 'React SSR Template', @@ -59,12 +60,13 @@ export async function createRouter( }, }; - // fetch the entity from service catalog here. + // Get the preparer for the mock entity const preparer = preparers.get(mockEntity); - // + // Run the preparer for the mock entity to produce a temporary directory with template in const path = await preparer.prepare(mockEntity); + // Run the templater on the mock directory with values from the post body await templater.run({ directory: path, values: { componentId: 'test' } }); }); diff --git a/plugins/scaffolder-backend/src/service/standaloneServer.ts b/plugins/scaffolder-backend/src/service/standaloneServer.ts deleted file mode 100644 index cad937d304..0000000000 --- a/plugins/scaffolder-backend/src/service/standaloneServer.ts +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2020 Spotify AB - * - * 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 { createTemplater, CookieCutter } from '../scaffolder'; -import { createServiceBuilder, useHotMemoize } from '@backstage/backend-common'; -import { createRouter } from './router'; -export interface ServerOptions { - port: number; - enableCors: boolean; - logger: Logger; -} - -export async function startStandaloneServer( - options: ServerOptions, -): Promise { - const logger = options.logger.child({ service: 'scaffolder-backend' }); - const templater = new CookieCutter(); - logger.debug('Creating application...'); - - const router = await createRouter({ - storage: null, - templater: createTemplater({ templater }), - logger, - }); - - const service = createServiceBuilder(module) - .enableCors({ origin: 'http://localhost:3000' }) - .addRouter('/catalog', router); - - return await service.start().catch(err => { - logger.error(err); - process.exit(1); - }); -} - -module.hot?.accept();