1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const schematics_1 = require("@angular-devkit/schematics");
|
4 | const core_1 = require("@angular-devkit/core");
|
5 | const ts = require("typescript");
|
6 | const ast_utils_1 = require("./devkit-utils/ast-utils");
|
7 | const change_1 = require("./devkit-utils/change");
|
8 |
|
9 |
|
10 |
|
11 | function 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 | }
|
20 | exports.getSourceFile = getSourceFile;
|
21 |
|
22 |
|
23 |
|
24 | function addModuleImportToRootModule(host, projectSourceRoot, moduleName, importSrc) {
|
25 | addModuleImportToModule(host, core_1.normalize(`${projectSourceRoot}/app/app.module.ts`), moduleName, importSrc);
|
26 | }
|
27 | exports.addModuleImportToRootModule = addModuleImportToRootModule;
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function 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 | }
|
46 | exports.addModuleImportToModule = addModuleImportToModule;
|