Slim cli-plugin-api to only export createCliPlugin

Move error handling and lazy utilities back to @backstage/cli since they
are host-only concerns. The cli-plugin-api internals subpath provides
isCliPlugin and initializeCliPlugin for the CLI host.

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
Made-with: Cursor
This commit is contained in:
Patrik Oldsberg
2026-03-11 08:09:32 +01:00
parent 0be3eab18b
commit d6caf7f13b
22 changed files with 175 additions and 193 deletions
@@ -21,7 +21,7 @@ import { JSONSchema7 as JSONSchema } from 'json-schema';
import openBrowser from 'react-dev-utils/openBrowser';
import chalk from 'chalk';
import { loadCliConfig } from '../lib/config';
import type { CommandContext } from '@backstage/cli-plugin-api';
import type { CommandContext } from '../../../wiring/types';
const DOCS_URL = 'https://config.backstage.io';
@@ -19,7 +19,7 @@ import { stringify as stringifyYaml } from 'yaml';
import { AppConfig, ConfigReader } from '@backstage/config';
import { loadCliConfig } from '../lib/config';
import { ConfigSchema, ConfigVisibility } from '@backstage/config-loader';
import type { CommandContext } from '@backstage/cli-plugin-api';
import type { CommandContext } from '../../../wiring/types';
export default async ({ args, info }: CommandContext) => {
const {
@@ -20,7 +20,7 @@ import { stringify as stringifyYaml } from 'yaml';
import { loadCliConfig } from '../lib/config';
import { JsonObject } from '@backstage/types';
import { mergeConfigSchemas } from '@backstage/config-loader';
import type { CommandContext } from '@backstage/cli-plugin-api';
import type { CommandContext } from '../../../wiring/types';
export default async ({ args, info }: CommandContext) => {
const {
@@ -16,7 +16,7 @@
import { cli } from 'cleye';
import { loadCliConfig } from '../lib/config';
import type { CommandContext } from '@backstage/cli-plugin-api';
import type { CommandContext } from '../../../wiring/types';
export default async ({ args, info }: CommandContext) => {
const {
@@ -25,7 +25,7 @@ import {
} from '@backstage/cli-node';
import { minimatch } from 'minimatch';
import fs from 'fs-extra';
import type { CommandContext } from '@backstage/cli-plugin-api';
import type { CommandContext } from '../../../wiring/types';
/**
* Attempts to read package.json from node_modules for a given package name.
@@ -33,7 +33,7 @@ import {
formatMessagePath,
validatePattern,
} from '../lib/messageFilePath';
import type { CommandContext } from '@backstage/cli-plugin-api';
import type { CommandContext } from '../../../wiring/types';
export default async ({ args, info }: CommandContext) => {
const {
@@ -28,7 +28,7 @@ import {
createMessagePathParser,
formatMessagePath,
} from '../lib/messageFilePath';
import type { CommandContext } from '@backstage/cli-plugin-api';
import type { CommandContext } from '../../../wiring/types';
interface ManifestRefEntry {
package: string;
+2 -2
View File
@@ -18,13 +18,13 @@ import { CommandGraph } from './CommandGraph';
import {
isCliPlugin,
initializeCliPlugin,
exitWithError,
} from '@backstage/cli-plugin-api';
} from '@backstage/cli-plugin-api/internals';
import type { BackstageCommand, CliFeature } from '@backstage/cli-plugin-api';
import { CommandRegistry } from './CommandRegistry';
import { Command } from 'commander';
import { version } from './version';
import chalk from 'chalk';
import { exitWithError } from './errors';
import { ForwardedError } from '@backstage/errors';
import { isPromise } from 'node:util/types';
+46 -1
View File
@@ -14,4 +14,49 @@
* limitations under the License.
*/
export { ExitCodeError, exitWithError } from '@backstage/cli-plugin-api';
import { CustomErrorBase, isError, stringifyError } from '@backstage/errors';
import chalk from 'chalk';
export class ExitCodeError extends CustomErrorBase {
readonly code: number;
constructor(code: number, command?: string) {
super(
command
? `Command '${command}' exited with code ${code}`
: `Child exited with code ${code}`,
);
this.code = code;
}
}
function exit(message: string, code: number = 1): never {
process.stderr.write(`\n${chalk.red(message)}\n\n`);
process.exit(code);
}
export function exitWithError(error: unknown): never {
if (!isError(error)) {
process.stderr.write(`\n${chalk.red(stringifyError(error))}\n\n`);
process.exit(1);
}
switch (error.name) {
case 'InputError':
return exit(error.message, 74 /* input/output error */);
case 'NotFoundError':
return exit(error.message, 127 /* command not found */);
case 'NotImplementedError':
return exit(error.message, 64 /* command line usage error */);
case 'AuthenticationError':
case 'NotAllowedError':
return exit(error.message, 77 /* permissino denied */);
case 'ExitCodeError':
return exit(
error.message,
'code' in error && typeof error.code === 'number' ? error.code : 1,
);
default:
return exit(stringifyError(error), 1);
}
}
+31 -1
View File
@@ -14,4 +14,34 @@
* limitations under the License.
*/
export { lazy } from '@backstage/cli-plugin-api';
import { assertError } from '@backstage/errors';
import { exitWithError } from './errors';
type ActionFunc = (...args: any[]) => Promise<void>;
type ActionExports<TModule extends object> = {
[KName in keyof TModule as TModule[KName] extends ActionFunc
? KName
: never]: TModule[KName];
};
// Wraps an action function so that it always exits and handles errors
export function lazy<TModule extends object>(
moduleLoader: () => Promise<TModule>,
exportName: keyof ActionExports<TModule>,
): (...args: any[]) => Promise<never> {
return async (...args: any[]) => {
try {
const mod = await moduleLoader();
const actualModule = (
mod as unknown as { default: ActionExports<TModule> }
).default;
const actionFunc = actualModule[exportName] as ActionFunc;
await actionFunc(...args);
process.exit(0);
} catch (error) {
assertError(error);
exitWithError(error);
}
};
}
-1
View File
@@ -14,7 +14,6 @@
* limitations under the License.
*/
// Re-export types from the plugin API for internal use within the CLI package.
export type {
CommandContext,
CommandExecuteFn,