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
}
@@ -129,9 +126,9 @@ export default {
129
126
navbar: [" <nav>" , " </nav>" ],
130
127
};
131
128
132
-
133
- let htmlString = vueFile .substring (vueFile .indexOf (' <template >' )+ 10 , vueFile .indexOf (' </template>' ));
134
- let scriptString = vueFile .substring (vueFile .indexOf (` <script>` )+ 8 , vueFile .indexOf (` /script>` )- 1 )
129
+
130
+ let htmlString = vueFile .substring (vueFile .indexOf (' <template >' ) + 10 , vueFile .indexOf (' </template>' ));
131
+ let scriptString = vueFile .substring (vueFile .indexOf (` <script>` ) + 8 , vueFile .indexOf (` /script>` ) - 1 )
135
132
136
133
htmlParser (htmlString);
137
134
importObj .props = this .parsingStringToProps (scriptString);
@@ -143,7 +140,7 @@ export default {
143
140
let groupings = findGroupings (htmlList);
144
141
let groupingObj = objectGenerator (groupings);
145
142
let groupingArray = [];
146
- for (const key in groupingObj){
143
+ for (const key in groupingObj) {
147
144
groupingArray .push (groupingObj[key])
148
145
}
149
146
importObj .htmlList = groupingArray;
@@ -156,19 +153,19 @@ export default {
156
153
* @return : nothing: passes the substrings into buildList to generate an array of elements
157
154
*/
158
155
159
- function htmlParser (htmlString ){
160
- if (! htmlString){ return }
156
+ function htmlParser (htmlString ) {
157
+ if (! htmlString) { return }
161
158
// remove new lines and tabs from HTML
162
159
htmlString = htmlString .replaceAll (' \n ' , ' ' ).replaceAll (' \t ' , ' ' );
163
160
// find index for the first < and > in the string
164
161
let openTag = htmlString .indexOf (' <' );
165
162
let closeTag = htmlString .indexOf (' >' );
166
163
167
164
// push the substring wrapped by < and > into the helper func
168
- buildList (htmlString .substring (openTag+ 1 , closeTag))
165
+ buildList (htmlString .substring (openTag + 1 , closeTag))
169
166
170
167
// recursively call htmlParser on the remainder of the string
171
- return htmlParser (htmlString .substring (closeTag+ 1 ))
168
+ return htmlParser (htmlString .substring (closeTag + 1 ))
172
169
}
173
170
174
171
@@ -181,24 +178,24 @@ export default {
181
178
* @return : nothing -- pushes into htmlList array which is defined outside scope of buildList
182
179
*/
183
180
184
- function buildList (substring ){
185
- for (const elem in htmlElementMap){
186
- for (let i = 0 ; i < htmlElementMap[elem].length ; i++ ){
187
- // if the htmlElementMap[elem][index] matches the substring, push it into the output array
188
- if (substring === ' p' ){
189
- htmlList .push (htmlElementMap .paragraph [0 ])
190
- return ;
191
- } else if (substring === ' /p' ) {
192
- htmlList .push (htmlElementMap .paragraph [1 ])
193
- return ;
194
- }
195
- if (htmlElementMap[elem][i].indexOf (substring) !== - 1 ){
196
- htmlList .push (htmlElementMap[elem][i])
197
- return ;
181
+ function buildList (substring ) {
182
+ for (const elem in htmlElementMap) {
183
+ for (let i = 0 ; i < htmlElementMap[elem].length ; i++ ) {
184
+ // if the htmlElementMap[elem][index] matches the substring, push it into the output array
185
+ if (substring === ' p' ) {
186
+ htmlList .push (htmlElementMap .paragraph [0 ])
187
+ return ;
188
+ } else if (substring === ' /p' ) {
189
+ htmlList .push (htmlElementMap .paragraph [1 ])
190
+ return ;
191
+ }
192
+ if (htmlElementMap[elem][i].indexOf (substring) !== - 1 ) {
193
+ htmlList .push (htmlElementMap[elem][i])
194
+ return ;
195
+ }
198
196
}
199
197
}
200
198
}
201
- }
202
199
203
200
/**
204
201
* @name : findGroupings
@@ -207,31 +204,31 @@ export default {
207
204
* @return : returns an array of arrays (grouped by parent/child)
208
205
*/
209
206
210
- function findGroupings (array ){
207
+ function findGroupings (array ) {
211
208
let count = 0 ; // tracks where the parent ends
212
209
let stopIndexes = []; // an array that will be used to slice out the parent/child relationships
213
- for (let i = 0 ; i < array .length ; i++ ){
214
- if (array[i][1 ] !== ' /' ){
215
- if (array[i] === ' <img>' || array[i] === ' <a href="#"/>' || array[i] === ' <input />' ){
216
- if (count === 0 ){
210
+ for (let i = 0 ; i < array .length ; i++ ) {
211
+ if (array[i][1 ] !== ' /' ) {
212
+ if (array[i] === ' <img>' || array[i] === ' <a href="#"/>' || array[i] === ' <input />' ) {
213
+ if (count === 0 ) {
217
214
stopIndexes .push (i);
218
215
continue ;
219
216
}
220
217
} else {
221
- count++ ;
218
+ count++ ;
222
219
}
223
220
} else {
224
221
count-- ;
225
222
}
226
- if (count === 0 ){
223
+ if (count === 0 ) {
227
224
stopIndexes .push (i)
228
225
}
229
226
}
230
227
let groupings = [];
231
228
let startIndex = 0 ;
232
- for (let i = 0 ; i < stopIndexes .length ; i++ ){
233
- groupings .push (array .slice (startIndex, stopIndexes[i]+ 1 ));
234
- startIndex = stopIndexes[i]+ 1 ;
229
+ for (let i = 0 ; i < stopIndexes .length ; i++ ) {
230
+ groupings .push (array .slice (startIndex, stopIndexes[i] + 1 ));
231
+ startIndex = stopIndexes[i] + 1 ;
235
232
}
236
233
return groupings;
237
234
}
@@ -244,22 +241,22 @@ export default {
244
241
* @return : returns an object containing parent/children html elements nested.
245
242
*/
246
243
247
- function objectGenerator (array ){
244
+ function objectGenerator (array ) {
248
245
let groupingObj = {};
249
- for (let i = 0 ; i < array .length ; i++ ){
250
- for (const key in htmlElementMap){
251
- if (array[i][0 ] === htmlElementMap[key][0 ]){
252
- let idGen = Date .now ()- Math .floor (Math .random ()* 1000000 );
253
- groupingObj[i] = {text: key, id: idGen}
246
+ for (let i = 0 ; i < array .length ; i++ ) {
247
+ for (const key in htmlElementMap) {
248
+ if (array[i][0 ] === htmlElementMap[key][0 ]) {
249
+ let idGen = Date .now () - Math .floor (Math .random () * 1000000 );
250
+ groupingObj[i] = { text: key, id: idGen }
254
251
}
255
252
}
256
253
array[i].pop ();
257
254
array[i].shift ();
258
- if (array[i].length > 0 ){
255
+ if (array[i].length > 0 ) {
259
256
const childGroupings = findGroupings (array[i]);
260
257
const childrenObj = objectGenerator (childGroupings);
261
258
const childrenArray = [];
262
- for (const key in childrenObj){
259
+ for (const key in childrenObj) {
263
260
childrenArray .push (childrenObj[key])
264
261
}
265
262
groupingObj[i].children = childrenArray;
0 commit comments