diff --git a/.changeset/nice-hairs-lick.md b/.changeset/nice-hairs-lick.md new file mode 100644 index 0000000000..4229b5aa98 --- /dev/null +++ b/.changeset/nice-hairs-lick.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Adds new web-library package option when generating a new plugin diff --git a/packages/cli/src/lib/new/factories/index.ts b/packages/cli/src/lib/new/factories/index.ts index 0764d33e2c..80a24702e0 100644 --- a/packages/cli/src/lib/new/factories/index.ts +++ b/packages/cli/src/lib/new/factories/index.ts @@ -16,5 +16,6 @@ export { frontendPlugin } from './frontendPlugin'; export { backendPlugin } from './backendPlugin'; +export { webLibraryPackage } from './webLibraryPackage'; export { pluginCommon } from './pluginCommon'; export { scaffolderModule } from './scaffolderModule'; diff --git a/packages/cli/src/lib/new/factories/webLibraryPackage.test.ts b/packages/cli/src/lib/new/factories/webLibraryPackage.test.ts new file mode 100644 index 0000000000..03be32dbaa --- /dev/null +++ b/packages/cli/src/lib/new/factories/webLibraryPackage.test.ts @@ -0,0 +1,152 @@ +/* + * 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. + */ + +import fs from 'fs-extra'; +import mockFs from 'mock-fs'; +import { resolve as resolvePath } from 'path'; +import { paths } from '../../paths'; +import { Task } from '../../tasks'; +import { FactoryRegistry } from '../FactoryRegistry'; +import { createMockOutputStream, mockPaths } from './common/testUtils'; +import { webLibraryPackage } from './webLibraryPackage'; + +describe('webLibraryPackage factory', () => { + beforeEach(() => { + mockPaths({ + targetRoot: '/root', + }); + }); + + afterEach(() => { + mockFs.restore(); + jest.resetAllMocks(); + }); + + it('should create a web library package', async () => { + const expectedwebLibraryPackageName = 'test'; + + mockFs({ + '/root': { + packages: mockFs.directory(), + }, + [paths.resolveOwn('templates')]: mockFs.load( + paths.resolveOwn('templates'), + ), + }); + + const options = await FactoryRegistry.populateOptions(webLibraryPackage, { + id: 'test', // name of web library package + }); + + let modified = false; + + const [output, mockStream] = createMockOutputStream(); + jest.spyOn(process, 'stderr', 'get').mockReturnValue(mockStream); + jest.spyOn(Task, 'forCommand').mockResolvedValue(); + + await webLibraryPackage.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 web-library package ${expectedwebLibraryPackageName}`, + 'Checking Prerequisites:', + `availability packages/${expectedwebLibraryPackageName}`, + '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 packages/${expectedwebLibraryPackageName}`, + ]); + + await expect( + fs.readJson( + `/root/packages/${expectedwebLibraryPackageName}/package.json`, + ), + ).resolves.toEqual( + expect.objectContaining({ + name: expectedwebLibraryPackageName, + private: true, + version: '1.0.0', + }), + ); + + expect(Task.forCommand).toHaveBeenCalledTimes(2); + expect(Task.forCommand).toHaveBeenCalledWith('yarn install', { + cwd: resolvePath(`/root/packages/${expectedwebLibraryPackageName}`), + optional: true, + }); + expect(Task.forCommand).toHaveBeenCalledWith('yarn lint --fix', { + cwd: resolvePath(`/root/packages/${expectedwebLibraryPackageName}`), + optional: true, + }); + }); + + it('should create a web library plugin with options and codeowners', async () => { + const expectedwebLibraryPackageName = 'test'; + + mockFs({ + '/root': { + CODEOWNERS: '', + packages: mockFs.directory(), + }, + [paths.resolveOwn('templates')]: mockFs.load( + paths.resolveOwn('templates'), + ), + }); + + const options = await FactoryRegistry.populateOptions(webLibraryPackage, { + id: 'test', + owner: '@backstage/test-owners', + }); + + const [, mockStream] = createMockOutputStream(); + jest.spyOn(process, 'stderr', 'get').mockReturnValue(mockStream); + jest.spyOn(Task, 'forCommand').mockResolvedValue(); + + await webLibraryPackage.create(options, { + scope: 'internal', + private: true, + isMonoRepo: false, + defaultVersion: '1.0.0', + markAsModified: () => {}, + createTemporaryDirectory: () => fs.mkdtemp('test'), + }); + + expect(Task.forCommand).toHaveBeenCalledTimes(2); + expect(Task.forCommand).toHaveBeenCalledWith('yarn install', { + cwd: resolvePath(`/root/${expectedwebLibraryPackageName}`), + optional: true, + }); + expect(Task.forCommand).toHaveBeenCalledWith('yarn lint --fix', { + cwd: resolvePath(`/root/${expectedwebLibraryPackageName}`), + optional: true, + }); + }); +}); diff --git a/packages/cli/src/lib/new/factories/webLibraryPackage.ts b/packages/cli/src/lib/new/factories/webLibraryPackage.ts new file mode 100644 index 0000000000..a160220cab --- /dev/null +++ b/packages/cli/src/lib/new/factories/webLibraryPackage.ts @@ -0,0 +1,71 @@ +/* + * 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. + */ + +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 webLibraryPackage = createFactory({ + name: 'web-library', + description: 'A new web-library package', + optionsDiscovery: async () => ({ + codeOwnersPath: await getCodeownersFilePath(paths.targetRoot), + }), + optionsPrompts: [pluginIdPrompt(), ownerPrompt()], + async create(options: Options, ctx: CreateContext) { + const { id } = options; + const name = ctx.scope ? `@${ctx.scope}/${id}` : `${id}`; + + Task.log(); + Task.log(`Creating web-library package ${chalk.cyan(name)}`); + + const targetDir = ctx.isMonoRepo + ? paths.resolveTargetRoot('packages', id) + : paths.resolveTargetRoot(`${id}`); + + await executePluginPackageTemplate(ctx, { + targetDir, + templateName: 'web-library-package', + values: { + id, + name, + pluginVersion: ctx.defaultVersion, + privatePackage: ctx.private, + npmRegistry: ctx.npmRegistry, + }, + }); + + if (options.owner) { + await addCodeownersEntry(`/packages/${id}`, 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/web-library-package/.eslintrc.js b/packages/cli/templates/web-library-package/.eslintrc.js new file mode 100644 index 0000000000..e2a53a6ad2 --- /dev/null +++ b/packages/cli/templates/web-library-package/.eslintrc.js @@ -0,0 +1 @@ +module.exports = require('@backstage/cli/config/eslint-factory')(__dirname); diff --git a/packages/cli/templates/web-library-package/README.md.hbs b/packages/cli/templates/web-library-package/README.md.hbs new file mode 100644 index 0000000000..0e2813c87a --- /dev/null +++ b/packages/cli/templates/web-library-package/README.md.hbs @@ -0,0 +1,12 @@ +# {{name}} + +_This package was created through the Backstage CLI_. + +## Installation + +Install the package via Yarn: + +```sh +cd # if within a monorepo +yarn add {{name}} +``` diff --git a/packages/cli/templates/web-library-package/package.json.hbs b/packages/cli/templates/web-library-package/package.json.hbs new file mode 100644 index 0000000000..957efef167 --- /dev/null +++ b/packages/cli/templates/web-library-package/package.json.hbs @@ -0,0 +1,37 @@ +{ + "name": "{{name}}", + "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.esm.js", + "types": "dist/index.d.ts" + }, + "backstage": { + "role": "web-library" + }, + "scripts": { + "start": "backstage-cli package start", + "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'}}", + "@testing-library/jest-dom": "{{versionQuery '@testing-library/jest-dom' '5.10.1'}}" + }, + "files": [ + "dist" + ] +} diff --git a/packages/cli/templates/web-library-package/src/index.ts.hbs b/packages/cli/templates/web-library-package/src/index.ts.hbs new file mode 100644 index 0000000000..cb0ff5c3b5 --- /dev/null +++ b/packages/cli/templates/web-library-package/src/index.ts.hbs @@ -0,0 +1 @@ +export {}; diff --git a/packages/cli/templates/web-library-package/src/setupTests.ts b/packages/cli/templates/web-library-package/src/setupTests.ts new file mode 100644 index 0000000000..7b0828bfa8 --- /dev/null +++ b/packages/cli/templates/web-library-package/src/setupTests.ts @@ -0,0 +1 @@ +import '@testing-library/jest-dom'; diff --git a/packages/cli/templates/web-library-package/tsconfig.json b/packages/cli/templates/web-library-package/tsconfig.json new file mode 100644 index 0000000000..ce3409d31f --- /dev/null +++ b/packages/cli/templates/web-library-package/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "@backstage/cli/config/tsconfig.json", + "include": [ + "src", + ], + "exclude": ["node_modules"], + "compilerOptions": { + "outDir": "dist-types", + "rootDir": "." + } +}