|
| 1 | +<script> |
| 2 | +
|
| 3 | +export default { |
| 4 | +
|
| 5 | + methods: { |
| 6 | + //emitter to send the importedObj to CreateComponent when fully parsed. |
| 7 | + createImport(importObj){ |
| 8 | + this.$emit('imported', importObj) |
| 9 | + }, |
| 10 | +
|
| 11 | + //renders the open file |
| 12 | + importComponent() { |
| 13 | + ipcRenderer |
| 14 | + .invoke("openProject", { |
| 15 | + properties: ["openProject"], |
| 16 | + filters: [ |
| 17 | + { |
| 18 | + name: "Vue Files", |
| 19 | + extensions: ["vue"], |
| 20 | + }, |
| 21 | + ], |
| 22 | + }) |
| 23 | + .then((res) => this.openVueFile(res.filePaths)) |
| 24 | + .catch((err) => console.log(err)); |
| 25 | + }, |
| 26 | + |
| 27 | + //parses script tag string for props |
| 28 | + parsingStringToProps (str){ |
| 29 | + let props = []; |
| 30 | + let split = str.split(' '); |
| 31 | + for(let i = 0; i < split.length; i++){ |
| 32 | + if(split[i].includes(':') && split[i] !== 'computed:' && split[i] !== 'methods:' && split[i] !== 'name:' && split[i] !=='components:'){ |
| 33 | + let cleanStr = split[i].slice(0,split[i].indexOf(':')) |
| 34 | + props.push(cleanStr) |
| 35 | + } |
| 36 | + } |
| 37 | + return props |
| 38 | + }, |
| 39 | +
|
| 40 | + //parses script tag string for actions |
| 41 | + parsingStringToAction (str){ |
| 42 | + let action = []; |
| 43 | + if (str.indexOf('...mapActions') === -1){return action}; |
| 44 | + let trashedSlice = str.slice(str.lastIndexOf('...mapActions')+15); |
| 45 | + let slice = trashedSlice.slice(0, trashedSlice.indexOf('])')); |
| 46 | + let split = slice.split(' ') |
| 47 | + for(let i = 0; i < split.length; i++){ |
| 48 | + if (split[i].includes(',')){ |
| 49 | + let cleanStr = split[i].slice(split[i].indexOf('"')+1,split[i].lastIndexOf('"')) |
| 50 | + action.push(cleanStr) |
| 51 | + } |
| 52 | + } |
| 53 | + return action; |
| 54 | + }, |
| 55 | +
|
| 56 | + //parses script tag string for state |
| 57 | + parsingStringToState (str){ |
| 58 | + let state = []; |
| 59 | + if (str.indexOf('...mapState') === -1){return state}; |
| 60 | + let trashedSlice = str.slice(str.lastIndexOf('...mapState')+15); |
| 61 | + let slice = trashedSlice.slice(0, trashedSlice.indexOf('])')); |
| 62 | + let split = slice.split(' ') |
| 63 | + for (let i = 0; i < split.length; i++){ |
| 64 | + if (split[i].includes(',')){ |
| 65 | + let cleanStr = split[i].slice(split[i].indexOf('"')+1,split[i].lastIndexOf('"')) |
| 66 | + state.push(cleanStr) |
| 67 | + } |
| 68 | + } |
| 69 | + return state |
| 70 | + }, |
| 71 | +
|
| 72 | + //the bulk of the work for this component |
| 73 | + openVueFile(data) { |
| 74 | + if (data === undefined) return; |
| 75 | +
|
| 76 | + const importObj = {}; //final output |
| 77 | + const htmlList = []; //array populated with substrings '<div>' '</div>' '<p>' etc. |
| 78 | + let compName = data[0].slice(data[0].lastIndexOf('/')+1).replace(/[^a-z0-9-_.]/gi, '').replace(/.vue/, ''); |
| 79 | + const vueFile = fs.readFileSync(data[0], "utf8"); |
| 80 | +
|
| 81 | + for (const key in this.$store.state.componentMap){ |
| 82 | + if (this.$store.state.componentMap[key].componentName === compName){ |
| 83 | + compName += 'duplicate'; //appends duplicate if the componentName already exists in state. |
| 84 | + } |
| 85 | + } |
| 86 | + importObj.componentName = compName; |
| 87 | +
|
| 88 | + const htmlElementMap = { //OverVue state management only handles these HTML tags. |
| 89 | + div: ["<div>", "</div>"], |
| 90 | + button: ["<button>", "</button>"], |
| 91 | + form: ["<form>", "</form>"], |
| 92 | + img: ["<img>", ""], |
| 93 | + link: ['<a href="#"/>', ""], |
| 94 | + list: ["<li>", "</li>"], |
| 95 | + paragraph: ["<p>", "</p>"], |
| 96 | + "list-ol": ["<ol>", "</ol>"], |
| 97 | + "list-ul": ["<ul>", "</ul>"], |
| 98 | + input: ["<input />", ""], |
| 99 | + navbar: ["<nav>", "</nav>"], |
| 100 | + }; |
| 101 | +
|
| 102 | + |
| 103 | + let htmlString = vueFile.substring(vueFile.indexOf('<template >')+10, vueFile.indexOf('</template>')); |
| 104 | + let scriptString = vueFile.substring(vueFile.indexOf(`<script>`)+8, vueFile.indexOf(`/script>`)-1) |
| 105 | +
|
| 106 | + htmlParser(htmlString); |
| 107 | + importObj.props = this.parsingStringToProps(scriptString); |
| 108 | + importObj.actions = this.parsingStringToAction(scriptString); |
| 109 | + importObj.state = this.parsingStringToState(scriptString); |
| 110 | +
|
| 111 | + htmlList.pop(); htmlList.shift(); //OverVue adds a <div></div> wrapper to all components. remove this before importing. |
| 112 | +
|
| 113 | + let groupings = findGroupings(htmlList); |
| 114 | + let groupingObj = objectGenerator(groupings); |
| 115 | + let groupingArray = []; |
| 116 | + for (const key in groupingObj){ |
| 117 | + groupingArray.push(groupingObj[key]) |
| 118 | + } |
| 119 | + importObj.htmlList = groupingArray; |
| 120 | + this.createImport(importObj) //send the importObj to CreateComponent. |
| 121 | +
|
| 122 | + /** |
| 123 | + * @name: htmlParser |
| 124 | + * @desc: parses imported .vue file for html elements and state, props and actions |
| 125 | + * @param: a string generated by reading a .vue file from the filesystem |
| 126 | + * @return: nothing: passes the substrings into buildList to generate an array of elements |
| 127 | + */ |
| 128 | +
|
| 129 | + function htmlParser(htmlString){ |
| 130 | + if (!htmlString){return} |
| 131 | + //remove new lines and tabs from HTML |
| 132 | + htmlString = htmlString.replaceAll('\n', '').replaceAll('\t', ''); |
| 133 | + //find index for the first < and > in the string |
| 134 | + let openTag = htmlString.indexOf('<'); |
| 135 | + let closeTag = htmlString.indexOf('>'); |
| 136 | +
|
| 137 | + //push the substring wrapped by < and > into the helper func |
| 138 | + buildList(htmlString.substring(openTag+1, closeTag)) |
| 139 | +
|
| 140 | + //recursively call htmlParser on the remainder of the string |
| 141 | + return htmlParser(htmlString.substring(closeTag+1)) |
| 142 | + } |
| 143 | +
|
| 144 | +
|
| 145 | + /** |
| 146 | + * @name: buildList |
| 147 | + * @desc: takes incoming substrings and pushes the appropriate |
| 148 | + * elements fromt he htmlElementMap defined by Overvue |
| 149 | + * |
| 150 | + * @param: substrings from htmlParser function |
| 151 | + * @return: nothing -- pushes into htmlList array which is defined outside scope of buildList |
| 152 | + */ |
| 153 | +
|
| 154 | + function buildList(substring){ |
| 155 | + for (const elem in htmlElementMap){ |
| 156 | + for (let i = 0; i < htmlElementMap[elem].length; i++){ |
| 157 | + //if the htmlElementMap[elem][index] matches the substring, push it into the output array |
| 158 | + if (substring === 'p'){ |
| 159 | + htmlList.push(htmlElementMap.paragraph[0]) |
| 160 | + return; |
| 161 | + } else if (substring === '/p') { |
| 162 | + htmlList.push(htmlElementMap.paragraph[1]) |
| 163 | + return; |
| 164 | + } |
| 165 | + if (htmlElementMap[elem][i].indexOf(substring) !== -1){ |
| 166 | + htmlList.push(htmlElementMap[elem][i]) |
| 167 | + return; |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | +
|
| 173 | + /** |
| 174 | + * @name: findGroupings |
| 175 | + * @desc: creates groupings of parent/child elements |
| 176 | + * @param: array generated by buildList of html elements from imported Vue file |
| 177 | + * @return: returns an array of arrays (grouped by parent/child) |
| 178 | + */ |
| 179 | +
|
| 180 | + function findGroupings(array){ |
| 181 | + let count = 0; //tracks where the parent ends |
| 182 | + let stopIndexes = []; //an array that will be used to slice out the parent/child relationships |
| 183 | + for (let i = 0; i < array.length; i++){ |
| 184 | + if (array[i][1] !== '/'){ |
| 185 | + if (array[i] === '<img>' || array[i] === '<a href="#"/>' || array[i] === '<input />'){ |
| 186 | + if (count === 0){ |
| 187 | + stopIndexes.push(i); |
| 188 | + continue; |
| 189 | + } |
| 190 | + } else { |
| 191 | + count++; |
| 192 | + } |
| 193 | + } else { |
| 194 | + count--; |
| 195 | + } |
| 196 | + if (count === 0){ |
| 197 | + stopIndexes.push(i) |
| 198 | + } |
| 199 | + } |
| 200 | + let groupings = []; |
| 201 | + let startIndex = 0; |
| 202 | + for (let i = 0; i < stopIndexes.length; i++){ |
| 203 | + groupings.push(array.slice(startIndex, stopIndexes[i]+1)); |
| 204 | + startIndex = stopIndexes[i]+1; |
| 205 | + } |
| 206 | + return groupings; |
| 207 | + } |
| 208 | +
|
| 209 | +
|
| 210 | + /** |
| 211 | + * @name: objectGenerator |
| 212 | + * @desc: generates a nested object from the result of findGroupings nesting children within parents |
| 213 | + * @param: array generated by findGroupings |
| 214 | + * @return: returns an object containing parent/children html elements nested. |
| 215 | + */ |
| 216 | +
|
| 217 | + function objectGenerator(array){ |
| 218 | + let groupingObj = {}; |
| 219 | + for (let i = 0; i < array.length; i++){ |
| 220 | + for (const key in htmlElementMap){ |
| 221 | + if (array[i][0] === htmlElementMap[key][0]){ |
| 222 | + let idGen = Date.now()-Math.floor(Math.random()*1000000); |
| 223 | + groupingObj[i] = {text: key, id: idGen} |
| 224 | + } |
| 225 | + } |
| 226 | + array[i].pop(); |
| 227 | + array[i].shift(); |
| 228 | + if (array[i].length > 0){ |
| 229 | + const childGroupings = findGroupings(array[i]); |
| 230 | + const childrenObj = objectGenerator(childGroupings); |
| 231 | + const childrenArray = []; |
| 232 | + for (const key in childrenObj){ |
| 233 | + childrenArray.push(childrenObj[key]) |
| 234 | + } |
| 235 | + groupingObj[i].children = childrenArray; |
| 236 | + } else { |
| 237 | + groupingObj[i].children = []; |
| 238 | + } |
| 239 | + } |
| 240 | + return groupingObj; |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | +} |
| 245 | +
|
| 246 | +</script> |
| 247 | + |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | + |
| 252 | + |
0 commit comments