-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathindex.js
31 lines (24 loc) · 770 Bytes
/
index.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
import fs from "node:fs/promises";
import path from "node:path";
import debug from "debug";
const errorLog = debug("nodejs-toolbox-catalog:error");
const CATEGORIES_DIRECTORY = "./src/categories";
const categories = [];
try {
const categoryFiles = await fs.readdir(
new URL(CATEGORIES_DIRECTORY, import.meta.url)
);
for (const categoryFile of categoryFiles) {
const contents = await fs.readFile(
new URL(`${CATEGORIES_DIRECTORY}/${categoryFile}`, import.meta.url),
{ encoding: "utf-8" }
);
const category = JSON.parse(contents);
category.slug = path.parse(categoryFile).name;
categories.push(category);
}
} catch (error) {
errorLog("Failed to parse categories %O", error);
throw error;
}
export default categories;