2024-05-15 05:14:01 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
import { termost } from "termost";
|
2024-05-19 04:02:36 +02:00
|
|
|
import { readThisNpmPackageVersion } from "./tools/readThisNpmPackageVersion";
|
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 = {
|
|
|
|
reactAppRootDirPath: string | undefined;
|
|
|
|
};
|
|
|
|
|
2024-05-19 04:26:01 +02:00
|
|
|
const program = termost<CliCommandOptions>(
|
|
|
|
{
|
|
|
|
"name": "keycloakify",
|
|
|
|
"description": "Keycloakify CLI",
|
|
|
|
"version": readThisNpmPackageVersion()
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"onException": error => {
|
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2024-05-15 05:14:01 +02:00
|
|
|
|
2024-05-18 03:12:07 +02:00
|
|
|
const optionsKeys: string[] = [];
|
|
|
|
|
2024-05-18 10:26:56 +02:00
|
|
|
program.option({
|
|
|
|
"key": "reactAppRootDirPath",
|
|
|
|
"name": (() => {
|
|
|
|
const long = "project";
|
|
|
|
const short = "p";
|
|
|
|
|
|
|
|
optionsKeys.push(long, short);
|
|
|
|
|
|
|
|
return { long, short };
|
|
|
|
})(),
|
2024-05-18 10:53:14 +02:00
|
|
|
"description": [
|
|
|
|
`For monorepos, path to the keycloakify project.`,
|
|
|
|
"Example: `npx keycloakify build --project packages/keycloak-theme`",
|
|
|
|
"https://docs.keycloakify.dev/build-options#project-or-p-cli-option"
|
|
|
|
].join(" "),
|
2024-05-18 10:26:56 +02:00
|
|
|
"defaultValue": undefined
|
|
|
|
});
|
2024-05-15 05:14:01 +02:00
|
|
|
|
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-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-19 04:26:01 +02:00
|
|
|
"handler": async cliCommandOptions => {
|
|
|
|
const { command } = await import("./keycloakify");
|
2024-05-18 08:56:11 +02:00
|
|
|
|
2024-05-19 04:26:01 +02:00
|
|
|
await command({ cliCommandOptions });
|
|
|
|
}
|
2024-05-15 05:14:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
program
|
2024-05-18 11:40:09 +02:00
|
|
|
.command<{ port: number; keycloakVersion: string | undefined }>({
|
2024-05-18 10:48:47 +02:00
|
|
|
"name": "start-keycloak",
|
|
|
|
"description": "Spin up a pre configured Docker image of Keycloak to test your theme."
|
2024-05-15 05:14:01 +02:00
|
|
|
})
|
2024-05-18 11:40:09 +02:00
|
|
|
.option({
|
|
|
|
"key": "port",
|
|
|
|
"name": (() => {
|
|
|
|
const name = "port";
|
|
|
|
|
|
|
|
optionsKeys.push(name);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
})(),
|
|
|
|
"description": "Keycloak server port.",
|
|
|
|
"defaultValue": 8080
|
|
|
|
})
|
|
|
|
.option({
|
|
|
|
"key": "keycloakVersion",
|
|
|
|
"name": (() => {
|
|
|
|
const name = "keycloak-version";
|
|
|
|
|
|
|
|
optionsKeys.push(name);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
})(),
|
|
|
|
"description": "Use a specific version of Keycloak.",
|
|
|
|
"defaultValue": undefined
|
|
|
|
})
|
2024-05-15 05:14:01 +02:00
|
|
|
.task({
|
2024-05-18 03:12:07 +02:00
|
|
|
skip,
|
2024-05-19 04:26:01 +02:00
|
|
|
"handler": async cliCommandOptions => {
|
|
|
|
const { command } = await import("./start-keycloak");
|
2024-05-18 08:56:11 +02:00
|
|
|
|
2024-05-19 04:26:01 +02:00
|
|
|
await command({ cliCommandOptions });
|
|
|
|
}
|
2024-05-15 05:14:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
program
|
|
|
|
.command({
|
2024-05-19 10:46:26 +02:00
|
|
|
"name": "download-keycloak-default-theme",
|
2024-05-18 10:48:47 +02:00
|
|
|
"description": "Download the built-in Keycloak theme."
|
2024-05-15 05:14:01 +02:00
|
|
|
})
|
|
|
|
.task({
|
2024-05-18 03:12:07 +02:00
|
|
|
skip,
|
2024-05-19 04:26:01 +02:00
|
|
|
"handler": async cliCommandOptions => {
|
2024-05-19 10:46:26 +02:00
|
|
|
const { command } = await import("./download-keycloak-default-theme");
|
2024-05-18 08:56:11 +02:00
|
|
|
|
2024-05-19 04:26:01 +02:00
|
|
|
await command({ cliCommandOptions });
|
|
|
|
}
|
2024-05-15 05:14:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
program
|
|
|
|
.command({
|
2024-05-19 10:46:26 +02:00
|
|
|
"name": "eject-page",
|
2024-05-18 10:48:47 +02:00
|
|
|
"description": "Eject a Keycloak page."
|
2024-05-15 05:14:01 +02:00
|
|
|
})
|
|
|
|
.task({
|
2024-05-18 03:12:07 +02:00
|
|
|
skip,
|
2024-05-19 04:26:01 +02:00
|
|
|
"handler": async cliCommandOptions => {
|
2024-05-19 10:46:26 +02:00
|
|
|
const { command } = await import("./eject-page");
|
2024-05-18 08:56:11 +02:00
|
|
|
|
2024-05-19 04:26:01 +02:00
|
|
|
await command({ cliCommandOptions });
|
|
|
|
}
|
2024-05-15 05:14:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
program
|
|
|
|
.command({
|
2024-05-18 10:48:47 +02:00
|
|
|
"name": "initialize-email-theme",
|
|
|
|
"description": "Initialize an email theme."
|
2024-05-15 05:14:01 +02:00
|
|
|
})
|
|
|
|
.task({
|
2024-05-18 03:12:07 +02:00
|
|
|
skip,
|
2024-05-19 04:26:01 +02:00
|
|
|
"handler": async cliCommandOptions => {
|
|
|
|
const { command } = await import("./initialize-email-theme");
|
2024-05-18 08:56:11 +02:00
|
|
|
|
2024-05-19 04:26:01 +02:00
|
|
|
await 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-19 04:26:01 +02:00
|
|
|
"handler": async cliCommandOptions => {
|
|
|
|
const { command } = await import("./copy-keycloak-resources-to-public");
|
2024-05-18 08:56:11 +02:00
|
|
|
|
2024-05-19 04:26:01 +02:00
|
|
|
await 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
|
|
|
}
|
|
|
|
}
|