-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_utils.js
More file actions
37 lines (31 loc) · 993 Bytes
/
Copy pathnode_utils.js
File metadata and controls
37 lines (31 loc) · 993 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
import fs from "fs";
import path from "path";
import fg from "fast-glob";
import matter from "gray-matter";
export { fg };
export { path };
export { fs };
export function read(filename, fallback = {}) {
try {
const content = fs.readFileSync(filename, "utf8");
if (filename.endsWith(".json")) {
return JSON.parse(content) || fallback;
}
return matter(content) || fallback;
} catch (e) {
console.error("Unable to read", filename, e);
return fallback;
}
}
export function write(filename, data = {}, content = "") {
let outContent = {};
if (filename.endsWith(".md")) {
data = JSON.parse(JSON.stringify(data)); // remove undefined...
outContent = matter.stringify(content, data, { language: "yaml", yamlOptions: { lineWidth: -1 } });
} else {
outContent = JSON.stringify(data, null, 2);
}
fs.mkdirSync(path.dirname(filename), { recursive: true });
console.log(`→ ${filename}`);
fs.writeFileSync(filename, outContent, "utf8");
}