1 | import * as _ from 'lodash';
|
2 | import * as path from 'path';
|
3 |
|
4 | import { logger } from '../../utils/logger';
|
5 | import FileEngine from './file.engine';
|
6 |
|
7 | const $: any = require('cheerio');
|
8 |
|
9 | class ComponentsTreeEngine {
|
10 | private components: any[] = [];
|
11 | private componentsForTree: any[] = [];
|
12 |
|
13 | private static instance: ComponentsTreeEngine;
|
14 | private constructor() {}
|
15 | public static getInstance() {
|
16 | if (!ComponentsTreeEngine.instance) {
|
17 | ComponentsTreeEngine.instance = new ComponentsTreeEngine();
|
18 | }
|
19 | return ComponentsTreeEngine.instance;
|
20 | }
|
21 |
|
22 | public addComponent(component) {
|
23 | this.components.push(component);
|
24 | }
|
25 |
|
26 | private readTemplates() {
|
27 | return new Promise((resolve, reject) => {
|
28 | let i = 0;
|
29 | let len = this.componentsForTree.length;
|
30 | let loop = () => {
|
31 | if (i <= len - 1) {
|
32 | if (this.componentsForTree[i].templateUrl) {
|
33 | let filePath =
|
34 | process.cwd() +
|
35 | path.sep +
|
36 | path.dirname(this.componentsForTree[i].file) +
|
37 | path.sep +
|
38 | this.componentsForTree[i].templateUrl;
|
39 | FileEngine.get(filePath).then(
|
40 | templateData => {
|
41 | this.componentsForTree[i].templateData = templateData;
|
42 | i++;
|
43 | loop();
|
44 | },
|
45 | e => {
|
46 | logger.error(e);
|
47 | reject();
|
48 | }
|
49 | );
|
50 | } else {
|
51 | this.componentsForTree[i].templateData = this.componentsForTree[i].template;
|
52 | i++;
|
53 | loop();
|
54 | }
|
55 | } else {
|
56 | resolve();
|
57 | }
|
58 | };
|
59 | loop();
|
60 | });
|
61 | }
|
62 |
|
63 | private findChildrenAndParents() {
|
64 | return new Promise((resolve, reject) => {
|
65 | _.forEach(this.componentsForTree, component => {
|
66 | let $component = $(component.templateData);
|
67 | _.forEach(this.componentsForTree, componentToFind => {
|
68 | if ($component.find(componentToFind.selector).length > 0) {
|
69 | console.log(componentToFind.name + ' found in ' + component.name);
|
70 | component.children.push(componentToFind.name);
|
71 | }
|
72 | });
|
73 | });
|
74 | resolve();
|
75 | });
|
76 | }
|
77 |
|
78 | private createTreesForComponents() {
|
79 | return new Promise((resolve, reject) => {
|
80 | _.forEach(this.components, component => {
|
81 | let _component = {
|
82 | name: component.name,
|
83 | file: component.file,
|
84 | selector: component.selector,
|
85 | children: [],
|
86 | template: '',
|
87 | templateUrl: ''
|
88 | };
|
89 | if (typeof component.template !== 'undefined') {
|
90 | _component.template = component.template;
|
91 | }
|
92 | if (component.templateUrl.length > 0) {
|
93 | _component.templateUrl = component.templateUrl[0];
|
94 | }
|
95 | this.componentsForTree.push(_component);
|
96 | });
|
97 | this.readTemplates().then(
|
98 | () => {
|
99 | this.findChildrenAndParents().then(
|
100 | () => {
|
101 | console.log('this.componentsForTree: ', this.componentsForTree);
|
102 | resolve();
|
103 | },
|
104 | e => {
|
105 | logger.error(e);
|
106 | reject();
|
107 | }
|
108 | );
|
109 | },
|
110 | e => {
|
111 | logger.error(e);
|
112 | }
|
113 | );
|
114 | });
|
115 | }
|
116 | }
|
117 |
|
118 | export default ComponentsTreeEngine.getInstance();
|