forked from solidjs/solid-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-dir-data.mjs
More file actions
41 lines (36 loc) · 865 Bytes
/
get-dir-data.mjs
File metadata and controls
41 lines (36 loc) · 865 Bytes
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
import fs from "fs/promises";
import path from "path";
import { sectionData } from "../schemas.mjs";
import { languages } from "../index.mjs";
/**
*
* @param {string} path
*/
function formatDirPath(path) {
for (const lang of languages) {
if (path.includes(lang)) {
return path.replace("/" + lang, "");
}
}
return path;
}
export async function getDirData(dirPath = process.cwd()) {
const dir = formatDirPath(dirPath);
try {
const data = JSON.parse(
await fs.readFile(path.resolve(dir, "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 ${dir} have a data.json?`
);
}
}