1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
---|
10 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
---|
11 | return new (P || (P = Promise))(function (resolve, reject) {
|
---|
12 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
---|
13 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
---|
14 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
---|
15 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
---|
16 | });
|
---|
17 | };
|
---|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
19 | exports.findModuleFromOptions = exports.addModuleImportToModule = exports.addModuleImportToRootModule = exports.parseSourceFile = void 0;
|
---|
20 | const schematics_1 = require("@angular-devkit/schematics");
|
---|
21 | const change_1 = require("@schematics/angular/utility/change");
|
---|
22 | const workspace_1 = require("@schematics/angular/utility/workspace");
|
---|
23 | const find_module_1 = require("@schematics/angular/utility/find-module");
|
---|
24 | const ts = require("typescript");
|
---|
25 | const project_main_file_1 = require("./project-main-file");
|
---|
26 | const vendored_ast_utils_1 = require("./vendored-ast-utils");
|
---|
27 | /** Reads file given path and returns TypeScript source file. */
|
---|
28 | function parseSourceFile(host, path) {
|
---|
29 | const buffer = host.read(path);
|
---|
30 | if (!buffer) {
|
---|
31 | throw new schematics_1.SchematicsException(`Could not find file for path: ${path}`);
|
---|
32 | }
|
---|
33 | return ts.createSourceFile(path, buffer.toString(), ts.ScriptTarget.Latest, true);
|
---|
34 | }
|
---|
35 | exports.parseSourceFile = parseSourceFile;
|
---|
36 | /** Import and add module to root app module. */
|
---|
37 | function addModuleImportToRootModule(host, moduleName, src, project) {
|
---|
38 | const modulePath = vendored_ast_utils_1.getAppModulePath(host, project_main_file_1.getProjectMainFile(project));
|
---|
39 | addModuleImportToModule(host, modulePath, moduleName, src);
|
---|
40 | }
|
---|
41 | exports.addModuleImportToRootModule = addModuleImportToRootModule;
|
---|
42 | /**
|
---|
43 | * Import and add module to specific module path.
|
---|
44 | * @param host the tree we are updating
|
---|
45 | * @param modulePath src location of the module to import
|
---|
46 | * @param moduleName name of module to import
|
---|
47 | * @param src src location to import
|
---|
48 | */
|
---|
49 | function addModuleImportToModule(host, modulePath, moduleName, src) {
|
---|
50 | const moduleSource = parseSourceFile(host, modulePath);
|
---|
51 | if (!moduleSource) {
|
---|
52 | throw new schematics_1.SchematicsException(`Module not found: ${modulePath}`);
|
---|
53 | }
|
---|
54 | const changes = vendored_ast_utils_1.addImportToModule(moduleSource, modulePath, moduleName, src);
|
---|
55 | const recorder = host.beginUpdate(modulePath);
|
---|
56 | changes.forEach(change => {
|
---|
57 | if (change instanceof change_1.InsertChange) {
|
---|
58 | recorder.insertLeft(change.pos, change.toAdd);
|
---|
59 | }
|
---|
60 | });
|
---|
61 | host.commitUpdate(recorder);
|
---|
62 | }
|
---|
63 | exports.addModuleImportToModule = addModuleImportToModule;
|
---|
64 | /** Wraps the internal find module from options with undefined path handling */
|
---|
65 | function findModuleFromOptions(host, options) {
|
---|
66 | return __awaiter(this, void 0, void 0, function* () {
|
---|
67 | const workspace = yield workspace_1.getWorkspace(host);
|
---|
68 | if (!options.project) {
|
---|
69 | options.project = Array.from(workspace.projects.keys())[0];
|
---|
70 | }
|
---|
71 | const project = workspace.projects.get(options.project);
|
---|
72 | if (options.path === undefined) {
|
---|
73 | options.path = `/${project.root}/src/app`;
|
---|
74 | }
|
---|
75 | return find_module_1.findModuleFromOptions(host, options);
|
---|
76 | });
|
---|
77 | }
|
---|
78 | exports.findModuleFromOptions = findModuleFromOptions;
|
---|
79 | //# sourceMappingURL=data:application/json;base64,eyJ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