1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | exports.checkComponentName = void 0;
|
13 | const tasks_1 = require("@angular-devkit/schematics/tasks");
|
14 | const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
|
15 | const utils_1 = require("../utils");
|
16 | const ng_module_imports_1 = require("../utils/ng-module-imports");
|
17 | const project_main_file_1 = require("../utils/project-main-file");
|
18 | const addStyles_1 = require("../utils/addStyles");
|
19 | const getVersions_1 = require("../utils/getVersions");
|
20 | const workspace_1 = require("@schematics/angular/utility/workspace");
|
21 | const datepickerComponentName = 'datepicker';
|
22 | const bsName = 'ngx-bootstrap';
|
23 | const BOOTSTRAP_AVAILABLE_STYLES = {
|
24 | 'css': [`./node_modules/bootstrap/dist/css/bootstrap.min.css`],
|
25 | 'scss': [`
|
26 | /* Importing Bootstrap SCSS file. */
|
27 | @import "~bootstrap/scss/bootstrap";
|
28 | `]
|
29 | };
|
30 | const DATEPICKER_AVAILABLESTYLES = {
|
31 | 'css': [`./node_modules/ngx-bootstrap/datepicker/bs-datepicker.css`],
|
32 | 'scss': [`
|
33 | /* Importing Datepicker SCSS file. */
|
34 | @import "~ngx-bootstrap/datepicker/bs-datepicker";
|
35 | `]
|
36 | };
|
37 | const components = {
|
38 | accordion: { moduleName: 'AccordionModule', link: `${bsName}/accordion`, animated: true },
|
39 | alerts: { moduleName: 'AlertModule', link: `${bsName}/alert` },
|
40 | buttons: { moduleName: 'ButtonsModule', link: `${bsName}/buttons` },
|
41 | carousel: { moduleName: 'CarouselModule', link: `${bsName}/carousel` },
|
42 | collapse: { moduleName: 'CollapseModule', link: `${bsName}/collapse`, animated: true },
|
43 | datepicker: { moduleName: 'BsDatepickerModule', link: `${bsName}/datepicker`, animated: true },
|
44 | dropdowns: { moduleName: 'BsDropdownModule', link: `${bsName}/dropdown`, animated: true },
|
45 | modals: { moduleName: 'ModalModule', link: `${bsName}/modal` },
|
46 | pagination: { moduleName: 'PaginationModule', link: `${bsName}/pagination` },
|
47 | popover: { moduleName: 'PopoverModule', link: `${bsName}/popover` },
|
48 | progressbar: { moduleName: 'ProgressbarModule', link: `${bsName}/progressbar` },
|
49 | rating: { moduleName: 'RatingModule', link: `${bsName}/rating` },
|
50 | sortable: { moduleName: 'SortableModule', link: `${bsName}/sortable` },
|
51 | tabs: { moduleName: 'TabsModule', link: `${bsName}/tabs` },
|
52 | timepicker: { moduleName: 'TimepickerModule', link: `${bsName}/timepicker` },
|
53 | tooltip: { moduleName: 'TooltipModule', link: `${bsName}/tooltip` },
|
54 | typeahead: { moduleName: 'TypeaheadModule', link: `${bsName}/typeahead`, animated: true }
|
55 | };
|
56 | function addBsToPackage(options) {
|
57 | const componentName = options.component
|
58 | ? options.component
|
59 | : options['--'] && options['--'][1];
|
60 | return (tree, context) => __awaiter(this, void 0, void 0, function* () {
|
61 | const workspace = yield workspace_1.getWorkspace(tree);
|
62 | const projectName = options.project || workspace.extensions.defaultProject.toString();
|
63 | const project = workspace.projects.get(projectName);
|
64 | addPackageJsonDependencies(tree, context);
|
65 | if (!componentName || componentName === datepickerComponentName || !components[componentName]) {
|
66 | insertCommonStyles(project, tree, projectName, options.stylesExtension);
|
67 | }
|
68 | else {
|
69 | insertBootstrapStyles(project, tree, projectName, options.stylesExtension);
|
70 | }
|
71 | context.addTask(new tasks_1.NodePackageInstallTask());
|
72 | if (componentName) {
|
73 | addModuleOfComponent(project, tree, context, componentName);
|
74 | }
|
75 | addAnimationModule(project, tree, context, componentName);
|
76 | });
|
77 | }
|
78 | exports.default = addBsToPackage;
|
79 | function addModuleOfComponent(project, host, context, componentName) {
|
80 | if (!project) {
|
81 | return;
|
82 | }
|
83 | const appModulePath = ng_ast_utils_1.getAppModulePath(host, project_main_file_1.getProjectMainFile(project));
|
84 | if (componentName && components[componentName]) {
|
85 | if (ng_module_imports_1.hasNgModuleImport(host, appModulePath, components[componentName].moduleName)) {
|
86 | context.logger.warn(`Could not set up ${components[componentName].moduleName} because it already imported.`);
|
87 | return;
|
88 | }
|
89 | utils_1.addModuleImportToRootModule(host, `${components[componentName].moduleName}.forRoot()`, components[componentName].link, project);
|
90 | }
|
91 | }
|
92 | function addPackageJsonDependencies(host, context) {
|
93 | const dependencies = getVersions_1.getDependencies(host);
|
94 | dependencies.forEach(dependency => {
|
95 | utils_1.addPackageToPackageJson(host, dependency.name, `${dependency.version}`);
|
96 | context.logger.log('info', `✅️ Added "${dependency.name}`);
|
97 | });
|
98 | return host;
|
99 | }
|
100 | function insertBootstrapStyles(project, host, projectName, extension) {
|
101 | if (!project) {
|
102 | return;
|
103 | }
|
104 | return addStyles_1.addStyles(project, 'build', host, BOOTSTRAP_AVAILABLE_STYLES, projectName, extension);
|
105 | }
|
106 | function insertCommonStyles(project, host, projectName, extension) {
|
107 | if (!project) {
|
108 | return;
|
109 | }
|
110 | insertBootstrapStyles(project, host, projectName, extension);
|
111 | return addStyles_1.addStyles(project, 'build', host, DATEPICKER_AVAILABLESTYLES, projectName, extension);
|
112 | }
|
113 | function addAnimationModule(project, host, context, componentName) {
|
114 | var _a;
|
115 | if (!project || !(!componentName || ((_a = components[componentName]) === null || _a === void 0 ? void 0 : _a.animated))) {
|
116 | return;
|
117 | }
|
118 | utils_1.addModuleImportToRootModule(host, 'BrowserAnimationsModule', '@angular/platform-browser/animations', project);
|
119 | }
|
120 | function checkComponentName(componentName) {
|
121 | return !!components[componentName];
|
122 | }
|
123 | exports.checkComponentName = checkComponentName;
|
124 |
|
\ | No newline at end of file |