-
Notifications
You must be signed in to change notification settings - Fork 0
/
projDir.js
89 lines (79 loc) · 2.31 KB
/
projDir.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
const { readdirSync } = require("fs")
const { parse, resolve, basename } = require("path")
/**
* @typedef {Object} Folder
* @property {string} name
* @property {string} type
* @property {any[]} children
*/
/**
* @typedef {Object} File
* @property {string} name
* @property {string} type
*/
module.exports = {
get: dir => {
if (dir.slice(-1) !== "/") dir += "/"
// Psuedo markdown files since I am not implementing MDX
const mds = ["about", "projects", "tech"]
/**
* Generate a tree structure for pages
*
* @param {string} dir
* @returns {(Folder | File)[]}
*/
function walk(dir) {
const files = readdirSync(dir, { withFileTypes: true })
let tree = []
for (const file of files) {
const res = resolve(dir, file.name)
if (file.isDirectory()) {
tree.push({ name: file.name, type: "folder", children: walk(res) })
// Sort everything alphabetically
tree.sort((fa, fb) => {
if (fa.name < fb.name) return -1
if (fa.name > fb.name) return 1
return 0
})
// Make folders be the first items
tree.sort((fa, fb) => {
if (fa.type === "folder") return -1
if (fb.type === "folder") return 1
return 0
})
// Sort folders alphabetically
tree.sort((fa, fb) => {
if (fa.type === "folder" && fb.type === "folder") {
if (fa.name < fb.name) return -1
if (fa.name > fb.name) return 1
return 0
}
})
} else {
const fileName = parse(file.name).name
tree.push({
name: fileName,
type:
// "tips" folder contains psuedo markdown files
mds.includes(fileName) || basename(dir) === "tips"
? ".md"
: parse(file.name).ext,
})
}
}
return tree
}
const tree = walk(dir)
// Remove unnecessary files since this is a NextJS web app
.filter(file => {
const name = parse(file.name).name
if (name === "_app" || name === "api" || name === "index") return false
else return true
})
return {
type: "root",
name: "PORTFOLIO-2022",
children: tree,
}
},
}