UNPKG

@ngneat/transloco

Version:

The internationalization (i18n) library for Angular

89 lines (88 loc) 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const schematics_1 = require("@angular-devkit/schematics"); const config_1 = require("@angular/cli/utilities/config"); const child_process_1 = require("child_process"); const package_1 = require("../utils/package"); const projects_1 = require("../utils/projects"); const transloco_1 = require("../utils/transloco"); function installKeysManager() { console.log('Installing packages for tooling...'); if (config_1.getConfiguredPackageManager() === 'yarn') { child_process_1.execSync('yarn add --dev @ngneat/transloco-keys-manager ngx-build-plus'); } else { child_process_1.execSync('npm install --save-dev @ngneat/transloco-keys-manager ngx-build-plus'); } } function updateAngularJson(host, options) { const angularJson = projects_1.getWorkspace(host); if (angularJson) { const project = angularJson.projects[options.project || angularJson.defaultProject]; project.architect.serve.builder = 'ngx-build-plus:dev-server'; } projects_1.setWorkspace(host, angularJson); } exports.updateAngularJson = updateAngularJson; function createWebpackConfig(host) { const webpackConfig = `const { TranslocoExtractKeysWebpackPlugin } = require('@ngneat/transloco-keys-manager'); module.exports = { plugins: [new TranslocoExtractKeysWebpackPlugin()] }; `; host.create('webpack-dev.config.js', webpackConfig); } exports.createWebpackConfig = createWebpackConfig; function addKeysDetectiveScript(host, strategy) { if (strategy === 'Both') { package_1.addScriptToPackageJson(host, 'start', 'ng serve --extra-webpack-config webpack-dev.config.js'); package_1.addScriptToPackageJson(host, 'i18n:extract', 'transloco-keys-manager extract'); } if (strategy === 'CLI') { package_1.addScriptToPackageJson(host, 'i18n:extract', 'transloco-keys-manager extract'); } if (strategy === 'Webpack Plugin') { package_1.addScriptToPackageJson(host, 'start', 'ng serve --extra-webpack-config webpack-dev.config.js'); } package_1.addScriptToPackageJson(host, 'i18n:find', 'transloco-keys-manager find'); } function updateTranslocoConfig(host, options) { const config = transloco_1.getConfig() || {}; let shouldUpdate = false; if (!config.rootTranslationsPath) { if (!options.translationPath) { throw new schematics_1.SchematicsException('Please provide the translation root path by using the --translation-path flag'); } config.rootTranslationsPath = options.translationPath; shouldUpdate = true; } if (!config.langs) { if (!options.langs) { throw new schematics_1.SchematicsException('Please provide the available languages either by using --langs or through the "langs" property in transloco.config.js file'); } config.langs = options.langs.split(',').map(l => l.trim()); shouldUpdate = true; } if (!config.keysManager) { config.keysManager = {}; shouldUpdate = true; } if (shouldUpdate) { transloco_1.updateConfig(host, config); } } function default_1(options) { return (host) => { // first install dependencies via command line to get the latest versions. installKeysManager(); updateTranslocoConfig(host, options); if (['Webpack Plugin', 'Both'].includes(options.strategy)) { createWebpackConfig(host); updateAngularJson(host, options); } addKeysDetectiveScript(host, options.strategy); return host; }; } exports.default = default_1; //# sourceMappingURL=index.js.map