import * as fs from "fs"; import { join as pathJoin, relative as pathRelative } from "path"; import { crawl } from "./tools/crawl"; import { downloadAndUnzip } from "./tools/downloadAndUnzip"; import { builtinThemesUrl } from "./install-builtin-keycloak-themes"; 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, "url": builtinThemesUrl }); 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,"-")] = propertiesParser.parse( fs.readFileSync(filePath) .toString("utf8") ); }); child_process.execSync(`rm -r ${tmpDirPath}`); const targetDirPath = pathJoin(getProjectRoot(), "src", "lib", "i18n", "generated_messages"); 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 */', `export const messages= ${JSON.stringify(record[pageType], null, 2)} as const;`, '/* spell-checker: enable */' ].join("\n"), "utf8") ); console.log(`${filePath} wrote`); });