@@ -27,11 +27,11 @@ export default {
27
27
28
28
openVueFile (data ) {
29
29
if (data === undefined ) return ;
30
+ const importObj = {};
31
+ let compName = data[0 ].slice (data[0 ].lastIndexOf (' /' )+ 1 ).replace (/ [^ a-z0-9 -_. ] / gi , ' ' );
30
32
let vueFile = fs .readFileSync (data[0 ], " utf8" );
31
- const importObj = {
32
- htmlList: []
33
- };
34
-
33
+ importObj .componentName = compName;
34
+
35
35
const htmlElementMap = { // OverVue state management only handles these HTML tags.
36
36
div: [" <div>" , " </div>" ],
37
37
button: [" <button>" , " </button>" ],
@@ -50,48 +50,112 @@ export default {
50
50
let htmlString = vueFile .substring (vueFile .indexOf (' <template >' )+ 10 , vueFile .indexOf (' </template>' ));
51
51
let scriptString = vueFile .substring (vueFile .indexOf (` <script>` )+ 8 , vueFile .indexOf (` /script>` )- 1 )
52
52
htmlParser (htmlString);
53
- createHTMLList (htmlList);
53
+ importObj .props = parsingStringToProps (scriptString);
54
+ importObj .actions = parsingStringToAction (scriptString);
55
+ importObj .state = parsingStringToState (scriptString);
56
+ htmlList .pop (); htmlList .shift (); // OverVue adds a <div></div> wrapper to all components. remove this before importing.
57
+ let groupings = findGroupings (htmlList);
58
+ let groupingObj = objectGenerator (groupings);
59
+ let groupingArray = [];
60
+ for (const key in groupingObj){
61
+ groupingArray .push (groupingObj[key])
62
+ }
63
+ importObj .htmlList = groupingArray;
64
+ console .log (importObj)
54
65
66
+
55
67
function htmlParser (htmlString ){
56
68
if (! htmlString){return }
57
69
// remove new lines and tabs from HTML
58
- htmlString = htmlString .replaceAll (' \n ' , ' ' ).replaceAll (' \t ' , ' ' )
70
+ htmlString = htmlString .replaceAll (' \n ' , ' ' ).replaceAll (' \t ' , ' ' );
59
71
// find index for the first < and > in the string
60
- let openTag = htmlString .indexOf (' <' )
61
- let closeTag = htmlString .indexOf (' >' )
72
+ let openTag = htmlString .indexOf (' <' );
73
+ let closeTag = htmlString .indexOf (' >' );
62
74
63
75
// push the substring wrapped by < and > into the helper func
64
- pushElement (htmlString .substring (openTag+ 1 , closeTag))
76
+ buildList (htmlString .substring (openTag+ 1 , closeTag))
65
77
66
78
// recursively call htmlParser on the remainder of the string
67
79
return htmlParser (htmlString .substring (closeTag+ 1 ))
68
80
}
69
81
70
- function pushElement (substring ){
82
+ function buildList (substring ){
71
83
for (const elem in htmlElementMap){
72
84
for (let i = 0 ; i < htmlElementMap[elem].length ; i++ ){
73
85
// if the htmlElementMap[elem][index] matches the substring, push it into the output array
86
+ if (substring === ' p' ){
87
+ htmlList .push (htmlElementMap .paragraph [0 ])
88
+ return ;
89
+ } else if (substring === ' /p' ) {
90
+ htmlList .push (htmlElementMap .paragraph [1 ])
91
+ return ;
92
+ }
74
93
if (htmlElementMap[elem][i].indexOf (substring) !== - 1 ){
75
94
htmlList .push (htmlElementMap[elem][i])
76
- break ; // exit, or else div will match with /div, for example.
95
+ return ;
77
96
}
78
97
}
79
98
}
80
99
}
81
100
82
- function createHTMLList (htmlList ){
83
- let currentOpen = null ;
84
- for (let i = 0 ; i < htmlList .length ; i++ ){
85
- const elemText = htmlList[i].substring (1 ,htmlList[i].length - 1 );
86
-
101
+ function findGroupings (array ){
102
+ let count = 0 ; // tracks where the parent ends
103
+ let stopIndexes = []; // an array that will be used to slice out the parent/child relationships
104
+ for (let i = 0 ; i < array .length ; i++ ){
105
+ if (array[i][1 ] !== ' /' ){
106
+ if (array[i] === ' <img>' || array[i] === ' <a href="#"/>' || array[i] === ' <input />' ){
107
+ if (count === 0 ){
108
+ stopIndexes .push (i);
109
+ continue ;
110
+ }
111
+ } else {
112
+ count++ ;
113
+ }
114
+ } else {
115
+ count-- ;
116
+ }
117
+ if (count === 0 ){
118
+ stopIndexes .push (i)
119
+ }
87
120
}
121
+
122
+ let groupings = [];
123
+ let startIndex = 0 ;
124
+ for (let i = 0 ; i < stopIndexes .length ; i++ ){
125
+ groupings .push (array .slice (startIndex, stopIndexes[i]+ 1 ));
126
+ startIndex = stopIndexes[i]+ 1 ;
127
+ }
128
+
129
+ return groupings;
88
130
}
89
131
90
- console .log (importObj)
91
-
92
-
93
- }
94
- }
132
+ function objectGenerator (array ){
133
+ let groupingObj = {};
134
+ let idGen = Date .now ()
135
+ for (let i = 0 ; i < array .length ; i++ ){
136
+ for (const key in htmlElementMap){
137
+ if (array[i][0 ] === htmlElementMap[key][0 ]){
138
+ groupingObj[i] = {text: key, id: idGen}
139
+ }
140
+ }
141
+ array[i].pop ();
142
+ array[i].shift ();
143
+ if (array[i].length > 0 ){
144
+ const childGroupings = findGroupings (array[i]);
145
+ const childrenObj = objectGenerator (childGroupings)
146
+ const childrenArray = [];
147
+ for (const key in childrenObj){
148
+ childrenArray .push (childrenObj[key])
149
+ }
150
+ groupingObj[i].children = childrenArray;
151
+ } else {
152
+ groupingObj[i].children = [];
153
+ }
154
+ }
155
+ return groupingObj;
156
+ }
157
+ }
158
+ }
95
159
}
96
160
97
161
// img, link, input do NOT need closing tags -- cannot have children either
@@ -103,18 +167,80 @@ export default {
103
167
// string.slice(indexof({),indexof(:))
104
168
105
169
function parsingStringToProps (str ){
170
+ let props = [];
171
+
172
+ let split = str .split (' ' );
173
+
174
+ for (let i = 0 ; i < split .length ; i++ ){
175
+
176
+ if (split[i].includes (' :' ) && split[i] !== ' computed:' && split[i] !== ' methods:' && split[i] !== ' name:' && split[i] !== ' components:' ){
177
+
178
+ let cleanStr = split[i].slice (0 ,split[i].indexOf (' :' ))
106
179
107
- // Split the strings into an array
108
- // look for the last '{' in the string
109
- // look for the first ':' after the last '{'
110
- // save the string in between as a first prop
180
+ props .push (cleanStr)
181
+
182
+ }
111
183
112
- // if after ':' there's a ',' look for ':'
113
- // save the string in between as a second prop
114
184
115
- // if after ':' there's a ',' invoke the second function in search for the third prop, etc..
116
- // save the string in between as third prop
185
+ }
186
+ console .log (' props: ' , props)
187
+ return props
117
188
}
189
+ // First slice from map actions until
190
+ function parsingStringToAction (str ){
191
+ let action = [];
192
+
193
+ // indexOfMapActions =
194
+ let trashedSlice = str .slice (str .lastIndexOf (' ...mapActions' )+ 15 );
195
+ let slice = trashedSlice .slice (0 , trashedSlice .indexOf (' ])' ));
196
+
197
+ let split = slice .split (' ' )
198
+
199
+ for (let i = 0 ; i < split .length ; i++ ){
200
+
201
+ if (split[i].includes (' ,' )){
202
+
203
+
204
+
205
+ let cleanStr = split[i].slice (split[i].indexOf (' "' )+ 1 ,split[i].lastIndexOf (' "' ))
206
+
207
+ action .push (cleanStr)
208
+ }
209
+
210
+
211
+ }
212
+ console .log (' actions' , action)
213
+ return action
214
+ }
215
+
216
+
217
+ function parsingStringToState (str ){
218
+ let state = [];
219
+
220
+ let trashedSlice = str .slice (str .lastIndexOf (' ...mapState' )+ 15 );
221
+ let slice = trashedSlice .slice (0 , trashedSlice .indexOf (' ])' ));
222
+
223
+
224
+ let split = slice .split (' ' )
225
+
226
+ for (let i = 0 ; i < split .length ; i++ ){
227
+
228
+ if (split[i].includes (' ,' )){
229
+
230
+ let cleanStr = split[i].slice (split[i].indexOf (' "' )+ 1 ,split[i].lastIndexOf (' "' ))
231
+
232
+
233
+ state .push (cleanStr)
234
+
235
+ }
236
+
237
+
238
+ }
239
+ console .log (' state' , state)
240
+ return state
241
+ }
242
+
243
+
118
244
/*
119
245
//
120
246
data () {
@@ -143,6 +269,8 @@ export default {
143
269
methods: {
144
270
...mapActions([
145
271
"TestActionForImport",
272
+ "AnotherTestAction",
273
+ "AThirdTestAction"
146
274
]),
147
275
},
148
276
0 commit comments