|
| 1 | +// This file defines extractor functionality that targets the files of an imported component library, generates an array of file paths to each vue component, and parses each component to generate an import object compatible with createComponent |
| 2 | + |
| 3 | +// parsing implementation in progress |
| 4 | + |
| 5 | + |
| 6 | +// importing parseComponent from npm package |
| 7 | +const { parseComponent } = require("vue-sfc-parser"); |
| 8 | +// https://www.npmjs.com/package/vue-sfc-parser |
| 9 | +const fs = require("fs"); |
| 10 | +const path = require("path"); |
| 11 | + |
| 12 | +// generate an array of file objects that are either files or directories (folders) with files |
| 13 | +// input: root directory of component library, empty result arr |
| 14 | +// output: arr of objects (tree data structure) |
| 15 | + |
| 16 | +const getFullDirectory = function(dir, result = []) { |
| 17 | + // list files in component library directory and loop through |
| 18 | + fs.readdirSync(dir).forEach((file) => { |
| 19 | + // builds full path of file |
| 20 | + const fPath = path.resolve(dir, file); |
| 21 | + |
| 22 | + // prepare stats obj |
| 23 | + const fileStats = { file, path: fPath }; |
| 24 | + |
| 25 | + // is the file a directory ? |
| 26 | + // if yes, traverse it also, if no just add it to the result |
| 27 | + if (fs.statSync(fPath).isDirectory()) { |
| 28 | + fileStats.type = "dir"; |
| 29 | + fileStats.files = []; |
| 30 | + result.push(fileStats); |
| 31 | + return getFullDirectory(fPath, fileStats.files); |
| 32 | + } |
| 33 | + |
| 34 | + fileStats.type = "file"; |
| 35 | + result.push(fileStats); |
| 36 | + }); |
| 37 | + // returns an array of file objects that are either files, or directories with files inside |
| 38 | + return result; |
| 39 | +}; |
| 40 | + |
| 41 | + |
| 42 | +// collect all file paths nested in full directory tree that end with extension .vue |
| 43 | + |
| 44 | +// const extractFilePaths = ({ dirArray = [], collection = [], target = ".vue" }) => { |
| 45 | +// for (let i = 0; i < dirArray.length; i++) { |
| 46 | +// if (dirArray[i].type === "dir") { |
| 47 | +// extractFilePaths({ dirArray: dirArray[i].files, collection, target }); |
| 48 | +// } else if (dirArray[i].file.endsWith(target)) { |
| 49 | +// collection.push(dirArray[i].path); |
| 50 | +// } |
| 51 | +// } |
| 52 | +// return collection; |
| 53 | +// } |
| 54 | + |
| 55 | +const getFilePathsByExtension = (dirArray, collection = [], extension = ".vue") => { |
| 56 | + for (let i = 0; i < dirArray.length; i++) { |
| 57 | + if (dirArray[i].type === "dir") { |
| 58 | + getFilePathsByExtension(dirArray[i].files, collection, extension); |
| 59 | + } else if (dirArray[i].file.endsWith(extension)) { |
| 60 | + collection.push(dirArray[i].path); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + //return arr of strings representing file path names |
| 65 | + return collection; |
| 66 | +}; |
| 67 | + |
| 68 | +// returns [ 'users/emma/yadda/yadda/button.vue', 'users/emma/yadda/yadda/button.vue' ] |
| 69 | + |
| 70 | +// parseVueFile => parse the view file |
| 71 | +// generateImportObj => call parseVueFile on every string in collection |
| 72 | +// NOTE: if want to expand logic here to allow for other libraries, change package name to name of that libary; look at for stretch goal |
| 73 | + |
| 74 | +const getVueFiles = (package = "iview") => { |
| 75 | + const rootDir = path.join(__dirname, "node_modules", package); |
| 76 | + //invoke getFullDirectory with root directory of package |
| 77 | + const fullDirectory = getFullDirectory(rootDir); |
| 78 | + //invoke getFilePathsByExtension with fullDirectory |
| 79 | + const vueFilePathList = getFilePathsByExtension(fullDirectory); |
| 80 | + return vueFilePathList; |
| 81 | +}; |
| 82 | + |
| 83 | + |
| 84 | +//work with documentation for parse vue to work out how to use this |
| 85 | +function parseVueFile(path) { |
| 86 | + const fileContents = fs.readFileSync(path).toString(); |
| 87 | + const parsedFile = parseComponent(fileContents); |
| 88 | + return parsedFile |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +function generateImportObjs(pathList) { // or something |
| 94 | + // iterate through paths |
| 95 | + // parse each file |
| 96 | + // extract necessary html / js stuff for importObj |
| 97 | + //necessary info: |
| 98 | + // component name |
| 99 | + // boilerplate properties for each component upon creation |
| 100 | + // const component = { |
| 101 | + // x: 0, |
| 102 | + // y: 20, |
| 103 | + // z: 0, |
| 104 | + // w: 200, |
| 105 | + // h: 200, |
| 106 | + // htmlList: this.selectedElementList, //htmlList ask Alex |
| 107 | + // noteList: [], |
| 108 | + // children: [], |
| 109 | + // actions: [], |
| 110 | + // props: [], |
| 111 | + // state: [], |
| 112 | + // parent: {}, |
| 113 | + // isActive: false, |
| 114 | + // idDrag: '', |
| 115 | + // idDrop: '', |
| 116 | + // color: "#ffffff85", |
| 117 | + // htmlAttributes: { |
| 118 | + // class: "", |
| 119 | + // id: "" |
| 120 | + // } |
| 121 | + // }; |
| 122 | + // return an array of importObjs with default settings, names, etc |
| 123 | +} |
| 124 | + |
| 125 | +// might need to shift away from component name to unique id |
| 126 | + |
| 127 | +// call parseVueFile on every file |
| 128 | +// const html = parseHTML(parsedFile); |
| 129 | +// const js = parseJS(parsedFile); |
| 130 | + |
| 131 | +// return vueFilePathList; |
0 commit comments