Enable to eject Template.tsx and UserProfileFormFields.tsx

This commit is contained in:
Joseph Garrone 2024-06-06 06:12:05 +02:00
parent 58892cbb56
commit 108c281b0c

View File

@ -39,13 +39,26 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
console.log(chalk.cyan("Select the page you want to customize:")); console.log(chalk.cyan("Select the page you want to customize:"));
const { value: pageId } = await cliSelect<LoginThemePageId | AccountThemePageId>({ const templateValue = "Template.tsx (Layout common to every page)";
const userProfileFormFieldsValue =
"UserProfileFormFields.tsx (Renders the form of the register.ftl, login-update-profile.ftl, update-email.ftl and idp-review-user-profile.ftl)";
const { value: pageIdOrComponent } = await cliSelect<
| LoginThemePageId
| AccountThemePageId
| typeof templateValue
| typeof userProfileFormFieldsValue
>({
values: (() => { values: (() => {
switch (themeType) { switch (themeType) {
case "login": case "login":
return [...loginThemePageIds]; return [
templateValue,
userProfileFormFieldsValue,
...loginThemePageIds
];
case "account": case "account":
return [...accountThemePageIds]; return [templateValue, ...accountThemePageIds];
} }
assert<Equals<typeof themeType, never>>(false); assert<Equals<typeof themeType, never>>(false);
})() })()
@ -53,27 +66,45 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
process.exit(-1); process.exit(-1);
}); });
console.log(`${pageId}`); console.log(`${pageIdOrComponent}`);
const componentPageBasename = capitalize(kebabCaseToCamelCase(pageId)).replace(
/ftl$/,
"tsx"
);
const { themeSrcDirPath } = getThemeSrcDirPath({ const { themeSrcDirPath } = getThemeSrcDirPath({
reactAppRootDirPath: buildOptions.reactAppRootDirPath reactAppRootDirPath: buildOptions.reactAppRootDirPath
}); });
const componentBasename = (() => {
if (pageIdOrComponent === templateValue) {
return "Template.tsx";
}
if (pageIdOrComponent === userProfileFormFieldsValue) {
return "UserProfileFormFields.tsx";
}
return capitalize(kebabCaseToCamelCase(pageIdOrComponent)).replace(/ftl$/, "tsx");
})();
const pagesOrDot = (() => {
if (
pageIdOrComponent === templateValue ||
pageIdOrComponent === userProfileFormFieldsValue
) {
return ".";
}
return "pages";
})();
const targetFilePath = pathJoin( const targetFilePath = pathJoin(
themeSrcDirPath, themeSrcDirPath,
themeType, themeType,
"pages", pagesOrDot,
componentPageBasename componentBasename
); );
if (fs.existsSync(targetFilePath)) { if (fs.existsSync(targetFilePath)) {
console.log( console.log(
`${pageId} is already ejected, ${pathRelative( `${pageIdOrComponent} is already ejected, ${pathRelative(
process.cwd(), process.cwd(),
targetFilePath targetFilePath
)} already exists` )} already exists`
@ -90,28 +121,78 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
} }
} }
const componentPageContent = fs const componentCode = fs
.readFileSync( .readFileSync(
pathJoin( pathJoin(
getThisCodebaseRootDirPath(), getThisCodebaseRootDirPath(),
"src", "src",
themeType, themeType,
"pages", pagesOrDot,
componentPageBasename componentBasename
) )
) )
.toString("utf8"); .toString("utf8");
fs.writeFileSync(targetFilePath, Buffer.from(componentPageContent, "utf8")); fs.writeFileSync(targetFilePath, Buffer.from(componentCode, "utf8"));
console.log(
`${chalk.green("✓")} ${chalk.bold(
pathJoin(".", pathRelative(process.cwd(), targetFilePath))
)} copy pasted from the Keycloakify source code into your project`
);
edit_KcApp: {
if (
pageIdOrComponent !== templateValue &&
pageIdOrComponent !== userProfileFormFieldsValue
) {
break edit_KcApp;
}
const kcAppTsxPath = pathJoin(themeSrcDirPath, themeType, "KcApp.tsx");
const kcAppTsxCode = fs.readFileSync(kcAppTsxPath).toString("utf8");
const modifiedKcAppTsxCode = (() => {
switch (pageIdOrComponent) {
case templateValue:
return kcAppTsxCode.replace(
`keycloakify/${themeType}/Template`,
"./Template"
);
case userProfileFormFieldsValue:
return kcAppTsxCode.replace(
`keycloakify/login/UserProfileFormFields`,
"./UserProfileFormFields"
);
}
assert<Equals<typeof pageIdOrComponent, never>>(false);
})();
if (kcAppTsxCode === modifiedKcAppTsxCode) {
console.log(
chalk.red(
"Unable to automatically update KcApp.tsx, please update it manually"
)
);
return;
}
fs.writeFileSync(kcAppTsxPath, Buffer.from(modifiedKcAppTsxCode, "utf8"));
console.log(
`${chalk.green("✓")} ${chalk.bold(
pathJoin(".", pathRelative(process.cwd(), kcAppTsxPath))
)} Updated`
);
return;
}
const userProfileFormFieldComponentName = "UserProfileFormFields"; const userProfileFormFieldComponentName = "UserProfileFormFields";
console.log( console.log(
[ [
``,
`${chalk.green("✓")} ${chalk.bold(
pathJoin(".", pathRelative(process.cwd(), targetFilePath))
)} copy pasted from the Keycloakify source code into your project`,
``, ``,
`You now need to update your page router:`, `You now need to update your page router:`,
``, ``,
@ -127,10 +208,10 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
`// ...`, `// ...`,
``, ``,
chalk.green( chalk.green(
`+const ${componentPageBasename.replace( `+const ${componentBasename.replace(
/.tsx$/, /.tsx$/,
"" ""
)} = lazy(() => import("./pages/${componentPageBasename}"));` )} = lazy(() => import("./pages/${componentBasename}"));`
), ),
...[ ...[
``, ``,
@ -143,11 +224,11 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
` {(() => {`, ` {(() => {`,
` switch (kcContext.pageId) {`, ` switch (kcContext.pageId) {`,
` // ...`, ` // ...`,
`+ case "${pageId}": return (`, `+ case "${pageIdOrComponent}": return (`,
`+ <Login`, `+ <Login`,
`+ {...{ kcContext, i18n, classes }}`, `+ {...{ kcContext, i18n, classes }}`,
`+ Template={Template}`, `+ Template={Template}`,
...(!componentPageContent.includes(userProfileFormFieldComponentName) ...(!componentCode.includes(userProfileFormFieldComponentName)
? [] ? []
: [ : [
`+ ${userProfileFormFieldComponentName}={${userProfileFormFieldComponentName}}` `+ ${userProfileFormFieldComponentName}={${userProfileFormFieldComponentName}}`