Skip to content

Commit 5a5d028

Browse files
authored
Merge pull request #1 from oslabs-beta/mvp-presentation
Mvp presentation changes to dev branch
2 parents 3b162b0 + e5f61e7 commit 5a5d028

22 files changed

+1607
-260
lines changed

extractor.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
console.log(getVueFiles());
84+
85+
//work with documentation for parse vue to work out how to use this
86+
function parseVueFile(path) {
87+
const fileContents = fs.readFileSync(path).toString();
88+
const parsedFile = parseComponent(fileContents);
89+
return parsedFile
90+
}
91+
92+
console.log(parseVueFile(getVueFiles()[0]))
93+
94+
function generateImportObjs(pathList) { // or something
95+
// iterate through paths
96+
// parse each file
97+
// extract necessary html / js stuff for importObj
98+
//necessary info:
99+
// component name
100+
// boilerplate properties for each component upon creation
101+
// const component = {
102+
// x: 0,
103+
// y: 20,
104+
// z: 0,
105+
// w: 200,
106+
// h: 200,
107+
// htmlList: this.selectedElementList, //htmlList ask Alex
108+
// noteList: [],
109+
// children: [],
110+
// actions: [],
111+
// props: [],
112+
// state: [],
113+
// parent: {},
114+
// isActive: false,
115+
// idDrag: '',
116+
// idDrop: '',
117+
// color: "#ffffff85",
118+
// htmlAttributes: {
119+
// class: "",
120+
// id: ""
121+
// }
122+
// };
123+
// return an array of importObjs with default settings, names, etc
124+
}
125+
126+
// might need to shift away from component name to unique id
127+
128+
// call parseVueFile on every file
129+
// const html = parseHTML(parsedFile);
130+
// const js = parseJS(parsedFile);
131+
132+
// return vueFilePathList;

0 commit comments

Comments
 (0)