-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompile.js
75 lines (60 loc) · 2.26 KB
/
compile.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"use strict";
const glob = require("glob");
const path = require("path");
const fs = require("fs-extra");
const pathRegexp = /\.\/src\/(?<language>[^/\s]+)\/(?<module>\S+)\/(?<name>[^/\s.]+)(\.?(?<ext>[^/\s.]+))?/;
function generateLanguageFile(language) {
const data = [];
glob.sync(`./src/${language}/**/*.js`).forEach(function(file) {
const fileContent = require(path.resolve(file));
const transformedContent = {};
const { module } = file.match(pathRegexp).groups;
Object.entries(fileContent).forEach(([key, item]) => {
transformedContent[`[${module}] ${key}`] = {
prefix: item.prefix,
body: item.body,
description: item.description || key
};
});
data.push(transformedContent);
});
// VSCODE
fs.outputJsonSync(`dist/${language}.json`, Object.assign({}, ...data), { spaces: 2 });
}
function generateDocs() {
glob.sync("./src/**/*.js").forEach(function(file) {
const toc = [];
const readMe = [];
const fileContent = require(path.resolve(file));
const { language, module, name, ext } = file.match(pathRegexp).groups;
const mdFilePath = `docs/${language}/${module}/${name}.md`;
Object.entries(fileContent).forEach(([key, value]) => {
const anchor = key.replace(/[^\w\s]+/g, "").replace(/\s/g, "-").toLowerCase();
toc.push({ key, link: `#${anchor}`});
const body = value.body
.replace(/(?<=\${\d+:\S+)}/g, "")
.replace(/\${\d+:?}?/g, "")
.replace(/\t/g, " ");
readMe.push(`### \`${key}\``);
readMe.push(`**Prefix:** \`${value.prefix}\`\n`);
readMe.push("**Description**:");
readMe.push("```");
readMe.push(value.description || key);
readMe.push("```");
readMe.push("**Generated code**:");
readMe.push(`\`\`\`${ext}`);
readMe.push(`${body}`);
readMe.push("```");
});
readMe.unshift("## Snippets");
readMe.unshift(toc.map((el) => `- [${el.key}](${el.link})`).join("\n"));
readMe.unshift("## Table of Contents");
readMe.unshift(`# ${language} - ${module} - ${name}`);
fs.outputFileSync(mdFilePath, readMe.join("\n"));
});
}
fs.emptyDirSync("dist");
generateLanguageFile("javascript");
generateLanguageFile("handlebars");
fs.emptyDirSync("docs");
generateDocs();