From e8a5a20f7ddf375a641df29ffbb5940581adcc67 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 2 Mar 2026 20:49:56 +0100 Subject: [PATCH] cli: split pack.ts into prepack.ts and postpack.ts Each file now has a default export matching the CommandExecuteFn pattern, removing the need for import wrapping in the loader. Signed-off-by: Patrik Oldsberg Made-with: Cursor --- packages/cli/src/lib/lazy.ts | 46 +++++++++++++++++++ .../build/commands/package/postpack.ts | 25 ++++++++++ .../commands/package/{pack.ts => prepack.ts} | 18 +++----- packages/cli/src/modules/build/index.ts | 23 +--------- 4 files changed, 80 insertions(+), 32 deletions(-) create mode 100644 packages/cli/src/lib/lazy.ts create mode 100644 packages/cli/src/modules/build/commands/package/postpack.ts rename packages/cli/src/modules/build/commands/package/{pack.ts => prepack.ts} (80%) diff --git a/packages/cli/src/lib/lazy.ts b/packages/cli/src/lib/lazy.ts new file mode 100644 index 0000000000..8919ed8c28 --- /dev/null +++ b/packages/cli/src/lib/lazy.ts @@ -0,0 +1,46 @@ +/* + * 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 { assertError } from '@backstage/errors'; +import { exitWithError } from '../lib/errors'; + +type ActionFunc = (...args: any[]) => Promise; +type ActionExports = { + [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( + moduleLoader: () => Promise, + exportName: keyof ActionExports, +): (...args: any[]) => Promise { + return async (...args: any[]) => { + try { + const mod = await moduleLoader(); + const actualModule = ((mod as any).default ?? + mod) as ActionExports; + const actionFunc = actualModule[exportName] as ActionFunc; + await actionFunc(...args); + + process.exit(0); + } catch (error) { + assertError(error); + exitWithError(error); + } + }; +} diff --git a/packages/cli/src/modules/build/commands/package/postpack.ts b/packages/cli/src/modules/build/commands/package/postpack.ts new file mode 100644 index 0000000000..8ca1c800ce --- /dev/null +++ b/packages/cli/src/modules/build/commands/package/postpack.ts @@ -0,0 +1,25 @@ +/* + * Copyright 2020 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 { cli } from 'cleye'; +import { targetPaths } from '@backstage/cli-common'; +import { revertProductionPack } from '../../lib/packager/productionPack'; +import type { CommandContext } from '../../../../wiring/types'; + +export default async ({ args, info }: CommandContext) => { + cli({ help: info }, undefined, args); + await revertProductionPack(targetPaths.dir); +}; diff --git a/packages/cli/src/modules/build/commands/package/pack.ts b/packages/cli/src/modules/build/commands/package/prepack.ts similarity index 80% rename from packages/cli/src/modules/build/commands/package/pack.ts rename to packages/cli/src/modules/build/commands/package/prepack.ts index 0265c07093..39d92518bc 100644 --- a/packages/cli/src/modules/build/commands/package/pack.ts +++ b/packages/cli/src/modules/build/commands/package/prepack.ts @@ -14,17 +14,17 @@ * limitations under the License. */ -import { - productionPack, - revertProductionPack, -} from '../../lib/packager/productionPack'; -import { targetPaths } from '@backstage/cli-common'; - +import { cli } from 'cleye'; import fs from 'fs-extra'; +import { targetPaths } from '@backstage/cli-common'; +import { productionPack } from '../../lib/packager/productionPack'; import { publishPreflightCheck } from '../../lib/publishing'; import { createTypeDistProject } from '../../lib/typeDistProject'; +import type { CommandContext } from '../../../../wiring/types'; + +export default async ({ args, info }: CommandContext) => { + cli({ help: info }, undefined, args); -export const pre = async () => { publishPreflightCheck({ dir: targetPaths.dir, packageJson: await fs.readJson(targetPaths.resolve('package.json')), @@ -35,7 +35,3 @@ export const pre = async () => { featureDetectionProject: await createTypeDistProject(), }); }; - -export const post = async () => { - await revertProductionPack(targetPaths.dir); -}; diff --git a/packages/cli/src/modules/build/index.ts b/packages/cli/src/modules/build/index.ts index 794f5b8976..1e8d1d2060 100644 --- a/packages/cli/src/modules/build/index.ts +++ b/packages/cli/src/modules/build/index.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { cli } from 'cleye'; import { Command, Option } from 'commander'; import { createCliPlugin } from '../../wiring/factory'; import { lazy } from '../../wiring/lazy'; @@ -216,16 +215,7 @@ export const buildPlugin = createCliPlugin({ path: ['package', 'prepack'], description: 'Prepares a package for packaging before publishing', execute: { - loader: () => - import('./commands/package/pack').then(m => { - const { pre } = (m as any).default ?? m; - return { - default: async ({ args, info }) => { - cli({ help: info }, undefined, args); - await pre(); - }, - }; - }), + loader: () => import('./commands/package/prepack'), }, }); @@ -233,16 +223,7 @@ export const buildPlugin = createCliPlugin({ path: ['package', 'postpack'], description: 'Restores the changes made by the prepack command', execute: { - loader: () => - import('./commands/package/pack').then(m => { - const { post } = (m as any).default ?? m; - return { - default: async ({ args, info }) => { - cli({ help: info }, undefined, args); - await post(); - }, - }; - }), + loader: () => import('./commands/package/postpack'), }, });