keycloak_theme/src/bin/generate-i18n-messages.ts

77 lines
2.2 KiB
TypeScript
Raw Normal View History

import "minimal-polyfills/Object.fromEntries";
2021-02-28 18:40:57 +01:00
import * as fs from "fs";
import { join as pathJoin, relative as pathRelative } from "path";
import { crawl } from "./tools/crawl";
import { downloadAndUnzip } from "./tools/downloadAndUnzip";
2021-03-04 21:43:36 +01:00
import { builtinThemesUrl } from "./install-builtin-keycloak-themes";
2021-02-28 18:40:57 +01:00
import { getProjectRoot } from "./tools/getProjectRoot";
import * as child_process from "child_process";
//@ts-ignore
const propertiesParser = require("properties-parser");
const tmpDirPath = pathJoin(getProjectRoot(), "tmp_xImOef9dOd44");
child_process.execSync(`rm -rf ${tmpDirPath}`);
downloadAndUnzip({
"destDirPath": tmpDirPath,
2021-03-04 21:43:36 +01:00
"url": builtinThemesUrl
2021-02-28 18:40:57 +01:00
});
type Dictionary = { [idiomId: string]: string };
const record: { [typeOfPage: string]: { [language: string]: Dictionary } } = {};
process.chdir(pathJoin(tmpDirPath, "base"));
crawl(".").forEach(filePath => {
const match = filePath.match(/^([^/]+)\/messages\/messages_([^.]+)\.properties$/);
if (match === null) {
return;
}
const [, typeOfPage, language] = match;
(record[typeOfPage] ??= {})[language.replace(/_/g, "-")] =
Object.fromEntries(
Object.entries(
propertiesParser.parse(
fs.readFileSync(filePath)
.toString("utf8")
)
).map(([key, value]: any) => [key, value.replace(/''/g, "'")])
2021-02-28 18:40:57 +01:00
);
});
child_process.execSync(`rm -r ${tmpDirPath}`);
2021-04-08 17:06:09 +02:00
const targetDirPath = pathJoin(getProjectRoot(), "src", "lib", "i18n", "generated_kcMessages");
2021-02-28 20:22:18 +01:00
fs.mkdirSync(targetDirPath, { "recursive": true });
Object.keys(record).forEach(pageType => {
const filePath = pathJoin(targetDirPath, `${pageType}.ts`);
fs.writeFileSync(
filePath,
Buffer.from(
[
`//This code was automatically generated by running ${pathRelative(getProjectRoot(), __filename)}`,
'//PLEASE DO NOT EDIT MANUALLY',
'',
'/* spell-checker: disable */',
2021-04-08 17:06:09 +02:00
`export const kcMessages= ${JSON.stringify(record[pageType], null, 2)};`,
2021-02-28 20:22:18 +01:00
'/* spell-checker: enable */'
].join("\n"), "utf8")
);
console.log(`${filePath} wrote`);
});