1
1
<template >
2
- <q-btn
3
- id =" import-component-btn"
4
- size =" md"
5
- color =" secondary"
6
- :label =title
7
- @click =" importComponent"
8
- />
2
+ <q-btn id =" import-component-btn" size =" md" color =" secondary" :label =title @click =" importComponent" />
9
3
</template >
10
4
11
5
@@ -28,7 +22,7 @@ export default {
28
22
},
29
23
methods: {
30
24
// emitter to send the importedObj to CreateComponent when fully parsed.
31
- createImport (importObj ){
25
+ createImport (importObj ) {
32
26
useCreateComponent .bind (this )(importObj) // this is where we will want to invoke the composable
33
27
},
34
28
... mapActions ([
@@ -50,49 +44,52 @@ export default {
50
44
},
51
45
],
52
46
})
53
- .then ((res ) => this .openVueFile (res .filePaths ))
47
+ .then ((res ) => {
48
+ this .openVueFile (res .filePaths )
49
+ alert (' Successfully Imported' )
50
+ })
54
51
.catch ((err ) => console .log (err));
55
52
},
56
-
53
+
57
54
// parses script tag string for props
58
- parsingStringToProps (str ){
55
+ parsingStringToProps (str ) {
59
56
let props = [];
60
57
let split = str .split (' ' );
61
- for (let i = 0 ; i < split .length ; i++ ){
62
- if (split[i].includes (' :' ) && split[i] !== ' computed:' && split[i] !== ' methods:' && split[i] !== ' name:' && split[i] !== ' components:' ){
63
- let cleanStr = split[i].slice (0 ,split[i].indexOf (' :' ))
58
+ for (let i = 0 ; i < split .length ; i++ ) {
59
+ if (split[i].includes (' :' ) && split[i] !== ' computed:' && split[i] !== ' methods:' && split[i] !== ' name:' && split[i] !== ' components:' ) {
60
+ let cleanStr = split[i].slice (0 , split[i].indexOf (' :' ))
64
61
props .push (cleanStr)
65
62
}
66
63
}
67
64
return props
68
65
},
69
66
70
67
// parses script tag string for actions
71
- parsingStringToAction (str ){
68
+ parsingStringToAction (str ) {
72
69
let action = [];
73
- if (str .indexOf (' ...mapActions' ) === - 1 ){ return action};
74
- let trashedSlice = str .slice (str .lastIndexOf (' ...mapActions' )+ 15 );
70
+ if (str .indexOf (' ...mapActions' ) === - 1 ) { return action };
71
+ let trashedSlice = str .slice (str .lastIndexOf (' ...mapActions' ) + 15 );
75
72
let slice = trashedSlice .slice (0 , trashedSlice .indexOf (' ])' ));
76
73
let split = slice .split (' ' )
77
- for (let i = 0 ; i < split .length ; i++ ){
78
- if (split[i].includes (' ,' )){
79
- let cleanStr = split[i].slice (split[i].indexOf (' "' )+ 1 , split[i].lastIndexOf (' "' ))
74
+ for (let i = 0 ; i < split .length ; i++ ) {
75
+ if (split[i].includes (' ,' )) {
76
+ let cleanStr = split[i].slice (split[i].indexOf (' "' ) + 1 , split[i].lastIndexOf (' "' ))
80
77
action .push (cleanStr)
81
78
}
82
79
}
83
80
return action;
84
81
},
85
82
86
83
// parses script tag string for state
87
- parsingStringToState (str ){
84
+ parsingStringToState (str ) {
88
85
let state = [];
89
- if (str .indexOf (' ...mapState' ) === - 1 ){ return state};
90
- let trashedSlice = str .slice (str .lastIndexOf (' ...mapState' )+ 15 );
86
+ if (str .indexOf (' ...mapState' ) === - 1 ) { return state };
87
+ let trashedSlice = str .slice (str .lastIndexOf (' ...mapState' ) + 15 );
91
88
let slice = trashedSlice .slice (0 , trashedSlice .indexOf (' ])' ));
92
- let split = slice .split (' ' )
93
- for (let i = 0 ; i < split .length ; i++ ){
94
- if (split[i].includes (' ,' )){
95
- let cleanStr = split[i].slice (split[i].indexOf (' "' )+ 1 , split[i].lastIndexOf (' "' ))
89
+ let split = slice .split (' ' )
90
+ for (let i = 0 ; i < split .length ; i++ ) {
91
+ if (split[i].includes (' ,' )) {
92
+ let cleanStr = split[i].slice (split[i].indexOf (' "' ) + 1 , split[i].lastIndexOf (' "' ))
96
93
state .push (cleanStr)
97
94
}
98
95
}
@@ -105,11 +102,11 @@ export default {
105
102
106
103
const importObj = {}; // final output
107
104
const htmlList = []; // array populated with substrings '<div>' '</div>' '<p>' etc.
108
- let compName = data[0 ].slice (data[0 ].lastIndexOf (' /' )+ 1 ).replace (/ [^ a-z0-9 -_. ] / gi , ' ' ).replace (/ . vue/ , ' ' );
105
+ let compName = data[0 ].slice (data[0 ].lastIndexOf (' /' ) + 1 ).replace (/ [^ a-z0-9 -_. ] / gi , ' ' ).replace (/ . vue/ , ' ' );
109
106
const vueFile = fs .readFileSync (data[0 ], " utf8" );
110
107
111
- for (const key in this .$store .state .componentMap ){
112
- if (this .$store .state .componentMap [key].componentName === compName){
108
+ for (const key in this .$store .state .componentMap ) {
109
+ if (this .$store .state .componentMap [key].componentName === compName) {
113
110
compName += ' duplicate' ; // appends duplicate if the componentName already exists in state.
114
111
}
115
112
}
@@ -138,9 +135,9 @@ export default {
138
135
h6: [" <h6>" , " </h6>" ],
139
136
};
140
137
141
-
142
- let htmlString = vueFile .substring (vueFile .indexOf (' <template >' )+ 10 , vueFile .indexOf (' </template>' ));
143
- let scriptString = vueFile .substring (vueFile .indexOf (` <script>` )+ 8 , vueFile .indexOf (` /script>` )- 1 )
138
+
139
+ let htmlString = vueFile .substring (vueFile .indexOf (' <template >' ) + 10 , vueFile .indexOf (' </template>' ));
140
+ let scriptString = vueFile .substring (vueFile .indexOf (` <script>` ) + 8 , vueFile .indexOf (` /script>` ) - 1 )
144
141
145
142
htmlParser (htmlString);
146
143
importObj .props = this .parsingStringToProps (scriptString);
@@ -152,7 +149,7 @@ export default {
152
149
let groupings = findGroupings (htmlList);
153
150
let groupingObj = objectGenerator (groupings);
154
151
let groupingArray = [];
155
- for (const key in groupingObj){
152
+ for (const key in groupingObj) {
156
153
groupingArray .push (groupingObj[key])
157
154
}
158
155
importObj .htmlList = groupingArray;
@@ -165,19 +162,19 @@ export default {
165
162
* @return : nothing: passes the substrings into buildList to generate an array of elements
166
163
*/
167
164
168
- function htmlParser (htmlString ){
169
- if (! htmlString){ return }
165
+ function htmlParser (htmlString ) {
166
+ if (! htmlString) { return }
170
167
// remove new lines and tabs from HTML
171
168
htmlString = htmlString .replaceAll (' \n ' , ' ' ).replaceAll (' \t ' , ' ' );
172
169
// find index for the first < and > in the string
173
170
let openTag = htmlString .indexOf (' <' );
174
171
let closeTag = htmlString .indexOf (' >' );
175
172
176
173
// push the substring wrapped by < and > into the helper func
177
- buildList (htmlString .substring (openTag+ 1 , closeTag))
174
+ buildList (htmlString .substring (openTag + 1 , closeTag))
178
175
179
176
// recursively call htmlParser on the remainder of the string
180
- return htmlParser (htmlString .substring (closeTag+ 1 ))
177
+ return htmlParser (htmlString .substring (closeTag + 1 ))
181
178
}
182
179
183
180
@@ -190,24 +187,24 @@ export default {
190
187
* @return : nothing -- pushes into htmlList array which is defined outside scope of buildList
191
188
*/
192
189
193
- function buildList (substring ){
194
- for (const elem in htmlElementMap){
195
- for (let i = 0 ; i < htmlElementMap[elem].length ; i++ ){
196
- // if the htmlElementMap[elem][index] matches the substring, push it into the output array
197
- if (substring === ' p' ){
198
- htmlList .push (htmlElementMap .paragraph [0 ])
199
- return ;
200
- } else if (substring === ' /p' ) {
201
- htmlList .push (htmlElementMap .paragraph [1 ])
202
- return ;
203
- }
204
- if (htmlElementMap[elem][i].indexOf (substring) !== - 1 ){
205
- htmlList .push (htmlElementMap[elem][i])
206
- return ;
190
+ function buildList (substring ) {
191
+ for (const elem in htmlElementMap) {
192
+ for (let i = 0 ; i < htmlElementMap[elem].length ; i++ ) {
193
+ // if the htmlElementMap[elem][index] matches the substring, push it into the output array
194
+ if (substring === ' p' ) {
195
+ htmlList .push (htmlElementMap .paragraph [0 ])
196
+ return ;
197
+ } else if (substring === ' /p' ) {
198
+ htmlList .push (htmlElementMap .paragraph [1 ])
199
+ return ;
200
+ }
201
+ if (htmlElementMap[elem][i].indexOf (substring) !== - 1 ) {
202
+ htmlList .push (htmlElementMap[elem][i])
203
+ return ;
204
+ }
207
205
}
208
206
}
209
207
}
210
- }
211
208
212
209
/**
213
210
* @name : findGroupings
@@ -216,31 +213,31 @@ export default {
216
213
* @return : returns an array of arrays (grouped by parent/child)
217
214
*/
218
215
219
- function findGroupings (array ){
216
+ function findGroupings (array ) {
220
217
let count = 0 ; // tracks where the parent ends
221
218
let stopIndexes = []; // an array that will be used to slice out the parent/child relationships
222
- for (let i = 0 ; i < array .length ; i++ ){
223
- if (array[i][1 ] !== ' /' ){
224
- if (array[i] === ' <img>' || array[i] === ' <a href="#"/>' || array[i] === ' <input />' ){
225
- if (count === 0 ){
219
+ for (let i = 0 ; i < array .length ; i++ ) {
220
+ if (array[i][1 ] !== ' /' ) {
221
+ if (array[i] === ' <img>' || array[i] === ' <a href="#"/>' || array[i] === ' <input />' ) {
222
+ if (count === 0 ) {
226
223
stopIndexes .push (i);
227
224
continue ;
228
225
}
229
226
} else {
230
- count++ ;
227
+ count++ ;
231
228
}
232
229
} else {
233
230
count-- ;
234
231
}
235
- if (count === 0 ){
232
+ if (count === 0 ) {
236
233
stopIndexes .push (i)
237
234
}
238
235
}
239
236
let groupings = [];
240
237
let startIndex = 0 ;
241
- for (let i = 0 ; i < stopIndexes .length ; i++ ){
242
- groupings .push (array .slice (startIndex, stopIndexes[i]+ 1 ));
243
- startIndex = stopIndexes[i]+ 1 ;
238
+ for (let i = 0 ; i < stopIndexes .length ; i++ ) {
239
+ groupings .push (array .slice (startIndex, stopIndexes[i] + 1 ));
240
+ startIndex = stopIndexes[i] + 1 ;
244
241
}
245
242
return groupings;
246
243
}
@@ -253,22 +250,22 @@ export default {
253
250
* @return : returns an object containing parent/children html elements nested.
254
251
*/
255
252
256
- function objectGenerator (array ){
253
+ function objectGenerator (array ) {
257
254
let groupingObj = {};
258
- for (let i = 0 ; i < array .length ; i++ ){
259
- for (const key in htmlElementMap){
260
- if (array[i][0 ] === htmlElementMap[key][0 ]){
261
- let idGen = Date .now ()- Math .floor (Math .random ()* 1000000 );
262
- groupingObj[i] = {text: key, id: idGen}
255
+ for (let i = 0 ; i < array .length ; i++ ) {
256
+ for (const key in htmlElementMap) {
257
+ if (array[i][0 ] === htmlElementMap[key][0 ]) {
258
+ let idGen = Date .now () - Math .floor (Math .random () * 1000000 );
259
+ groupingObj[i] = { text: key, id: idGen }
263
260
}
264
261
}
265
262
array[i].pop ();
266
263
array[i].shift ();
267
- if (array[i].length > 0 ){
264
+ if (array[i].length > 0 ) {
268
265
const childGroupings = findGroupings (array[i]);
269
266
const childrenObj = objectGenerator (childGroupings);
270
267
const childrenArray = [];
271
- for (const key in childrenObj){
268
+ for (const key in childrenObj) {
272
269
childrenArray .push (childrenObj[key])
273
270
}
274
271
groupingObj[i].children = childrenArray;
0 commit comments