Skip to content

Commit 765783c

Browse files
author
Emma Genesen
committed
wrote file extractor to target vue component files of imported librbary and started integrating parse function to convert vue files to importObj
1 parent 3b162b0 commit 765783c

File tree

5 files changed

+391
-25733
lines changed

5 files changed

+391
-25733
lines changed

extractor.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
// this would need to exist in the root directory of a project (or whatever level the node modules are on)
6+
7+
// importing parseComponent from npm package
8+
const { parseComponent } = require("vue-sfc-parser");
9+
// https://www.npmjs.com/package/vue-sfc-parser
10+
const fs = require("fs");
11+
const path = require("path");
12+
13+
// generate an array of file objects that are either files or directories (folders) with files
14+
// input: root directory of component library, empty result arr
15+
// output: arr of objects (tree data structure)
16+
17+
const getFullDirectory = function(dir, result = []) {
18+
// list files in component library directory and loop through
19+
fs.readdirSync(dir).forEach((file) => {
20+
// builds full path of file
21+
const fPath = path.resolve(dir, file);
22+
23+
// prepare stats obj
24+
const fileStats = { file, path: fPath };
25+
26+
// is the file a directory ?
27+
// if yes, traverse it also, if no just add it to the result
28+
if (fs.statSync(fPath).isDirectory()) {
29+
fileStats.type = "dir";
30+
fileStats.files = [];
31+
result.push(fileStats);
32+
return getFullDirectory(fPath, fileStats.files);
33+
}
34+
35+
fileStats.type = "file";
36+
result.push(fileStats);
37+
});
38+
// returns an array of file objects that are either files, or directories with files inside
39+
return result;
40+
};
41+
42+
43+
// collect all file paths nested in full directory tree that end with extension .vue
44+
45+
// const extractFilePaths = ({ dirArray = [], collection = [], target = ".vue" }) => {
46+
// for (let i = 0; i < dirArray.length; i++) {
47+
// if (dirArray[i].type === "dir") {
48+
// extractFilePaths({ dirArray: dirArray[i].files, collection, target });
49+
// } else if (dirArray[i].file.endsWith(target)) {
50+
// collection.push(dirArray[i].path);
51+
// }
52+
// }
53+
// return collection;
54+
// }
55+
56+
const getFilePathsByExtension = (dirArray, collection = [], extension = ".vue") => {
57+
for (let i = 0; i < dirArray.length; i++) {
58+
if (dirArray[i].type === "dir") {
59+
getFilePathsByExtension(dirArray[i].files, collection, extension);
60+
} else if (dirArray[i].file.endsWith(extension)) {
61+
collection.push(dirArray[i].path);
62+
}
63+
}
64+
65+
//return arr of strings representing file path names
66+
return collection;
67+
};
68+
69+
// returns [ 'users/emma/yadda/yadda/button.vue', 'users/emma/yadda/yadda/button.vue' ]
70+
71+
// parseVueFile => parse the view file
72+
// generateImportObj => call parseVueFile on every string in collection
73+
// 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
74+
75+
const getVueFiles = (package = "iview") => {
76+
const rootDir = path.join(__dirname, "node_modules", package);
77+
//invoke getFullDirectory with root directory of package
78+
const fullDirectory = getFullDirectory(rootDir);
79+
//invoke getFilePathsByExtension with fullDirectory
80+
const vueFilePathList = getFilePathsByExtension(fullDirectory);
81+
return vueFilePathList;
82+
};
83+
84+
console.log(getVueFiles());
85+
86+
//work with documentation for parse vue to work out how to use this
87+
function parseVueFile(path) {
88+
const fileContents = fs.readFileSync(path).toString();
89+
const parsedFile = parseComponent(fileContents);
90+
return parsedFile
91+
}
92+
93+
console.log(parseVueFile(getVueFiles()[0]))
94+
95+
function generateImportObjs(pathList) { // or something
96+
// iterate through paths
97+
// parse each file
98+
// extract necessary html / js stuff for importObj
99+
// return an array of importObjs with default settings, names, etc
100+
}
101+
102+
// might need to shift away from component name to unique id
103+
104+
// call parseVueFile on every file
105+
// const html = parseHTML(parsedFile);
106+
// const js = parseJS(parsedFile);
107+
108+
// return vueFilePathList;

0 commit comments

Comments
 (0)