cli: add package publishing preflight check

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-06-08 12:36:29 +02:00
parent f85a7f2a5a
commit b0de760792
3 changed files with 86 additions and 1 deletions
+7
View File
@@ -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 });
};
+8 -1
View File
@@ -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<void> {
// 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) {
+71
View File
@@ -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'`,
);
}
}
}