forked from leachim6/hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_language.js
63 lines (54 loc) · 1.76 KB
/
list_language.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
const fs = require("fs");
const ignore_files_folder = [
".github",
".editorconfig",
"CONTRIBUTING.md",
".gitignore",
"README.md",
"update_list.py",
"list_language.js",
"LICENSE",
];
let count = 0;
let tableContent = "";
const list_dir = fs
.readdirSync(__dirname)
.filter((each) => !ignore_files_folder.includes(each));
for (let i = 0; i < list_dir.length; i++) {
const dir = list_dir[i];
const filelist = fs.readdirSync(__dirname + "/" + dir);
for (let j = 0; j < filelist.length; j++) {
const filename = filelist[j];
const filepath = `${__dirname}/${dir}/${filename}`;
try {
count++;
const fileSize = fs.statSync(filepath).size + " bytes";
const language = filename.split(".")[0];
const ext = filename.split(".")[1];
const r_path = `${encodeURIComponent(dir)}/${filename}`;
tableContent +=
tableRow(count, filename, r_path, language, ext, fileSize);
} catch (e) {
console.log("Exception");
}
}
}
const readmePath = __dirname + "\\README.md";
const readmeContent = fs.readFileSync(readmePath).toString("utf8");
const start_replace = "^<!--- Table List start-->.*";
const end_replace = "<!--- Table List end-->$";
const regex = new RegExp(start_replace + end_replace, "ms");
fs.writeFileSync(
readmePath,
readmeContent.replace(regex, template(tableContent))
);
function tableRow(no, name, dir, language, extension = "NA", size = "NA") {
return `|${no}|[${name}](${dir})|${language}|${extension}|${size}|\n`;
}
function template(content) {
return `<!--- Table List start-->
|No.|Filename|Language|Extension|Size|
|---|--------|--------|---------|----|
${content}
<!--- Table List end-->`;
}