-
Notifications
You must be signed in to change notification settings - Fork 11
/
fix-meta.js
57 lines (53 loc) · 1.34 KB
/
fix-meta.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
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
function travel(dir, callback) {
if (dir.includes("node_modules")) {
return;
}
if (fs.statSync(dir).isFile()) {
if (dir.endsWith(".md")) {
callback(dir);
}
} else {
fs.readdirSync(dir).forEach((file) => {
var pathname = path.join(dir, file);
if (fs.statSync(pathname).isDirectory()) {
travel(pathname, callback);
} else if (pathname.endsWith(".md")) {
callback(pathname);
}
});
}
}
function fix(mdpath) {
let yamlFile = path.join(
path.dirname(mdpath),
"..",
path.basename(mdpath, ".md") + ".yaml"
);
let ymlData = null;
if (fs.existsSync(yamlFile)) {
ymlData = yaml.load(fs.readFileSync(yamlFile, "utf8"));
}
if (!ymlData || !ymlData.icon) {
return;
}
let data = fs.readFileSync(mdpath, "utf8");
data = data.replace(/icon: .*/, "icon: " + ymlData.icon);
if (ymlData.sn_available) {
data = data.replace(
/(id:.*\n---)/,
"sn_available: " + ymlData.sn_available + "\n$1"
);
}
fs.writeFileSync(mdpath, data);
console.log(mdpath + " fixed");
}
module.exports = (dir) => {
travel(dir, fix);
};
if (typeof require !== "undefined" && require.main === module) {
const [dir] = process.argv.slice(2);
travel(path.join(__dirname, dir), fix);
}