forked from iconify/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-missing.ts
More file actions
77 lines (66 loc) · 1.9 KB
/
Copy pathcheck-missing.ts
File metadata and controls
77 lines (66 loc) · 1.9 KB
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
import { cwd } from 'node:process'
import { resolve } from 'node:path'
import { lstatSync, readdirSync } from 'node:fs'
import type { SidebarItem } from '../.vitepress/nav/sidebar-item'
import { GlobalSidebar } from '../.vitepress/nav/sidebar'
const rootFolder = cwd()
const distFolder = resolve(rootFolder, 'dist')
const ignoredFiles = new Set([
// Main files
'/index.html', '/404.html',
// Docs used in iframe
'/docs/delay-demo-freeze.html', '/docs/delay-demo-icon.html', '/docs/delay-demo.html',
])
// Find all built files
const files: Set<string> = new Set()
function scanDir(parent: string) {
const dir = distFolder + parent
readdirSync(dir).forEach((item) => {
if (item.startsWith('.'))
return
const filename = `${dir}/${item}`
const stat = lstatSync(filename)
if (stat.isDirectory()) {
scanDir(`${parent}/${item}`)
return
}
if (item.endsWith('.html')) {
const file = `${parent}/${item}`
if (!ignoredFiles.has(file))
files.add(file)
}
})
}
scanDir('')
// Flatten navigation
const nav: string[] = []
function addItems(items: SidebarItem[]) {
items.forEach(({ link, items }) => {
if (link) {
// Validate it
if (!link.startsWith('/'))
throw new Error(`Link "${link}" is not absolute`)
// Clean up link, pointing to .html file
let file
if (link.endsWith('/'))
file = `${link}index.html`
else if (link.endsWith('.md'))
file = link.replace('.md', '.html')
else
file = `${link}.html`
nav.push(file)
// Check if it exists
if (!files.has(file))
throw new Error(`Missing built file for "${link}", expected "${file}"`)
files.delete(file)
}
// Add child items
if (items)
addItems(items)
})
}
addItems(GlobalSidebar)
if (files.size) {
console.log(`${files.size} items not in sidebar:`)
console.log(Array.from(files).join('\n'))
}