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 <poldsberg@gmail.com>
Made-with: Cursor
This commit is contained in:
Patrik Oldsberg
2026-03-02 20:49:56 +01:00
parent f867152e8b
commit e8a5a20f7d
4 changed files with 80 additions and 32 deletions
+46
View File
@@ -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<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 any).default ??
mod) as ActionExports<TModule>;
const actionFunc = actualModule[exportName] as ActionFunc;
await actionFunc(...args);
process.exit(0);
} catch (error) {
assertError(error);
exitWithError(error);
}
};
}
@@ -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);
};
@@ -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);
};
+2 -21
View File
@@ -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'),
},
});