UNPKG

rucken

Version:

Console tools and scripts for nx and not only that I (EndyKaufman) use to automate the workflow and speed up the development process

240 lines 12.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Extracti18nService = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const child_process_1 = require("child_process"); const fs_1 = require("fs"); const log4js_1 = require("log4js"); const path_1 = require("path"); const utils_service_1 = require("../utils/utils.service"); let Extracti18nService = class Extracti18nService { utilsService; static title = 'extract-i18n'; logger; constructor(utilsService) { this.utilsService = utilsService; } setLogger(command) { this.logger = (0, log4js_1.getLogger)(command); this.logger.level = utils_service_1.UtilsService.logLevel(); } extract({ locales, markers, resetUnusedTranslates, noExtract, serverProjectNameParts, clientProjectNameParts, e2eProjectNameParts, }) { this.logger.info('Start create translate files...'); this.logger.debug(`Config: ${JSON.stringify({ locales, markers, resetUnusedTranslates, noExtract, serverProjectNameParts, clientProjectNameParts, e2eProjectNameParts, })}`); try { const projects = this.utilsService.getWorkspaceProjects(); this.createTtranslocoConfigJS(); this.logger.info('Process applications...'); Object.keys(projects) .filter((projectName) => projects[projectName].projectType === 'application' || projects[projectName].sourceRoot?.substring(0, 5) === 'apps/') .forEach((projectName) => { this.logger.debug(projectName, projects[projectName].sourceRoot); this.processApplication(projects[projectName].sourceRoot, resetUnusedTranslates || false, noExtract || false); }); this.logger.info('Process libraries...'); Object.keys(projects) .filter((projectName) => projects[projectName].projectType === 'library' || projects[projectName].sourceRoot?.substring(0, 5) === 'libs/') .forEach((projectName) => { this.logger.debug(projectName, projects[projectName].sourceRoot); this.processLibrary(projectName, projects[projectName].sourceRoot, projects[projectName].root, markers, resetUnusedTranslates || false, noExtract || false); }); this.collectServerToTranslocoConfig({ projects, locales, clientProjectNameParts, e2eProjectNameParts, }); this.collectClientToTranslocoConfig({ projects, locales, serverProjectNameParts, e2eProjectNameParts, }); this.logger.info('End of create translate files...'); } catch (error) { this.logger.error(error); process.exit(1); } } createTtranslocoConfigJS() { const translocoConfigJsFilepath = this.utilsService.resolveFilePath(utils_service_1.TRANSLOCO_CONFIG_JS); if (!(0, fs_1.existsSync)(translocoConfigJsFilepath)) { this.logger.info(`Process create ${utils_service_1.TRANSLOCO_CONFIG_JS}...`); (0, fs_1.writeFileSync)(translocoConfigJsFilepath, [ `const { readFileSync, existsSync } = require('fs');`, `module.exports = existsSync('${utils_service_1.TRANSLOCO_CONFIG_JSON}') ? JSON.parse(readFileSync('${utils_service_1.TRANSLOCO_CONFIG_JSON}').toString()) : {};`, ].join('\n')); } } collectClientToTranslocoConfig({ projects, locales, serverProjectNameParts, e2eProjectNameParts, }) { const translocoConfigFilepath = this.utilsService.resolveFilePath(utils_service_1.TRANSLOCO_CONFIG_JSON); const scopedLibs = Object.keys(projects) .filter((projectName) => (projects[projectName].projectType === 'library' || projects[projectName].sourceRoot?.substring(0, 5) === 'libs/') && !serverProjectNameParts.find((serverProjectNamePart) => projectName.includes(serverProjectNamePart))) .map((projectName) => projects[projectName].root); const applications = Object.keys(projects) .filter((projectName) => (projects[projectName].projectType === 'application' || projects[projectName].sourceRoot?.substring(0, 5) === 'apps/') && !serverProjectNameParts.find((serverProjectNamePart) => projectName.includes(serverProjectNamePart)) && !e2eProjectNameParts.find((e2eProjectNamePart) => projectName.includes(e2eProjectNamePart))) .map((projectName) => projects[projectName].sourceRoot); applications.forEach((sourceRoot) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any let translocoConfig = {}; if ((0, fs_1.existsSync)(translocoConfigFilepath)) { translocoConfig = JSON.parse((0, fs_1.readFileSync)(translocoConfigFilepath).toString()); } translocoConfig.clientRootTranslationsPath = `./${sourceRoot}/assets/i18n/`; translocoConfig.langs = locales; translocoConfig.clientScopedLibs = scopedLibs; translocoConfig.rootTranslationsPath = `./${sourceRoot}/assets/i18n/`; translocoConfig.scopedLibs = scopedLibs; if (!(0, fs_1.existsSync)((0, path_1.dirname)(translocoConfigFilepath))) { (0, fs_1.mkdirSync)((0, path_1.dirname)(translocoConfigFilepath), { recursive: true }); } (0, fs_1.writeFileSync)(translocoConfigFilepath, JSON.stringify(translocoConfig, null, 4)); const result1 = (0, child_process_1.spawnSync)('transloco-keys-manager', [], { stdio: 'inherit' }); if (result1.status !== 0) { this.logger.error(`transloco-keys-manager failed with status ${result1.status}`); } const result2 = (0, child_process_1.spawnSync)('transloco-scoped-libs', [ '--skip-gitignore' ], { stdio: 'inherit' }); if (result2.status !== 0) { this.logger.error(`transloco-scoped-libs failed with status ${result2.status}`); } }); } collectServerToTranslocoConfig({ projects, locales, clientProjectNameParts, e2eProjectNameParts, }) { const translocoConfigFilepath = this.utilsService.resolveFilePath(utils_service_1.TRANSLOCO_CONFIG_JSON); const scopedLibs = Object.keys(projects) .filter((projectName) => (projects[projectName].projectType === 'library' || projects[projectName].sourceRoot?.substring(0, 5) === 'libs/') && !clientProjectNameParts.find((clientProjectNamePart) => projectName.includes(clientProjectNamePart))) .map((projectName) => projects[projectName].root); const applications = Object.keys(projects) .filter((projectName) => (projects[projectName].projectType === 'application' || projects[projectName].sourceRoot?.substring(0, 5) === 'apps/') && !clientProjectNameParts.find((clientProjectNamePart) => projectName.includes(clientProjectNamePart)) && !e2eProjectNameParts.find((e2eProjectNamePart) => projectName.includes(e2eProjectNamePart))) .map((projectName) => projects[projectName].sourceRoot); applications.forEach((sourceRoot) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const translocoConfig = this.loadConfig(translocoConfigFilepath); translocoConfig.serverRootTranslationsPath = `./${sourceRoot}/assets/i18n/`; translocoConfig.langs = locales; translocoConfig.serverScopedLibs = scopedLibs; translocoConfig.rootTranslationsPath = `./${sourceRoot}/assets/i18n/`; translocoConfig.scopedLibs = scopedLibs; this.saveConfig(translocoConfigFilepath, translocoConfig); const result1 = (0, child_process_1.spawnSync)('transloco-keys-manager', [], { stdio: 'inherit' }); if (result1.status !== 0) { this.logger.error(`transloco-keys-manager failed with status ${result1.status}`); } const result2 = (0, child_process_1.spawnSync)('transloco-scoped-libs', [ '--skip-gitignore' ], { stdio: 'inherit' }); if (result2.status !== 0) { this.logger.error(`transloco-scoped-libs failed with status ${result2.status}`); } }); } // eslint-disable-next-line @typescript-eslint/no-explicit-any saveConfig(translocoConfigFilepath, translocoConfig) { if (!(0, fs_1.existsSync)((0, path_1.dirname)(translocoConfigFilepath))) { (0, fs_1.mkdirSync)((0, path_1.dirname)(translocoConfigFilepath), { recursive: true }); } (0, fs_1.writeFileSync)(translocoConfigFilepath, JSON.stringify(translocoConfig, null, 4)); } loadConfig(translocoConfigFilepath) { if ((0, fs_1.existsSync)(translocoConfigFilepath)) { return JSON.parse((0, fs_1.readFileSync)(translocoConfigFilepath).toString()); } return {}; } processApplication(sourceRoot, resetUnusedTranslates, noExtract) { if (!noExtract) { const result = (0, child_process_1.spawnSync)('transloco-keys-manager', [ 'extract', ...(resetUnusedTranslates ? ['--replace'] : []), '--input', `${(0, path_1.resolve)(sourceRoot)}`, '--output', `${(0, path_1.resolve)(sourceRoot)}/assets/i18n`, `--marker`, 'marker', '--file-format', 'json' ], { stdio: 'inherit' }); if (result.status !== 0) { this.logger.error(`transloco-keys-manager failed with status ${result.status}`); } } } processLibrary(projectName, sourceRoot, root, markers, resetUnusedTranslates, noExtract) { if (!noExtract) { const result = (0, child_process_1.spawnSync)('transloco-keys-manager', [ 'extract', ...(resetUnusedTranslates ? ['--replace'] : []), '--input', `${(0, path_1.resolve)(sourceRoot)}`, '--output', `${(0, path_1.resolve)(sourceRoot)}/i18n`, `--marker`, 'marker', '--file-format', 'json' ], { stdio: 'inherit' }); if (result.status !== 0) { this.logger.error(`transloco-keys-manager failed with status ${result.status}`); } } const packageJsonFilePath = this.utilsService.resolveFilePath(utils_service_1.PACKAGE_JSON, root); try { const packageJson = (0, fs_1.existsSync)(packageJsonFilePath) ? JSON.parse((0, fs_1.readFileSync)(packageJsonFilePath).toString()) : {}; packageJson.i18n = [{ scope: projectName, path: 'src/i18n', strategy: 'join', }]; markers.forEach((marker) => { if ((0, fs_1.existsSync)((0, path_1.resolve)((0, path_1.dirname)(packageJsonFilePath), `src/i18n/${marker}`))) { packageJson.i18n.push({ scope: `${projectName}-${marker}`, path: `src/i18n/${marker}`, strategy: 'join', }); } }); if (!(0, fs_1.existsSync)((0, path_1.dirname)(packageJsonFilePath))) { (0, fs_1.mkdirSync)((0, path_1.dirname)(packageJsonFilePath), { recursive: true }); } (0, fs_1.writeFileSync)(packageJsonFilePath, JSON.stringify(packageJson, null, 4)); } catch (err) { this.logger.error(`Error in file: ${packageJsonFilePath}`); this.logger.error(err); } } }; exports.Extracti18nService = Extracti18nService; exports.Extracti18nService = Extracti18nService = tslib_1.__decorate([ (0, common_1.Injectable)(), tslib_1.__metadata("design:paramtypes", [utils_service_1.UtilsService]) ], Extracti18nService); //# sourceMappingURL=extract-i18n.service.js.map