UNPKG

1.87 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const schematics_1 = require("@angular-devkit/schematics");
4const core_1 = require("@angular-devkit/core");
5const ts = require("typescript");
6const ast_utils_1 = require("./devkit-utils/ast-utils");
7const change_1 = require("./devkit-utils/change");
8/**
9 * Reads file given path and returns TypeScript source file.
10 */
11function getSourceFile(host, path) {
12 const buffer = host.read(path);
13 if (!buffer) {
14 throw new schematics_1.SchematicsException(`Could not find file for path: ${path}`);
15 }
16 const content = buffer.toString();
17 const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);
18 return source;
19}
20exports.getSourceFile = getSourceFile;
21/**
22 * Import and add module to root app module.
23 */
24function addModuleImportToRootModule(host, projectSourceRoot, moduleName, importSrc) {
25 addModuleImportToModule(host, core_1.normalize(`${projectSourceRoot}/app/app.module.ts`), moduleName, importSrc);
26}
27exports.addModuleImportToRootModule = addModuleImportToRootModule;
28/**
29 * Import and add module to specific module path.
30 * @param host the tree we are updating
31 * @param modulePath src location of the module to import
32 * @param moduleName name of module to import
33 * @param src src location to import
34 */
35function addModuleImportToModule(host, modulePath, moduleName, src) {
36 const moduleSource = getSourceFile(host, modulePath);
37 const changes = ast_utils_1.addImportToModule(moduleSource, modulePath, moduleName, src);
38 const recorder = host.beginUpdate(modulePath);
39 changes.forEach(change => {
40 if (change instanceof change_1.InsertChange) {
41 recorder.insertLeft(change.pos, change.toAdd);
42 }
43 });
44 host.commitUpdate(recorder);
45}
46exports.addModuleImportToModule = addModuleImportToModule;