ast.js 3.0 KB

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