forked from cosmos/sdk-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-config.js
executable file
·94 lines (74 loc) · 2.23 KB
/
check-config.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
#!/bin/node
const fs = require('fs');
REQUIRED_FIELDS = [ "theme", "title", "head", "themeConfig", "plugins", "patterns"];
REQUIRED_THEME_FIELDS = ["isIDAMode", "label", "sidebar"]
let errCount = 0;
console.log("checking config file consistency..")
const config = require("./.vuepress/config.js")
// check required fields
REQUIRED_FIELDS.forEach((f) => {
if (typeof config[f] === "undefined") {
console.log(`ERROR: Config field ${f} is missing`);
reportError();
}
})
REQUIRED_THEME_FIELDS.forEach((f) => {
if (typeof config.themeConfig[f] === "undefined") {
console.log(`ERROR: ThemeConfig field ${f} is missing`);
reportError();
}
})
// check sidebar links
const nav = config.themeConfig.sidebar.nav;
nav.forEach((item) => {
checkNavItem(item);
})
if (errCount > 0) {
console.log(`Found ${errCount} error${errCount > 0 ? "s" : ""} in config!`);
process.exit(1);
} else {
console.log("Config file is valid.");
}
/* Helpers */
function checkNavItem(item) {
if (typeof item.title === "undefined") {
console.log("ERROR: Nav item is missing title! item:");
console.log(item);
reportError();
}
if (typeof item.children === "undefined" && typeof item.path === "undefined") {
console.log("ERROR: Nav item must define either path or children! item:");
console.log(item);
reportError();
}
if (typeof item.children !== "undefined") {
if(!Array.isArray(item.children)) {
console.log("ERROR: Nav item children must be an array! item:");
console.log(item);
reportError();
}
item.children.forEach((sub) => { checkNavItem(sub) });
}
if (typeof item.path !== "undefined") {
if (item.path.charAt(0) != "/") {
console.log("ERROR: Nav item path must start with '/'");
console.log(item.path);
}
if (item.directory) {
if(item.path.charAt(item.path.length-1) != "/") {
console.log("ERROR: Directory nav item path must end with '/'");
console.log(item.path);
reportError();
}
//if (!fs.existsSync)
}
if (!fs.existsSync(`.${item.path.replace(".html", ".md")}`)) {
console.log("ERROR: invalid nav item file path:");
console.log(item.path);
reportError();
}
}
}
function reportError() {
errCount++;
}