2021-03-05 00:23:50 +01:00
|
|
|
import "minimal-polyfills/Object.fromEntries";
|
2021-02-28 18:40:57 +01:00
|
|
|
import * as fs from "fs";
|
2022-07-31 18:57:30 +02:00
|
|
|
import { join as pathJoin, relative as pathRelative, dirname as pathDirname } from "path";
|
2021-02-28 18:40:57 +01:00
|
|
|
import { crawl } from "./tools/crawl";
|
2021-10-07 21:00:53 +02:00
|
|
|
import { downloadBuiltinKeycloakTheme } from "./download-builtin-keycloak-theme";
|
2021-02-28 18:40:57 +01:00
|
|
|
import { getProjectRoot } from "./tools/getProjectRoot";
|
2021-10-06 17:22:52 +02:00
|
|
|
import { rm_rf, rm_r } from "./tools/rm";
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2022-07-29 23:10:35 +02:00
|
|
|
//NOTE: To run without argument when we want to generate src/i18n/generated_kcMessages files,
|
|
|
|
// update the version array for generating for newer version.
|
|
|
|
|
2021-02-28 18:40:57 +01:00
|
|
|
//@ts-ignore
|
|
|
|
const propertiesParser = require("properties-parser");
|
|
|
|
|
2022-06-17 01:42:12 +02:00
|
|
|
for (const keycloakVersion of ["11.0.3", "15.0.2", "18.0.1"]) {
|
2021-10-06 17:22:52 +02:00
|
|
|
console.log({ keycloakVersion });
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2021-10-06 17:22:52 +02:00
|
|
|
const tmpDirPath = pathJoin(getProjectRoot(), "tmp_xImOef9dOd44");
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2021-10-06 17:22:52 +02:00
|
|
|
rm_rf(tmpDirPath);
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2021-10-06 17:22:52 +02:00
|
|
|
downloadBuiltinKeycloakTheme({
|
|
|
|
keycloakVersion,
|
2022-08-20 11:44:48 +07:00
|
|
|
"destDirPath": tmpDirPath
|
2021-10-06 17:22:52 +02:00
|
|
|
});
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2021-10-06 17:22:52 +02:00
|
|
|
type Dictionary = { [idiomId: string]: string };
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2021-10-12 00:26:29 +02:00
|
|
|
const record: { [typeOfPage: string]: { [language: string]: Dictionary } } = {};
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2021-10-06 17:22:52 +02:00
|
|
|
{
|
|
|
|
const baseThemeDirPath = pathJoin(tmpDirPath, "base");
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2021-10-06 17:22:52 +02:00
|
|
|
crawl(baseThemeDirPath).forEach(filePath => {
|
2021-10-12 00:26:29 +02:00
|
|
|
const match = filePath.match(/^([^/]+)\/messages\/messages_([^.]+)\.properties$/);
|
2021-10-06 17:22:52 +02:00
|
|
|
|
|
|
|
if (match === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [, typeOfPage, language] = match;
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2021-10-12 00:26:29 +02:00
|
|
|
(record[typeOfPage] ??= {})[language.replace(/_/g, "-")] = Object.fromEntries(
|
|
|
|
Object.entries(propertiesParser.parse(fs.readFileSync(pathJoin(baseThemeDirPath, filePath)).toString("utf8"))).map(
|
2022-08-20 11:44:48 +07:00
|
|
|
([key, value]: any) => [key, value.replace(/''/g, "'")]
|
|
|
|
)
|
2021-10-12 00:26:29 +02:00
|
|
|
);
|
2021-10-06 17:22:52 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
rm_r(tmpDirPath);
|
2021-02-28 20:22:18 +01:00
|
|
|
|
2021-10-06 17:22:52 +02:00
|
|
|
Object.keys(record).forEach(pageType => {
|
2022-07-31 18:57:30 +02:00
|
|
|
const recordForPageType = record[pageType];
|
|
|
|
|
|
|
|
Object.keys(recordForPageType).forEach(language => {
|
2022-07-31 19:03:20 +02:00
|
|
|
const filePath = pathJoin(getProjectRoot(), "src", "lib", "i18n", "generated_messages", keycloakVersion, pageType, `${language}.ts`);
|
2022-07-31 18:57:30 +02:00
|
|
|
|
|
|
|
fs.mkdirSync(pathDirname(filePath), { "recursive": true });
|
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
filePath,
|
|
|
|
Buffer.from(
|
|
|
|
[
|
|
|
|
`//This code was automatically generated by running ${pathRelative(getProjectRoot(), __filename)}`,
|
|
|
|
"//PLEASE DO NOT EDIT MANUALLY",
|
|
|
|
"",
|
|
|
|
"/* spell-checker: disable */",
|
|
|
|
`const messages= ${JSON.stringify(recordForPageType[language], null, 2)};`,
|
|
|
|
"",
|
|
|
|
"export default messages;",
|
2022-08-20 11:44:48 +07:00
|
|
|
"/* spell-checker: enable */"
|
2022-07-31 18:57:30 +02:00
|
|
|
].join("\n"),
|
2022-08-20 11:44:48 +07:00
|
|
|
"utf8"
|
|
|
|
)
|
2022-07-31 18:57:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
console.log(`${filePath} wrote`);
|
|
|
|
});
|
2021-10-06 17:22:52 +02:00
|
|
|
});
|
|
|
|
}
|