Skip to content

Commit

Permalink
fix(cli) use cli directly as a fallback
Browse files Browse the repository at this point in the history
This commit changes the CLI to try generating a command to export a plugin
package as a dynamic plugin if the plugin package doesn't have an
export-dynamic script defined.

Signed-off-by: Stan Lewis <gashcrumb@gmail.com>
  • Loading branch information
gashcrumb committed Nov 21, 2024
1 parent a044779 commit 7443eb6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-trees-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@janus-idp/cli": patch
---

fix(cli): try using npx if a plugin package does not have an export-dynamic script
50 changes: 37 additions & 13 deletions packages/cli/src/commands/package-dynamic-plugins/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PackageRoles } from '@backstage/cli-node';
import chalk from 'chalk';
import { OptionValues } from 'commander';
import fs from 'fs-extra';
import { PackageJson } from 'type-fest';
import YAML from 'yaml';

import os from 'os';
Expand Down Expand Up @@ -31,17 +32,18 @@ export async function command(opts: OptionValues): Promise<void> {
return;
}
}
const workspacePackage = await fs.readJson(
const workspacePackage = (await fs.readJson(
paths.resolveTarget('package.json'),
);
)) as PackageJson;
const workspacePackageRole =
PackageRoles.detectRoleFromPackage(workspacePackage);
const workspacePackageRoleInfo =
workspacePackageRole !== undefined
? PackageRoles.getRoleInfo(workspacePackageRole)
: undefined;
const isMonoRepo =
typeof workspacePackage.workspaces?.packages !== 'undefined';
typeof (workspacePackage.workspaces as PackageJson.WorkspaceConfig)
.packages !== 'undefined';
// Find all plugin packages in the workspace
const packages = isMonoRepo
? await discoverPluginPackages()
Expand All @@ -56,20 +58,42 @@ export async function command(opts: OptionValues): Promise<void> {
];
// run export-dynamic on each plugin package
for (const pluginPkg of packages) {
const { packageDirectory, packageFilePath } = pluginPkg;
const { packageDirectory, packageFilePath, packageJson } = pluginPkg;
if (
!fs.existsSync(path.join(packageDirectory, 'dist-dynamic')) ||
forceExport
) {
Task.log(
`Running export-dynamic-plugin on plugin package ${packageFilePath}`,
);
try {
await Task.forCommand(`yarn export-dynamic`, { cwd: packageDirectory });
} catch (err) {
if (
Object.keys(packageJson.scripts as { [key: string]: string }).find(
script => script === 'export-dynamic',
)
) {
Task.log(
`Running existing export-dynamic script on plugin package ${packageFilePath}`,
);
try {
await Task.forCommand(`yarn export-dynamic`, {
cwd: packageDirectory,
});
} catch (err) {
Task.log(
`Encountered an error running 'yarn export-dynamic' on plugin package ${packageFilePath}, this plugin will not be packaged. The error was ${err}`,
);
}
} else {
Task.log(
`Encountered an error running 'yarn export-dynamic on plugin package ${packageFilePath}, this plugin will not be packaged. The error was ${err}`,
`Using generated command to export plugin package ${packageFilePath}`,
);
try {
await Task.forCommand(
`${process.execPath} ${process.argv[1]} package export-dynamic-plugin`,
{ cwd: packageDirectory },
);
} catch (err) {
Task.log(
`Encountered an error running 'npx @janus-idp/cli' on plugin package ${packageFilePath}, this plugin will not be packaged. The error was ${err}`,
);
}
}
} else {
Task.log(
Expand All @@ -93,7 +117,7 @@ export async function command(opts: OptionValues): Promise<void> {
packageRoleInfo,
} = pluginPkg;
const packageName =
packageJson.name.replace(/^@/, '').replace(/\//, '-') +
packageJson.name!.replace(/^@/, '').replace(/\//, '-') +
((packageRoleInfo && packageRoleInfo.platform) === 'node'
? '-dynamic'
: '');
Expand Down Expand Up @@ -252,7 +276,7 @@ async function discoverPluginPackages() {
return (
await Promise.all(
packageJsonFilePaths.map(async packageFilePath => {
const packageJson = await fs.readJson(packageFilePath);
const packageJson = (await fs.readJson(packageFilePath)) as PackageJson;
const packageRole = PackageRoles.getRoleFromPackage(packageJson);
const packageRoleInfo = PackageRoles.getRoleInfo(packageRole || '');
const packageDirectory = path.dirname(packageFilePath);
Expand Down

0 comments on commit 7443eb6

Please sign in to comment.