-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
160 lines (145 loc) · 3.59 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var fs = require("fs");
var readline = require("readline");
const stream = require("stream");
const { promisify } = require("util");
const { resolve } = require("path");
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
const foldersToExclude = [
"@NGComponents",
"@layouts",
"rest/",
"@components/utils/",
"@components/texts/",
"@components/tooltips/",
"@components/buttons/",
"@components/quests/",
"@components/cells/",
"@components/cards/",
"@utils/",
"@NGUtils/",
"uuid",
"web3-token",
"@apollo/",
"@graphql/",
"strapi",
"/Meta",
"/Portal",
"public/assets/",
"highlightjs",
".styles",
"/enums",
".enum",
".js",
".type",
".styles",
"react",
"next/image",
"/axios",
"/Flex",
"next",
"Main",
"ethers",
];
const filename = "diagram.txt";
const folders = "foldersToParse";
let currentFile = "";
let allDependencies = [];
let allFiles = [];
let writeStream = fs.createWriteStream(filename, { flags: "a" });
/**
* MAIN
*/
if (fs.existsSync(filename)) {
fs.unlinkSync(filename);
}
(async () => {
console.log("######## START ########");
getFiles(folders)
.then(async (files) => {
for (const file of files) {
if (file.split(".").pop() === "tsx") {
let fileDependencies = [];
currentFile = file.split("\\").pop().replace(".tsx", "");
allFiles.push(currentFile);
const input = fs.createReadStream(file);
for await (const line of readLines({ input })) {
extractDependencies_File(line, fileDependencies);
}
/* console.log(fileDependencies); */
writeToFile(fileDependencies);
}
}
[...new Set(allDependencies)].map((dependency) => {
if (allFiles.indexOf(dependency) === -1) {
writeStream.write(`${dependency}\n`);
}
});
console.log("######## END ########");
})
.catch((e) => console.error(e));
})();
/**
* ########## FONCTION ##########
*/
async function getFiles(dir) {
const subdirs = await readdir(dir);
const files = await Promise.all(
subdirs.map(async (subdir) => {
const res = resolve(dir, subdir);
return (await stat(res)).isDirectory() ? getFiles(res) : res;
})
);
return files.reduce((a, f) => a.concat(f), []);
}
/**
*
*/
function readLines({ input }) {
const output = new stream.PassThrough({ objectMode: true });
const rl = readline.createInterface({ input });
rl.on("line", (line) => {
output.write(line);
});
rl.on("close", () => {
output.push(null);
});
return output;
}
/**
*
*/
async function writeToFile(fileDependencies) {
if (fileDependencies.length > 0) {
writeStream.write(`${currentFile},"`);
fileDependencies.forEach((item) => {
writeStream.write(`${item}`);
if (item !== fileDependencies[fileDependencies.length - 1]) {
writeStream.write(`,`);
}
});
writeStream.write(`"\n`);
}
}
/**
*
*/
function extractDependencies_File(line, _fileDependencies) {
let _line = line;
_line = _line.replaceAll("'", "")./* replaceAll(".", ""). */ replaceAll(";", "");
const splitResult = _line.split(" ");
if (splitResult.length > 2 && splitResult[0] === "import") {
const lastElement = splitResult[splitResult.length - 1];
let isExclude = false;
foldersToExclude.map((folder) => {
if (lastElement.includes(folder)) {
isExclude = true;
}
});
if (!isExclude) {
/* console.log("lastElement", lastElement); */
_fileDependencies.push(lastElement.split("/").pop());
allDependencies.push(lastElement.split("/").pop());
}
}
}