index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 config_1 = require("@schematics/angular/utility/config");
  12. const utils_1 = require("../utils");
  13. const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
  14. const project_main_file_1 = require("../utils/project-main-file");
  15. const ng_module_imports_1 = require("../utils/ng-module-imports");
  16. const bootstrapStylePath = `./node_modules/bootstrap/dist/css/bootstrap.min.css`;
  17. const datePickerStylePath = `./node_modules/ngx-bootstrap/datepicker/bs-datepicker.css`;
  18. const datepickerComponentName = 'datepicker';
  19. const bsName = 'ngx-bootstrap';
  20. const components = {
  21. accordion: { moduleName: 'AccordionModule', link: `${bsName}/accordion`, animated: true },
  22. alerts: { moduleName: 'AlertModule', link: `${bsName}/alert` },
  23. buttons: { moduleName: 'ButtonsModule', link: `${bsName}/buttons` },
  24. carousel: { moduleName: 'CarouselModule', link: `${bsName}/carousel` },
  25. collapse: { moduleName: 'CollapseModule', link: `${bsName}/collapse`, animated: true },
  26. datepicker: { moduleName: 'BsDatepickerModule', link: `${bsName}/datepicker`, animated: true },
  27. dropdowns: { moduleName: 'BsDropdownModule', link: `${bsName}/dropdown`, animated: true },
  28. modals: { moduleName: 'ModalModule', link: `${bsName}/modal` },
  29. pagination: { moduleName: 'PaginationModule', link: `${bsName}/pagination` },
  30. popover: { moduleName: 'PopoverModule', link: `${bsName}/popover` },
  31. progressbar: { moduleName: 'ProgressbarModule', link: `${bsName}/progressbar` },
  32. rating: { moduleName: 'RatingModule', link: `${bsName}/rating` },
  33. sortable: { moduleName: 'SortableModule', link: `${bsName}/sortable` },
  34. tabs: { moduleName: 'TabsModule', link: `${bsName}/tabs` },
  35. timepicker: { moduleName: 'TimepickerModule', link: `${bsName}/timepicker` },
  36. tooltip: { moduleName: 'TooltipModule', link: `${bsName}/tooltip` },
  37. typeahead: { moduleName: 'TypeaheadModule', link: `${bsName}/typeahead`, animated: true }
  38. };
  39. /* tslint:disable-next-line: no-default-export */
  40. function default_1(options) {
  41. const componentName = options.component
  42. ? options.component
  43. : options['--'] && options['--'][1];
  44. return schematics_1.chain([
  45. addPackageJsonDependencies(),
  46. utils_1.installPackageJsonDependencies(),
  47. !componentName || componentName === datepickerComponentName
  48. ? addStyles(options, insertCommonStyles)
  49. : addStyles(options, insertBootstrapStyles),
  50. componentName
  51. ? addModuleOfComponent(options.project, componentName)
  52. : schematics_1.noop(),
  53. addAnimationModule(options.project, componentName)
  54. ]);
  55. }
  56. exports.default = default_1;
  57. function addModuleOfComponent(projectName, componentName) {
  58. return (host) => {
  59. const workspace = config_1.getWorkspace(host);
  60. const project = utils_1.getProjectFromWorkspace(workspace, projectName);
  61. const appModulePath = ng_ast_utils_1.getAppModulePath(host, project_main_file_1.getProjectMainFile(project));
  62. if (componentName && components[componentName]) {
  63. if (ng_module_imports_1.hasNgModuleImport(host, appModulePath, components[componentName].moduleName)) {
  64. /* tslint:disable-next-line: no-console */
  65. return console.warn(`Could not set up ${components[componentName].moduleName} because it already imported.`);
  66. }
  67. utils_1.addModuleImportToRootModule(host, `${components[componentName].moduleName}.forRoot()`, components[componentName].link, project);
  68. }
  69. return host;
  70. };
  71. }
  72. function addPackageJsonDependencies() {
  73. return (host, context) => {
  74. const dependencies = [
  75. { name: 'bootstrap', version: '4.1.1' },
  76. { name: 'ngx-bootstrap', version: '^4.1.1' }
  77. ];
  78. dependencies.forEach(dependency => {
  79. utils_1.addPackageToPackageJson(host, dependency.name, `${dependency.version}`);
  80. context.logger.log('info', `✅️ Added "${dependency.name}`);
  81. });
  82. return host;
  83. };
  84. }
  85. function addStyles(options, insertStyle) {
  86. return function (host) {
  87. const workspace = config_1.getWorkspace(host);
  88. const project = utils_1.getProjectFromWorkspace(workspace, options.project);
  89. insertStyle(project, host, workspace);
  90. return host;
  91. };
  92. }
  93. exports.addStyles = addStyles;
  94. function insertBootstrapStyles(project, host, workspace) {
  95. utils_1.addStyleToTarget(project, 'build', host, bootstrapStylePath, workspace);
  96. utils_1.addStyleToTarget(project, 'test', host, bootstrapStylePath, workspace);
  97. }
  98. function insertCommonStyles(project, host, workspace) {
  99. utils_1.addStyleToTarget(project, 'build', host, datePickerStylePath, workspace);
  100. utils_1.addStyleToTarget(project, 'test', host, datePickerStylePath, workspace);
  101. insertBootstrapStyles(project, host, workspace);
  102. }
  103. function addAnimationModule(projectName, componentName) {
  104. return (host) => {
  105. if (!(!componentName || components[componentName].animated)) {
  106. return host;
  107. }
  108. const workspace = config_1.getWorkspace(host);
  109. const project = utils_1.getProjectFromWorkspace(workspace, projectName);
  110. utils_1.addModuleImportToRootModule(host, 'BrowserAnimationsModule', '@angular/platform-browser/animations', project);
  111. return host;
  112. };
  113. }
  114. //# sourceMappingURL=index.js.map