-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathcheck-nav.js
62 lines (50 loc) · 1.78 KB
/
check-nav.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
const { basename, join, dirname, sep, posix } = require('path')
function ensureNavigationComplete (nav, fsPaths, ext) {
const navPaths = getNavigationPaths(nav)
const unmatchedNav = {}
const unmatchedFs = {}
for (const navPath of navPaths) {
// every nav path starts as an unmatched fs path
unmatchedFs[navPath] = true
}
for (const path of fsPaths) {
const key = posix.sep + join(dirname(path), basename(path, ext)).split(sep).join(posix.sep)
// for each fs path, if it exists in the nav we
// unmark it as unmatched on the filesystem.
// otherwise its unmarked in the nav
if (unmatchedFs[key]) {
delete unmatchedFs[key]
} else {
unmatchedNav[key] = true
}
}
const toKeys = (v) => Object.keys(v).sort().map((p) => p.split(posix.sep).join(sep))
const missingNav = toKeys(unmatchedNav)
const missingFs = toKeys(unmatchedFs)
const errors = []
if (missingNav.length) {
errors.push('The following path(s) exist on disk but are not present in /lib/content/nav.yml:')
errors.push(...missingNav.map(n => ` ${n}`))
}
if (missingFs.length) {
errors.push('The following path(s) exist in lib/content/nav.yml but are not present on disk:')
errors.push(...missingFs.map(n => ` ${n}`))
}
if (errors.length) {
errors.unshift('Documentation navigation (lib/content/nav.yml) does not match filesystem.')
errors.push('Update nav.yml to ensure that all files are listed in the appropriate place.')
throw new Error(errors.join('\n'))
}
}
function getNavigationPaths (entries) {
const paths = []
for (const entry of entries) {
if (entry.children) {
paths.push(...getNavigationPaths(entry.children))
} else {
paths.push(entry.url)
}
}
return paths
}
module.exports = ensureNavigationComplete