2024-05-15 05:14:01 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
import { termost } from "termost";
|
2024-05-16 06:21:54 +02:00
|
|
|
import * as child_process from "child_process";
|
2024-05-15 05:14:01 +02:00
|
|
|
|
|
|
|
export type CliCommandOptions = {
|
|
|
|
isSilent: boolean;
|
|
|
|
reactAppRootDirPath: string | undefined;
|
|
|
|
};
|
|
|
|
|
2024-05-16 06:21:54 +02:00
|
|
|
const program = termost<CliCommandOptions>("Keycloak theme builder");
|
2024-05-15 05:14:01 +02:00
|
|
|
|
2024-05-18 03:12:07 +02:00
|
|
|
const optionsKeys: string[] = [];
|
|
|
|
|
2024-05-15 05:14:01 +02:00
|
|
|
program
|
|
|
|
.option({
|
|
|
|
"key": "reactAppRootDirPath",
|
2024-05-18 03:12:07 +02:00
|
|
|
"name": (() => {
|
|
|
|
const long = "project";
|
|
|
|
const short = "p";
|
|
|
|
|
|
|
|
optionsKeys.push(long, short);
|
|
|
|
|
|
|
|
return { long, short };
|
|
|
|
})(),
|
2024-05-15 05:14:01 +02:00
|
|
|
"description": "https://docs.keycloakify.dev/build-options#project-or-p-cli-option",
|
|
|
|
"defaultValue": undefined
|
|
|
|
})
|
|
|
|
.option({
|
|
|
|
"key": "isSilent",
|
2024-05-18 03:12:07 +02:00
|
|
|
"name": (() => {
|
|
|
|
const name = "silent";
|
|
|
|
|
|
|
|
optionsKeys.push(name);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
})(),
|
2024-05-15 05:14:01 +02:00
|
|
|
"description": "https://docs.keycloakify.dev/build-options#silent",
|
|
|
|
"defaultValue": false
|
|
|
|
});
|
|
|
|
|
2024-05-18 03:12:07 +02:00
|
|
|
function skip(_context: any, argv: { options: Record<string, unknown> }) {
|
|
|
|
const unrecognizedOptionKey = Object.keys(argv.options).find(key => !optionsKeys.includes(key));
|
|
|
|
|
|
|
|
if (unrecognizedOptionKey !== undefined) {
|
|
|
|
console.error(`keycloakify: Unrecognized option: ${unrecognizedOptionKey.length === 1 ? "-" : "--"}${unrecognizedOptionKey}`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-05-18 03:35:55 +02:00
|
|
|
async function runAndLogErrors(fn: () => Promise<void>) {
|
|
|
|
try {
|
|
|
|
await fn();
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-15 05:14:01 +02:00
|
|
|
program
|
|
|
|
.command({
|
|
|
|
"name": "build",
|
|
|
|
"description": "Build the theme (default subcommand)."
|
|
|
|
})
|
|
|
|
.task({
|
2024-05-18 03:12:07 +02:00
|
|
|
skip,
|
2024-05-18 08:56:11 +02:00
|
|
|
"handler": cliCommandOptions =>
|
|
|
|
runAndLogErrors(async () => {
|
|
|
|
const { command } = await import("./keycloakify");
|
|
|
|
|
|
|
|
await runAndLogErrors(() => command({ cliCommandOptions }));
|
|
|
|
})
|
2024-05-15 05:14:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
program
|
|
|
|
.command({
|
|
|
|
"name": "download-builtin-keycloak-theme",
|
|
|
|
"description": "Download the built-in Keycloak theme."
|
|
|
|
})
|
|
|
|
.task({
|
2024-05-18 03:12:07 +02:00
|
|
|
skip,
|
2024-05-18 08:56:11 +02:00
|
|
|
"handler": cliCommandOptions =>
|
|
|
|
runAndLogErrors(async () => {
|
|
|
|
const { command } = await import("./download-builtin-keycloak-theme");
|
|
|
|
|
|
|
|
await runAndLogErrors(() => command({ cliCommandOptions }));
|
|
|
|
})
|
2024-05-15 05:14:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
program
|
|
|
|
.command({
|
|
|
|
"name": "eject-keycloak-page",
|
|
|
|
"description": "Eject a Keycloak page."
|
|
|
|
})
|
|
|
|
.task({
|
2024-05-18 03:12:07 +02:00
|
|
|
skip,
|
2024-05-18 08:56:11 +02:00
|
|
|
"handler": cliCommandOptions =>
|
|
|
|
runAndLogErrors(async () => {
|
|
|
|
const { command } = await import("./eject-keycloak-page");
|
|
|
|
|
|
|
|
await runAndLogErrors(() => command({ cliCommandOptions }));
|
|
|
|
})
|
2024-05-15 05:14:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
program
|
|
|
|
.command({
|
|
|
|
"name": "initialize-email-theme",
|
|
|
|
"description": "Initialize an email theme."
|
|
|
|
})
|
|
|
|
.task({
|
2024-05-18 03:12:07 +02:00
|
|
|
skip,
|
2024-05-18 08:56:11 +02:00
|
|
|
"handler": cliCommandOptions =>
|
|
|
|
runAndLogErrors(async () => {
|
|
|
|
const { command } = await import("./initialize-email-theme");
|
|
|
|
|
|
|
|
await runAndLogErrors(() => command({ cliCommandOptions }));
|
|
|
|
})
|
2024-05-15 05:14:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
program
|
|
|
|
.command({
|
2024-05-18 04:17:45 +02:00
|
|
|
"name": "start-keycloak-container",
|
|
|
|
"description": "Spin up a Keycloak container with the theme preloaded and the realm pre configured."
|
2024-05-15 05:14:01 +02:00
|
|
|
})
|
|
|
|
.task({
|
2024-05-18 03:12:07 +02:00
|
|
|
skip,
|
2024-05-18 08:56:11 +02:00
|
|
|
"handler": cliCommandOptions =>
|
|
|
|
runAndLogErrors(async () => {
|
|
|
|
const { command } = await import("./start-keycloak-container");
|
|
|
|
|
|
|
|
await runAndLogErrors(() => command({ cliCommandOptions }));
|
|
|
|
})
|
2024-05-15 05:14:01 +02:00
|
|
|
});
|
2024-05-16 06:21:54 +02:00
|
|
|
|
2024-05-16 09:20:37 +02:00
|
|
|
program
|
|
|
|
.command({
|
2024-05-18 04:17:45 +02:00
|
|
|
"name": "copy-keycloak-resources-to-public",
|
|
|
|
"description": "(Webpack/Create-React-App only) Copy Keycloak default theme resources to the public directory."
|
2024-05-16 09:20:37 +02:00
|
|
|
})
|
|
|
|
.task({
|
2024-05-18 03:12:07 +02:00
|
|
|
skip,
|
2024-05-18 08:56:11 +02:00
|
|
|
"handler": cliCommandOptions =>
|
|
|
|
runAndLogErrors(async () => {
|
|
|
|
const { command } = await import("./copy-keycloak-resources-to-public");
|
|
|
|
|
|
|
|
await runAndLogErrors(() => command({ cliCommandOptions }));
|
|
|
|
})
|
2024-05-16 09:20:37 +02:00
|
|
|
});
|
|
|
|
|
2024-05-16 06:21:54 +02:00
|
|
|
// Fallback to build command if no command is provided
|
|
|
|
{
|
|
|
|
const [, , ...rest] = process.argv;
|
|
|
|
|
2024-05-18 03:12:07 +02:00
|
|
|
if (rest.length === 0 || (rest[0].startsWith("-") && rest[0] !== "--help" && rest[0] !== "-h")) {
|
|
|
|
const { status } = child_process.spawnSync("npx", ["keycloakify", "build", ...rest], {
|
2024-05-16 06:21:54 +02:00
|
|
|
"stdio": "inherit"
|
|
|
|
});
|
2024-05-18 03:12:07 +02:00
|
|
|
|
|
|
|
process.exit(status ?? 1);
|
2024-05-16 06:21:54 +02:00
|
|
|
}
|
|
|
|
}
|