ast.js 1.5 KB

1234567891011121314151617181920212223242526272829303132
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.addIonicModuleImportToNgModule = void 0;
  4. const schematics_1 = require("@angular-devkit/schematics");
  5. const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
  6. const change_1 = require("@schematics/angular/utility/change");
  7. const ts = require("typescript");
  8. /**
  9. * Reads file given path and returns TypeScript source file.
  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. /**
  21. * Import and add module to root app module.
  22. */
  23. function addIonicModuleImportToNgModule(host, modulePath) {
  24. const recorder = host.beginUpdate(modulePath);
  25. const moduleSource = getSourceFile(host, modulePath);
  26. const ionicModuleChange = (0, ast_utils_1.insertImport)(moduleSource, modulePath, 'IonicModule', '@ionic/angular');
  27. (0, change_1.applyToUpdateRecorder)(recorder, [ionicModuleChange]);
  28. const metadataChange = (0, ast_utils_1.addSymbolToNgModuleMetadata)(moduleSource, modulePath, 'imports', 'IonicModule.forRoot({})');
  29. (0, change_1.applyToUpdateRecorder)(recorder, metadataChange);
  30. host.commitUpdate(recorder);
  31. }
  32. exports.addIonicModuleImportToNgModule = addIonicModuleImportToNgModule;