root-module.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. /**
  3. * Use of this source code is governed by an MIT-style license that can be
  4. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  5. */
  6. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  7. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  8. return new (P || (P = Promise))(function (resolve, reject) {
  9. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  10. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  11. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  12. step((generator = generator.apply(thisArg, _arguments || [])).next());
  13. });
  14. };
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.addModule = addModule;
  17. exports.addDeclaration = addDeclaration;
  18. const schematics_1 = require("@angular/cdk/schematics");
  19. const schematics_2 = require("@angular-devkit/schematics");
  20. const change_1 = require("@schematics/angular/utility/change");
  21. const find_module_1 = require("@schematics/angular/utility/find-module");
  22. const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
  23. const workspace_1 = require("@schematics/angular/utility/workspace");
  24. const ts = require("typescript");
  25. function readIntoSourceFile(host, modulePath) {
  26. const text = host.read(modulePath);
  27. if (text === null) {
  28. throw new schematics_2.SchematicsException(`File ${modulePath} does not exist.`);
  29. }
  30. const sourceText = text.toString('utf-8');
  31. return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
  32. }
  33. function addModule(moduleName, modulePath, projectName) {
  34. return (host) => __awaiter(this, void 0, void 0, function* () {
  35. const workspace = yield (0, workspace_1.getWorkspace)(host);
  36. const project = (0, schematics_1.getProjectFromWorkspace)(workspace, projectName);
  37. (0, schematics_1.addModuleImportToRootModule)(host, moduleName, modulePath, project);
  38. return (0, schematics_2.noop)();
  39. });
  40. }
  41. function addDeclaration(componentName, componentPath, projectName) {
  42. return (host) => __awaiter(this, void 0, void 0, function* () {
  43. const workspace = yield (0, workspace_1.getWorkspace)(host);
  44. const project = (0, schematics_1.getProjectFromWorkspace)(workspace, projectName);
  45. const appModulePath = (0, ng_ast_utils_1.getAppModulePath)(host, (0, schematics_1.getProjectMainFile)(project));
  46. const source = readIntoSourceFile(host, appModulePath);
  47. const relativePath = (0, find_module_1.buildRelativePath)(appModulePath, componentPath);
  48. const declarationChanges = (0, schematics_1.addDeclarationToModule)(source, appModulePath, componentName, relativePath);
  49. const declarationRecorder = host.beginUpdate(appModulePath);
  50. for (const change of declarationChanges) {
  51. if (change instanceof change_1.InsertChange) {
  52. declarationRecorder.insertLeft(change.pos, change.toAdd);
  53. }
  54. }
  55. host.commitUpdate(declarationRecorder);
  56. return (0, schematics_2.noop)();
  57. });
  58. }
  59. //# sourceMappingURL=root-module.js.map