-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileToMap.js
63 lines (58 loc) · 1.68 KB
/
fileToMap.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("node:fs");
const path = require("node:path");
const util = require("util");
const isFile = (fileName) => {
return fs.lstatSync(fileName).isFile();
};
const isDir = (dirName) => {
return fs.lstatSync(dirName).isDirectory();
};
const folderPath = "./src/content/";
try {
if (!fs.existsSync(folderPath)) {
// fs.mkdirSync(folderName);
console.error("目录不存在");
}
} catch (err) {
console.error(err);
}
// let files = fs.readdirSync(folderPath);
// files = files
// .map((fileName) => {
// return path.join(folderPath, fileName);
// })
// .filter(isFile);
const filterPath = (filePath) => {
// console.log("filterPath:", filePath);
return filePath.replace("src/content/", ""); //.replace(/(\d{1,2}-)/g, "");
};
const readDocsDir = (folderPath) => {
let files = fs.readdirSync(folderPath);
let fileMap = [];
for (let fileName of files) {
let file = path.join(folderPath, fileName);
if (isFile(file)) fileMap.push(filterPath(file));
if (isDir(file)) {
// console.log(file);
// fileMap.push({ path: fileName, file: readDocsDir(file) });
// fileMap.push(readDocsDir(file));
readDocsDir(file).map((fileSub) => fileMap.push(filterPath(fileSub)));
}
}
return fileMap;
};
let mapPath = {};
readDocsDir(folderPath).map((file) => {
let key = file
.replace(".mdx", "")
.replace("/index", "")
.replace(/(\d{1,2}-)/g, "");
mapPath[key] = file;
});
try {
fs.writeFileSync("./src/nav/link.map.json", JSON.stringify(mapPath, null, 2), "utf8");
} catch (err) {
console.error(err);
}
// console.log(files);
// console.log(util.inspect(mapPath, { showHidden: false, depth: null, colors: true }));