ast.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.dev/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.parseSourceFile = parseSourceFile;
  20. exports.addModuleImportToRootModule = addModuleImportToRootModule;
  21. exports.addModuleImportToModule = addModuleImportToModule;
  22. exports.findModuleFromOptions = findModuleFromOptions;
  23. const schematics_1 = require("@angular-devkit/schematics");
  24. const change_1 = require("@schematics/angular/utility/change");
  25. const workspace_1 = require("@schematics/angular/utility/workspace");
  26. const find_module_1 = require("@schematics/angular/utility/find-module");
  27. const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
  28. const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
  29. const ts = require("typescript");
  30. const project_main_file_1 = require("./project-main-file");
  31. /** Reads file given path and returns TypeScript source file. */
  32. function parseSourceFile(host, path) {
  33. const buffer = host.read(path);
  34. if (!buffer) {
  35. throw new schematics_1.SchematicsException(`Could not find file for path: ${path}`);
  36. }
  37. return ts.createSourceFile(path, buffer.toString(), ts.ScriptTarget.Latest, true);
  38. }
  39. /** Import and add module to root app module. */
  40. function addModuleImportToRootModule(host, moduleName, src, project) {
  41. const modulePath = (0, ng_ast_utils_1.getAppModulePath)(host, (0, project_main_file_1.getProjectMainFile)(project));
  42. addModuleImportToModule(host, modulePath, moduleName, src);
  43. }
  44. /**
  45. * Import and add module to specific module path.
  46. * @param host the tree we are updating
  47. * @param modulePath src location of the module to import
  48. * @param moduleName name of module to import
  49. * @param src src location to import
  50. */
  51. function addModuleImportToModule(host, modulePath, moduleName, src) {
  52. const moduleSource = parseSourceFile(host, modulePath);
  53. if (!moduleSource) {
  54. throw new schematics_1.SchematicsException(`Module not found: ${modulePath}`);
  55. }
  56. const changes = (0, ast_utils_1.addImportToModule)(moduleSource, modulePath, moduleName, src);
  57. const recorder = host.beginUpdate(modulePath);
  58. changes.forEach(change => {
  59. if (change instanceof change_1.InsertChange) {
  60. recorder.insertLeft(change.pos, change.toAdd);
  61. }
  62. });
  63. host.commitUpdate(recorder);
  64. }
  65. /** Wraps the internal find module from options with undefined path handling */
  66. function findModuleFromOptions(host, options) {
  67. return __awaiter(this, void 0, void 0, function* () {
  68. const workspace = yield (0, workspace_1.getWorkspace)(host);
  69. if (!options.project) {
  70. options.project = Array.from(workspace.projects.keys())[0];
  71. }
  72. const project = workspace.projects.get(options.project);
  73. if (options.path === undefined) {
  74. options.path = `/${project.root}/src/app`;
  75. }
  76. return (0, find_module_1.findModuleFromOptions)(host, options);
  77. });
  78. }
  79. //# sourceMappingURL=ast.js.map