Skip to content

Commit

Permalink
fix(cli): add flag to specify output directory
Browse files Browse the repository at this point in the history
This change adds an --export-to flag to the package-dynamic-plugins
CLI command. This flag will skip the container build step, and instead
copy the staged dynamic plugin assets to the specified output directory.
The addition of this flag makes the --tag argument optional, and only
required if --export-to is not specified.

Signed-off-by: Stan Lewis <gashcrumb@gmail.com>
  • Loading branch information
gashcrumb committed Oct 22, 2024
1 parent 844ac17 commit 5b58f39
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .changeset/mean-eagles-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@janus-idp/cli": patch
---

fix(cli) add flag to specify output directory

This change adds an --export-to flag to the package-dynamic-plugins CLI command. This flag will skip the container build step, and instead copy the staged dynamic plugin assets to the specified output directory. The addition of this flag makes the --tag argument optional, and only required if --export-to is not specified.
8 changes: 6 additions & 2 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,13 @@ export function registerScriptCommand(program: Command) {
'--preserve-temp-dir',
'Leave the temporary staging directory on the filesystem instead of deleting it',
)
.requiredOption(
.option(
'--export-to <directory>',
'Export the plugins to the specified directory, skips building the container image',
)
.option(
'-t, --tag <tag>',
'Tag name to use when building the plugin registry image',
'Tag name to use when building the plugin registry image. Required if "--export-to" is not specified',
)
.option(
'--use-docker',
Expand Down
44 changes: 33 additions & 11 deletions packages/cli/src/commands/package-dynamic-plugins/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import { paths } from '../../lib/paths';
import { Task } from '../../lib/tasks';

export async function command(opts: OptionValues): Promise<void> {
const { forceExport, preserveTempDir, tag, useDocker } = opts;
const { exportTo, forceExport, preserveTempDir, tag, useDocker } = opts;
if (!exportTo && !tag) {
Task.error(
`Neither ${chalk.white('--export-to')} or ${chalk.white('--tag')} was specified, either specify ${chalk.white('--export-to')} to export plugins to a directory or ${chalk.white('--tag')} to export plugins to a container image`,
);
return;
}
const containerTool = useDocker ? 'docker' : 'podman';
// check if the container tool is available
try {
Expand Down Expand Up @@ -95,7 +101,12 @@ export async function command(opts: OptionValues): Promise<void> {
const targetDirectory = path.join(tmpDir, packageName);
Task.log(`Copying '${distDirectory}' to '${targetDirectory}`);
try {
fs.cpSync(distDirectory, targetDirectory, { recursive: true });
// Copy the exported package to the staging area and ensure symlinks
// are copied as normal folders
fs.cpSync(distDirectory, targetDirectory, {
recursive: true,
dereference: true,
});
const {
name,
version,
Expand Down Expand Up @@ -158,18 +169,29 @@ export async function command(opts: OptionValues): Promise<void> {
metadataFile,
JSON.stringify(pluginRegistryMetadata, undefined, 2),
);
// run the command to generate the image
Task.log(`Creating image using ${containerTool}`);
await Task.forCommand(
`echo "from scratch
if (exportTo) {
// copy the temporary directory contents to the target directory
fs.mkdirSync(exportTo, { recursive: true });
Task.log(`Writing exported plugins to ${exportTo}`);
fs.readdirSync(tmpDir).forEach(entry => {
const source = path.join(tmpDir, entry);
const destination = path.join(exportTo, entry);
fs.copySync(source, destination, { recursive: true, overwrite: true });
});
} else {
// run the command to generate the image
Task.log(`Creating image using ${containerTool}`);
await Task.forCommand(
`echo "from scratch
COPY . .
" | ${containerTool} build --annotation com.redhat.rhdh.plugins='${JSON.stringify(pluginRegistryMetadata)}' -t '${tag}' -f - .
`,
{ cwd: tmpDir },
);
Task.log(`Successfully built image ${tag} with following plugins:`);
for (const plugin of pluginRegistryMetadata) {
Task.log(` ${chalk.white(Object.keys(plugin)[0])}`);
{ cwd: tmpDir },
);
Task.log(`Successfully built image ${tag} with following plugins:`);
for (const plugin of pluginRegistryMetadata) {
Task.log(` ${chalk.white(Object.keys(plugin)[0])}`);
}
}
// print out a configuration example based on available plugin data
try {
Expand Down

0 comments on commit 5b58f39

Please sign in to comment.