forked from solidjs/solid-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolid-collections.mjs
More file actions
191 lines (169 loc) · 4.14 KB
/
solid-collections.mjs
File metadata and controls
191 lines (169 loc) · 4.14 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { z } from "zod";
import fs from "fs/promises";
import { existsSync } from "fs";
import path from "path";
import matter from "gray-matter";
const COLLECTIONS_ROOT = "src/routes";
const pages = z.array(z.string());
const sectionSchema = z.object({
type: z.literal("section"),
title: z.string(),
// children: z.array(z.string()),
pages,
});
const entrySchema = z.object({
type: z.literal("markdown"),
path: z.string(),
slug: z.string(),
titles: z.string(),
});
const sectionData = z.object({
title: z.string(),
pages,
});
const frontMatterSchema = z.object({
title: z.string(),
});
async function getDirData(dirPath = process.cwd()) {
try {
const data = JSON.parse(
await fs.readFile(path.resolve(dirPath, "data.json"), "utf-8")
);
if (!sectionData.safeParse(data).success) {
// throw new Error("failed to parse")
console.error("failed to parse::", data);
}
return data;
} catch (e) {
console.error("\n");
console.error("\n");
console.error(e);
throw new Error(
`failed to parse directory info. Does ${dirPath} have a data.json?`
);
}
}
async function buildFileTree(entry = COLLECTIONS_ROOT) {
const entryPath = path.resolve(process.cwd(), entry);
const parentSegment = path.parse(entryPath).dir;
const stats = await fs.stat(entryPath);
if (stats.isDirectory()) {
const info = await getDirData(entryPath);
const nested = await Promise.all(
info.pages.map(async (file) => {
return buildFileTree(path.join(entryPath, file));
})
);
return {
type: "section",
title: info.title,
pages: info.pages,
children: nested.filter(Boolean),
};
} else if (!entryPath.includes("data.json")) {
const file = await fs.readFile(entryPath, "utf-8");
const parentSection = await getDirData(path.resolve(parentSegment));
const { title, mainNavExclude } = matter(file).data;
/**
* @todo
* parse frontmatter with Zod
*/
return {
type: "markdown",
file: path.basename(entryPath),
path:
"/" +
path
.relative(path.join(process.cwd(), COLLECTIONS_ROOT), entryPath)
.replace(/\index\.mdx?/, "")
.replace(/\.mdx?/, ""),
slug: path.basename(entryPath, path.extname(entryPath)),
parent: parentSection.title,
title,
mainNavExclude,
};
} else {
console.error(`WARNING: \n ${entry} was not found.\n Please fix it!\n`);
return;
}
}
async function createNavTree() {
const [learn, references] = await Promise.all([
buildFileTree(COLLECTIONS_ROOT),
buildFileTree(`${COLLECTIONS_ROOT}/reference`),
]);
if (
learn &&
learn.type === "section" &&
references &&
references.type === "section"
) {
return {
references: references.children,
learn: learn.children,
};
}
}
/**
*
* @param {string} fileName
* @param {object} fileContent
* @param {boolean} removeAsConst
* @param {string} collectionDir
*/
async function writeFile(
fileName,
fileContent,
removeAsConst = false,
collectionDir = ".solid"
) {
fs.writeFile(
path.resolve(collectionDir, fileName),
`export default ${JSON.stringify(fileContent, null, 2)} ${
removeAsConst ? "" : "as const"
}`
);
}
async function createSolidCollectionDir() {
const collectionDir = path.resolve(process.cwd(), ".solid");
if (!existsSync(collectionDir)) {
fs.mkdir(path.resolve(process.cwd(), ".solid"));
}
}
/**
*
* @param {Awaited<ReturnType<typeof createNavTree>>} tree
* @param {object} entryMap
*/
function createFlatEntryList(tree, entryMap) {
for (const item of tree) {
if (item.type === "markdown") {
if (entryMap.findIndex((e) => e.slug === item.slug) > -1) {
console.error(`Duplicated entry found: ${item.slug}`);
break;
}
entryMap.push(item);
} else {
createFlatEntryList(item.children, entryMap);
}
}
return entryMap;
}
(async () => {
const tree = await createNavTree();
await createSolidCollectionDir();
const learnMap = createFlatEntryList(tree.learn, []);
const referenceMap = createFlatEntryList(tree.references, []);
await Promise.all([
writeFile("tree.ts", tree),
writeFile(
"entriesList.js",
{
references: referenceMap,
learn: learnMap,
},
true
),
writeFile("entries.ts", { references: referenceMap, learn: learnMap }),
]);
})();