-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.js
59 lines (50 loc) · 1.63 KB
/
generator.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
const fs = require("fs").promises;
const path = require("path");
const { getLabelValue } = require("./utils.js");
const listFiles = async ({ folderName, isChild }) => {
const children = await fs.readdir(folderName, { withFileTypes: true });
const markDownFiles = children
.filter(file => file.name.includes(".md"))
.map(file => file.name);
const folders = children.filter(
child => !child.isFile() && child.name[0] !== "."
);
let snippet = {};
for (const folder of folders) {
listFiles({
folderName: path.join(folderName, folder.name),
isChild: true
});
}
for (const fileName of markDownFiles) {
const fileContent = (
await fs.readFile(path.join(folderName, fileName), "utf8")
).split("\n");
/*
-----------------------------------------
Name: Poorly named snippet
Prefix: pns
Description: This snippet does something
-----------------------------------------
This is the actual snippet body
*/
const snippetName = getLabelValue(fileContent[1], "Name:");
const prefix = getLabelValue(fileContent[2], "Prefix:");
const body = fileContent.slice(5, fileContent.length - 1);
const description = getLabelValue(fileContent[3], "Description:");
snippet = {
...snippet,
[snippetName]: {
prefix,
body,
description
}
};
}
if (isChild && Object.keys(snippet).length !== 0) {
console.log(`* ${folderName}.json`);
await fs.writeFile(`${folderName}.json`, JSON.stringify(snippet, null, 2));
}
};
console.log("Writing snippets to\n");
listFiles({ folderName: path.resolve(), isChild: false });