UNPKG

ng-afelio

Version:
121 lines (119 loc) 5.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const tasks_1 = require("@angular-devkit/schematics/tasks"); const dependencies_1 = require("@schematics/angular/utility/dependencies"); const workspace_1 = require("@schematics/angular/utility/workspace"); const ts = require("typescript"); const ast_util_1 = require("../util/ast-util"); const change_1 = require("../util/change"); function installNgxTranslate() { return (host, context) => { const lib = { type: dependencies_1.NodeDependencyType.Default, name: '@ngx-translate/core', version: '^15.0.0', overwrite: false, }; const loader = { type: dependencies_1.NodeDependencyType.Default, name: '@ngx-translate/http-loader', version: '^8.0.0', overwrite: false, }; (0, dependencies_1.addPackageJsonDependency)(host, lib); (0, dependencies_1.addPackageJsonDependency)(host, loader); context.addTask(new tasks_1.NodePackageInstallTask(), []); }; } function applyModuleImports(projectAppPath, options) { return host => { if (options.appModule) { const changes = []; const modulePath = (0, core_1.join)(projectAppPath, options.appModule); const text = host.read(modulePath); if (!text) { throw new schematics_1.SchematicsException(`Module file at ${modulePath} does not exist.`); } const sourceText = text.toString('utf8'); const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true); // Add ts imports changes.push((0, ast_util_1.insertImport)(source, modulePath, 'TranslateLoader, TranslateModule', '@ngx-translate/core')); changes.push((0, ast_util_1.insertImport)(source, modulePath, 'TranslateHttpLoader', '@ngx-translate/http-loader')); changes.push((0, ast_util_1.insertImport)(source, modulePath, 'HttpClient, HttpClientModule', '@angular/common/http')); // Add ng imports const translateImport = `TranslateModule.forRoot({ loader: { provide: TranslateLoader, useFactory: (createTranslateLoader), deps: [HttpClient] }, })`; changes.push(...(0, ast_util_1.addImportToModule)(source, modulePath, 'HttpClientModule', null)); changes.push(...(0, ast_util_1.addImportToModule)(source, modulePath, translateImport, null)); // Save changes (0, change_1.applyChangesToHost)(host, modulePath, changes); } return host; }; } function addHttpLoader(projectAppPath, options) { return (host) => { if (options.appModule) { // const changes: Change[] = []; const modulePath = (0, core_1.join)(projectAppPath, options.appModule); const text = host.read(modulePath); if (!text) { throw new schematics_1.SchematicsException(`Module file at ${modulePath} does not exist.`); } const sourceText = text.toString('utf8'); const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true); const ngModuleDecorator = (0, ast_util_1.findNode)(source, ts.SyntaxKind.Decorator, '@NgModule'); if (ngModuleDecorator) { const toInsert = `export function createTranslateLoader(http: HttpClient): TranslateHttpLoader { return new TranslateHttpLoader(http, './assets/locales/', '.json'); } `; const pos = ngModuleDecorator.getStart(); const recorder = host.beginUpdate(modulePath); recorder.insertRight(pos, toInsert); host.commitUpdate(recorder); } } return host; }; } function default_1(options) { return async (host) => { if (!options.project) { throw new schematics_1.SchematicsException('Option (project) is required.'); } const workspace = await (0, workspace_1.getWorkspace)(host); const project = workspace.projects.get(options.project); let projectAppPath; if (project) { projectAppPath = (0, workspace_1.buildDefaultPath)(project); } else { throw new schematics_1.SchematicsException(`Project "${options.project}" not found.`); } const parsedPath = (0, core_1.join)(projectAppPath, '../assets'); const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [ (0, schematics_1.template)({ ...core_1.strings, ...options, }), (0, schematics_1.move)(parsedPath), ]); return (0, schematics_1.chain)([ (0, schematics_1.branchAndMerge)((0, schematics_1.chain)([ (0, schematics_1.mergeWith)(templateSource), installNgxTranslate(), applyModuleImports(projectAppPath, options), addHttpLoader(projectAppPath, options), ])), ]); }; } exports.default = default_1;