From 9d8528eb8d3ee4cf0b8cf5d04a5d478f169164c6 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 10 Mar 2023 12:04:28 +0100 Subject: [PATCH] cli: add node-plugin template Signed-off-by: Patrik Oldsberg --- packages/cli/src/lib/new/factories/index.ts | 1 + .../src/lib/new/factories/pluginNode.test.ts | 107 ++++++++++++++++++ .../cli/src/lib/new/factories/pluginNode.ts | 74 ++++++++++++ .../default-node-plugin-package/.eslintrc.js | 1 + .../default-node-plugin-package/README.md.hbs | 5 + .../package.json.hbs | 36 ++++++ .../src/index.ts.hbs | 18 +++ .../src/setupTests.ts | 1 + .../default-node-plugin-package/tsconfig.json | 9 ++ 9 files changed, 252 insertions(+) create mode 100644 packages/cli/src/lib/new/factories/pluginNode.test.ts create mode 100644 packages/cli/src/lib/new/factories/pluginNode.ts create mode 100644 packages/cli/templates/default-node-plugin-package/.eslintrc.js create mode 100644 packages/cli/templates/default-node-plugin-package/README.md.hbs create mode 100644 packages/cli/templates/default-node-plugin-package/package.json.hbs create mode 100644 packages/cli/templates/default-node-plugin-package/src/index.ts.hbs create mode 100644 packages/cli/templates/default-node-plugin-package/src/setupTests.ts create mode 100644 packages/cli/templates/default-node-plugin-package/tsconfig.json diff --git a/packages/cli/src/lib/new/factories/index.ts b/packages/cli/src/lib/new/factories/index.ts index 80a24702e0..9fc69e0ca4 100644 --- a/packages/cli/src/lib/new/factories/index.ts +++ b/packages/cli/src/lib/new/factories/index.ts @@ -18,4 +18,5 @@ export { frontendPlugin } from './frontendPlugin'; export { backendPlugin } from './backendPlugin'; export { webLibraryPackage } from './webLibraryPackage'; export { pluginCommon } from './pluginCommon'; +export { pluginNode } from './pluginNode'; export { scaffolderModule } from './scaffolderModule'; diff --git a/packages/cli/src/lib/new/factories/pluginNode.test.ts b/packages/cli/src/lib/new/factories/pluginNode.test.ts new file mode 100644 index 0000000000..50971ff191 --- /dev/null +++ b/packages/cli/src/lib/new/factories/pluginNode.test.ts @@ -0,0 +1,107 @@ +/* + * Copyright 2021 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 fs from 'fs-extra'; +import mockFs from 'mock-fs'; +import { sep, resolve as resolvePath } from 'path'; +import { paths } from '../../paths'; +import { Task } from '../../tasks'; +import { FactoryRegistry } from '../FactoryRegistry'; +import { createMockOutputStream, mockPaths } from './common/testUtils'; +import { pluginNode } from './pluginNode'; + +describe('pluginNode factory', () => { + beforeEach(() => { + mockPaths({ + targetRoot: '/root', + }); + }); + + afterEach(() => { + mockFs.restore(); + jest.resetAllMocks(); + }); + + it('should create a node plugin package', async () => { + mockFs({ + '/root': { + plugins: mockFs.directory(), + }, + [paths.resolveOwn('templates')]: mockFs.load( + paths.resolveOwn('templates'), + ), + }); + + const options = await FactoryRegistry.populateOptions(pluginNode, { + id: 'test', + }); + + let modified = false; + + const [output, mockStream] = createMockOutputStream(); + jest.spyOn(process, 'stderr', 'get').mockReturnValue(mockStream); + jest.spyOn(Task, 'forCommand').mockResolvedValue(); + + await pluginNode.create(options, { + private: true, + isMonoRepo: true, + defaultVersion: '1.0.0', + markAsModified: () => { + modified = true; + }, + createTemporaryDirectory: () => fs.mkdtemp('test'), + }); + + expect(modified).toBe(true); + + expect(output).toEqual([ + '', + 'Creating Node.js plugin library backstage-plugin-test-node', + 'Checking Prerequisites:', + `availability plugins${sep}test-node`, + 'creating temp dir', + 'Executing Template:', + 'copying .eslintrc.js', + 'templating README.md.hbs', + 'templating package.json.hbs', + 'templating index.ts.hbs', + 'copying setupTests.ts', + 'Installing:', + `moving plugins${sep}test-node`, + ]); + + await expect( + fs.readJson('/root/plugins/test-node/package.json'), + ).resolves.toEqual( + expect.objectContaining({ + name: 'backstage-plugin-test-node', + description: 'Node.js library for the test plugin', + private: true, + version: '1.0.0', + }), + ); + + expect(Task.forCommand).toHaveBeenCalledTimes(2); + expect(Task.forCommand).toHaveBeenCalledWith('yarn install', { + cwd: resolvePath('/root/plugins/test-node'), + optional: true, + }); + expect(Task.forCommand).toHaveBeenCalledWith('yarn lint --fix', { + cwd: resolvePath('/root/plugins/test-node'), + optional: true, + }); + }); +}); diff --git a/packages/cli/src/lib/new/factories/pluginNode.ts b/packages/cli/src/lib/new/factories/pluginNode.ts new file mode 100644 index 0000000000..44dbea9fce --- /dev/null +++ b/packages/cli/src/lib/new/factories/pluginNode.ts @@ -0,0 +1,74 @@ +/* + * Copyright 2021 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 chalk from 'chalk'; +import { paths } from '../../paths'; +import { addCodeownersEntry, getCodeownersFilePath } from '../../codeowners'; +import { createFactory, CreateContext } from '../types'; +import { Task } from '../../tasks'; +import { ownerPrompt, pluginIdPrompt } from './common/prompts'; +import { executePluginPackageTemplate } from './common/tasks'; + +type Options = { + id: string; + owner?: string; + codeOwnersPath?: string; +}; + +export const pluginNode = createFactory({ + name: 'plugin-node', + description: 'A new Node.js library plugin package', + optionsDiscovery: async () => ({ + codeOwnersPath: await getCodeownersFilePath(paths.targetRoot), + }), + optionsPrompts: [pluginIdPrompt(), ownerPrompt()], + async create(options: Options, ctx: CreateContext) { + const { id } = options; + const suffix = `${id}-node`; + const name = ctx.scope + ? `@${ctx.scope}/plugin-${suffix}` + : `backstage-plugin-${suffix}`; + + Task.log(); + Task.log(`Creating Node.js plugin library ${chalk.cyan(name)}`); + + const targetDir = ctx.isMonoRepo + ? paths.resolveTargetRoot('plugins', suffix) + : paths.resolveTargetRoot(`backstage-plugin-${suffix}`); + + await executePluginPackageTemplate(ctx, { + targetDir, + templateName: 'default-node-plugin-package', + values: { + id, + name, + privatePackage: ctx.private, + npmRegistry: ctx.npmRegistry, + pluginVersion: ctx.defaultVersion, + }, + }); + + if (options.owner) { + await addCodeownersEntry(`/plugins/${suffix}`, options.owner); + } + + await Task.forCommand('yarn install', { cwd: targetDir, optional: true }); + await Task.forCommand('yarn lint --fix', { + cwd: targetDir, + optional: true, + }); + }, +}); diff --git a/packages/cli/templates/default-node-plugin-package/.eslintrc.js b/packages/cli/templates/default-node-plugin-package/.eslintrc.js new file mode 100644 index 0000000000..e2a53a6ad2 --- /dev/null +++ b/packages/cli/templates/default-node-plugin-package/.eslintrc.js @@ -0,0 +1 @@ +module.exports = require('@backstage/cli/config/eslint-factory')(__dirname); diff --git a/packages/cli/templates/default-node-plugin-package/README.md.hbs b/packages/cli/templates/default-node-plugin-package/README.md.hbs new file mode 100644 index 0000000000..a9fc97935e --- /dev/null +++ b/packages/cli/templates/default-node-plugin-package/README.md.hbs @@ -0,0 +1,5 @@ +# {{name}} + +Welcome to the Node.js library package for the {{id}} plugin! + +_This plugin was created through the Backstage CLI_ diff --git a/packages/cli/templates/default-node-plugin-package/package.json.hbs b/packages/cli/templates/default-node-plugin-package/package.json.hbs new file mode 100644 index 0000000000..45becd1bea --- /dev/null +++ b/packages/cli/templates/default-node-plugin-package/package.json.hbs @@ -0,0 +1,36 @@ +{ + "name": "{{name}}", + "description": "Node.js library for the {{id}} plugin", + "version": "{{pluginVersion}}", + "main": "src/index.ts", + "types": "src/index.ts", + "license": "Apache-2.0", +{{#if privatePackage}} + "private": {{privatePackage}}, +{{/if}} + "publishConfig": { +{{#if npmRegistry}} + "registry": "{{npmRegistry}}", +{{/if}} + "access": "public", + "main": "dist/index.cjs.js", + "types": "dist/index.d.ts" + }, + "backstage": { + "role": "node-library" + }, + "scripts": { + "build": "backstage-cli package build", + "lint": "backstage-cli package lint", + "test": "backstage-cli package test", + "clean": "backstage-cli package clean", + "prepack": "backstage-cli package prepack", + "postpack": "backstage-cli package postpack" + }, + "devDependencies": { + "@backstage/cli": "{{versionQuery '@backstage/cli'}}" + }, + "files": [ + "dist" + ] +} diff --git a/packages/cli/templates/default-node-plugin-package/src/index.ts.hbs b/packages/cli/templates/default-node-plugin-package/src/index.ts.hbs new file mode 100644 index 0000000000..8f5ad37ffb --- /dev/null +++ b/packages/cli/templates/default-node-plugin-package/src/index.ts.hbs @@ -0,0 +1,18 @@ +/***/ +/** + * Node.js library for the {{id}} plugin. + * + * @packageDocumentation + */ + +// In this package you might for example export functions that +// help other plugins or module interact with your plugin. + +/** + * Does something useful. + * + * @public + */ +export function someFunction() { + // ... +} diff --git a/packages/cli/templates/default-node-plugin-package/src/setupTests.ts b/packages/cli/templates/default-node-plugin-package/src/setupTests.ts new file mode 100644 index 0000000000..cb0ff5c3b5 --- /dev/null +++ b/packages/cli/templates/default-node-plugin-package/src/setupTests.ts @@ -0,0 +1 @@ +export {}; diff --git a/packages/cli/templates/default-node-plugin-package/tsconfig.json b/packages/cli/templates/default-node-plugin-package/tsconfig.json new file mode 100644 index 0000000000..5ae9aeb62d --- /dev/null +++ b/packages/cli/templates/default-node-plugin-package/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "@backstage/cli/config/tsconfig.json", + "include": ["src"], + "exclude": ["node_modules"], + "compilerOptions": { + "outDir": "dist-types", + "rootDir": "." + } +}