UNPKG

angular-toaster

Version:

An Angular Toaster Notification library based on AngularJS-Toaster

47 lines (46 loc) 2.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseSourceFile = parseSourceFile; exports.addModuleImportToRootModule = addModuleImportToRootModule; exports.addModuleImportToModule = addModuleImportToModule; const tslib_1 = require("tslib"); const schematics_1 = require("@angular-devkit/schematics"); const ts = tslib_1.__importStar(require("@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript")); const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils"); const project_1 = require("./project"); const change_1 = require("@schematics/angular/utility/change"); /** Reads file given path and returns TypeScript source file. */ function parseSourceFile(host, path) { const buffer = host.read(path); if (!buffer) { throw new schematics_1.SchematicsException(`Could not find file for path: ${path}`); } return ts.createSourceFile(path, buffer.toString(), ts.ScriptTarget.Latest, true); } /** Import and add module to root app module. */ function addModuleImportToRootModule(host, moduleName, src, project) { const modulePath = (0, ng_ast_utils_1.getAppModulePath)(host, (0, project_1.getProjectMainFile)(project)); addModuleImportToModule(host, modulePath, moduleName, src); } /** * Import and add module to specific module path. * @param host the tree we are updating * @param modulePath src location of the module to import * @param moduleName name of module to import * @param src src location to import */ function addModuleImportToModule(host, modulePath, moduleName, src) { const moduleSource = parseSourceFile(host, modulePath); if (!moduleSource) { throw new schematics_1.SchematicsException(`Module not found: ${modulePath}`); } const changes = (0, ast_utils_1.addImportToModule)(moduleSource, modulePath, moduleName, src); const recorder = host.beginUpdate(modulePath); changes.forEach(change => { if (change instanceof change_1.InsertChange) { recorder.insertLeft(change.pos, change.toAdd); } }); host.commitUpdate(recorder); }