diff --git a/packages/cli/src/commands/pack.ts b/packages/cli/src/commands/pack.ts index 9c101e97fe..8fbbdb3d74 100644 --- a/packages/cli/src/commands/pack.ts +++ b/packages/cli/src/commands/pack.ts @@ -19,8 +19,15 @@ import { revertProductionPack, } from '../lib/packager/productionPack'; import { paths } from '../lib/paths'; +import fs from 'fs-extra'; +import { publishPreflightCheck } from '../lib/publishing'; export const pre = async () => { + publishPreflightCheck({ + dir: paths.targetDir, + packageJson: await fs.readJson(paths.resolveTarget('package.json')), + }); + await productionPack({ packageDir: paths.targetDir }); }; diff --git a/packages/cli/src/commands/repo/fix.ts b/packages/cli/src/commands/repo/fix.ts index 88070ceb8f..c170c5c3fd 100644 --- a/packages/cli/src/commands/repo/fix.ts +++ b/packages/cli/src/commands/repo/fix.ts @@ -25,6 +25,7 @@ import { OptionValues } from 'commander'; import fs from 'fs-extra'; import { resolve as resolvePath, posix, relative as relativePath } from 'path'; import { paths } from '../../lib/paths'; +import { publishPreflightCheck } from '../../lib/publishing'; /** * A mutable object representing a package.json file with potential fixes. @@ -438,7 +439,13 @@ export async function command(opts: OptionValues): Promise { // Fixers that only apply to repos that publish packages if (opts.publish) { - fixers.push(fixRepositoryField, fixPluginId, fixPluginPackages); + fixers.push( + fixRepositoryField, + fixPluginId, + fixPluginPackages, + // Run the publish preflight check too, to make sure we don't uncover errors during publishing + publishPreflightCheck, + ); } for (const fixer of fixers) { diff --git a/packages/cli/src/lib/publishing.ts b/packages/cli/src/lib/publishing.ts new file mode 100644 index 0000000000..20e4241985 --- /dev/null +++ b/packages/cli/src/lib/publishing.ts @@ -0,0 +1,71 @@ +/* + * 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 { BackstagePackage } from '@backstage/cli-node'; + +/** + * A basic check that throws if a packages doesn't contain required backstage metadata for publishing + */ +export function publishPreflightCheck(pkg: BackstagePackage): void { + const { name, backstage } = pkg.packageJson; + if (!backstage || !name) { + return; + } + + const { role } = backstage; + + if ( + role === 'backend-plugin' || + role === 'backend-plugin-module' || + role === 'frontend-plugin' || + role === 'frontend-plugin-module' + ) { + if (!backstage.pluginId) { + throw new Error( + `Plugin package ${name} is missing a backstage.pluginId, please run 'backstage-cli repo fix --publish'`, + ); + } + } + + if (role === 'backend-plugin' || role === 'frontend-plugin') { + if (!backstage.pluginPackages) { + throw new Error( + `Plugin package ${name} is missing a backstage.pluginPackages, please run 'backstage-cli repo fix --publish'`, + ); + } + } + + if ( + backstage.pluginId && + (role === 'common-library' || + role === 'node-library' || + role === 'web-library') + ) { + if (!backstage.pluginPackages) { + throw new Error( + `Plugin library package ${name} is missing a backstage.pluginPackages, please run 'backstage-cli repo fix --publish'`, + ); + } + } + + if (role === 'backend-plugin-module' || role === 'frontend-plugin-module') { + if (!backstage.pluginPackage) { + throw new Error( + `Plugin module package ${name} is missing a backstage.pluginPackage, please run 'backstage-cli repo fix --publish'`, + ); + } + } +}