2023-03-19 15:52:41 +01:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2024-02-11 20:15:18 +01:00
|
|
|
import { getThisCodebaseRootDirPath } from "./tools/getThisCodebaseRootDirPath";
|
2023-03-19 15:52:41 +01:00
|
|
|
import cliSelect from "cli-select";
|
2024-05-19 04:02:36 +02:00
|
|
|
import {
|
|
|
|
loginThemePageIds,
|
|
|
|
accountThemePageIds,
|
|
|
|
type LoginThemePageId,
|
|
|
|
type AccountThemePageId,
|
|
|
|
themeTypes,
|
|
|
|
type ThemeType
|
|
|
|
} from "./shared/constants";
|
2023-03-19 15:52:41 +01:00
|
|
|
import { capitalize } from "tsafe/capitalize";
|
2024-05-18 03:41:12 +02:00
|
|
|
import * as fs from "fs";
|
|
|
|
import { join as pathJoin, relative as pathRelative, dirname as pathDirname } from "path";
|
2023-03-19 15:52:41 +01:00
|
|
|
import { kebabCaseToCamelCase } from "./tools/kebabCaseToSnakeCase";
|
2023-03-20 00:03:15 +01:00
|
|
|
import { assert, Equals } from "tsafe/assert";
|
2024-05-15 05:14:01 +02:00
|
|
|
import { getThemeSrcDirPath } from "./shared/getThemeSrcDirPath";
|
|
|
|
import type { CliCommandOptions } from "./main";
|
|
|
|
import { readBuildOptions } from "./shared/buildOptions";
|
2024-05-19 10:23:38 +02:00
|
|
|
import chalk from "chalk";
|
2023-03-19 15:52:41 +01:00
|
|
|
|
2024-05-15 05:14:01 +02:00
|
|
|
export async function command(params: { cliCommandOptions: CliCommandOptions }) {
|
|
|
|
const { cliCommandOptions } = params;
|
2023-03-20 00:03:15 +01:00
|
|
|
|
2024-05-15 05:14:01 +02:00
|
|
|
const buildOptions = readBuildOptions({
|
|
|
|
cliCommandOptions
|
2024-02-11 16:17:38 +01:00
|
|
|
});
|
2023-09-03 23:26:34 +02:00
|
|
|
|
2024-05-19 10:23:38 +02:00
|
|
|
console.log(chalk.cyan("Theme type:"));
|
2024-05-15 05:14:01 +02:00
|
|
|
|
2023-03-20 00:03:15 +01:00
|
|
|
const { value: themeType } = await cliSelect<ThemeType>({
|
2024-05-20 15:48:51 +02:00
|
|
|
values: [...themeTypes]
|
2023-03-19 15:52:41 +01:00
|
|
|
}).catch(() => {
|
|
|
|
process.exit(-1);
|
|
|
|
});
|
|
|
|
|
2024-05-19 10:23:38 +02:00
|
|
|
console.log(`→ ${themeType}`);
|
|
|
|
|
|
|
|
console.log(chalk.cyan("Select the page you want to customize:"));
|
2023-03-20 00:03:15 +01:00
|
|
|
|
|
|
|
const { value: pageId } = await cliSelect<LoginThemePageId | AccountThemePageId>({
|
2024-05-20 15:48:51 +02:00
|
|
|
values: (() => {
|
2023-03-20 00:03:15 +01:00
|
|
|
switch (themeType) {
|
|
|
|
case "login":
|
|
|
|
return [...loginThemePageIds];
|
|
|
|
case "account":
|
|
|
|
return [...accountThemePageIds];
|
|
|
|
}
|
|
|
|
assert<Equals<typeof themeType, never>>(false);
|
|
|
|
})()
|
|
|
|
}).catch(() => {
|
|
|
|
process.exit(-1);
|
|
|
|
});
|
|
|
|
|
2024-05-19 10:23:38 +02:00
|
|
|
console.log(`→ ${pageId}`);
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const componentPageBasename = capitalize(kebabCaseToCamelCase(pageId)).replace(
|
|
|
|
/ftl$/,
|
|
|
|
"tsx"
|
|
|
|
);
|
2023-03-19 15:52:41 +01:00
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const { themeSrcDirPath } = getThemeSrcDirPath({
|
|
|
|
reactAppRootDirPath: buildOptions.reactAppRootDirPath
|
|
|
|
});
|
2023-03-25 04:56:17 +01:00
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const targetFilePath = pathJoin(
|
|
|
|
themeSrcDirPath,
|
|
|
|
themeType,
|
|
|
|
"pages",
|
|
|
|
componentPageBasename
|
|
|
|
);
|
2023-03-19 15:52:41 +01:00
|
|
|
|
2024-05-18 03:41:12 +02:00
|
|
|
if (fs.existsSync(targetFilePath)) {
|
2024-05-20 15:48:51 +02:00
|
|
|
console.log(
|
|
|
|
`${pageId} is already ejected, ${pathRelative(
|
|
|
|
process.cwd(),
|
|
|
|
targetFilePath
|
|
|
|
)} already exists`
|
|
|
|
);
|
2023-03-19 15:52:41 +01:00
|
|
|
|
|
|
|
process.exit(-1);
|
|
|
|
}
|
|
|
|
|
2024-05-18 03:41:12 +02:00
|
|
|
{
|
|
|
|
const targetDirPath = pathDirname(targetFilePath);
|
|
|
|
|
|
|
|
if (!fs.existsSync(targetDirPath)) {
|
2024-05-20 15:48:51 +02:00
|
|
|
fs.mkdirSync(targetDirPath, { recursive: true });
|
2024-05-18 03:41:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-18 04:14:36 +02:00
|
|
|
const componentPageContent = fs
|
2024-05-20 15:48:51 +02:00
|
|
|
.readFileSync(
|
|
|
|
pathJoin(
|
|
|
|
getThisCodebaseRootDirPath(),
|
|
|
|
"src",
|
|
|
|
themeType,
|
|
|
|
"pages",
|
|
|
|
componentPageBasename
|
|
|
|
)
|
|
|
|
)
|
2024-05-18 04:14:36 +02:00
|
|
|
.toString("utf8");
|
|
|
|
|
|
|
|
fs.writeFileSync(targetFilePath, Buffer.from(componentPageContent, "utf8"));
|
|
|
|
|
|
|
|
const userProfileFormFieldComponentName = "UserProfileFormFields";
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
[
|
|
|
|
``,
|
2024-05-19 10:23:38 +02:00
|
|
|
`${chalk.green("✓")} ${chalk.bold(
|
|
|
|
pathJoin(".", pathRelative(process.cwd(), targetFilePath))
|
|
|
|
)} copy pasted from the Keycloakify source code into your project`,
|
2024-05-18 04:14:36 +02:00
|
|
|
``,
|
|
|
|
`You now need to update your page router:`,
|
|
|
|
``,
|
2024-05-20 15:48:51 +02:00
|
|
|
`${chalk.bold(
|
|
|
|
pathJoin(
|
|
|
|
".",
|
|
|
|
pathRelative(process.cwd(), themeSrcDirPath),
|
|
|
|
themeType,
|
|
|
|
"KcApp.tsx"
|
|
|
|
)
|
|
|
|
)}:`,
|
2024-05-19 10:23:38 +02:00
|
|
|
chalk.grey("```"),
|
2024-05-18 04:14:36 +02:00
|
|
|
`// ...`,
|
|
|
|
``,
|
2024-05-20 15:48:51 +02:00
|
|
|
chalk.green(
|
|
|
|
`+const ${componentPageBasename.replace(
|
|
|
|
/.tsx$/,
|
|
|
|
""
|
|
|
|
)} = lazy(() => import("./pages/${componentPageBasename}"));`
|
|
|
|
),
|
2024-05-19 10:23:38 +02:00
|
|
|
...[
|
|
|
|
``,
|
|
|
|
` export default function KcApp(props: { kcContext: KcContext; }) {`,
|
|
|
|
``,
|
|
|
|
` // ...`,
|
|
|
|
``,
|
|
|
|
` return (`,
|
|
|
|
` <Suspense>`,
|
|
|
|
` {(() => {`,
|
|
|
|
` switch (kcContext.pageId) {`,
|
|
|
|
` // ...`,
|
|
|
|
`+ case "${pageId}": return (`,
|
|
|
|
`+ <Login`,
|
|
|
|
`+ {...{ kcContext, i18n, classes }}`,
|
|
|
|
`+ Template={Template}`,
|
|
|
|
...(!componentPageContent.includes(userProfileFormFieldComponentName)
|
|
|
|
? []
|
2024-05-20 15:48:51 +02:00
|
|
|
: [
|
|
|
|
`+ ${userProfileFormFieldComponentName}={${userProfileFormFieldComponentName}}`
|
|
|
|
]),
|
2024-05-19 10:23:38 +02:00
|
|
|
`+ doUseDefaultCss={true}`,
|
|
|
|
`+ />`,
|
|
|
|
`+ );`,
|
|
|
|
` default: return <Fallback /* .. */ />;`,
|
|
|
|
` }`,
|
|
|
|
` })()}`,
|
|
|
|
` </Suspense>`,
|
|
|
|
` );`,
|
|
|
|
` }`
|
|
|
|
].map(line => {
|
|
|
|
if (line.startsWith("+")) {
|
|
|
|
return chalk.green(line);
|
|
|
|
}
|
|
|
|
if (line.startsWith("-")) {
|
|
|
|
return chalk.red(line);
|
|
|
|
}
|
|
|
|
return chalk.grey(line);
|
|
|
|
}),
|
|
|
|
chalk.grey("```")
|
2024-05-18 04:14:36 +02:00
|
|
|
].join("\n")
|
|
|
|
);
|
2024-05-15 05:14:01 +02:00
|
|
|
}
|