-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
generate-modules-site.js
128 lines (105 loc) · 2.82 KB
/
generate-modules-site.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const fs = require("fs");
const util = require("util");
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
// Convert Markdown syntax to HTML
const marked = require("marked");
// Strip HTML tags from a string
const striptags = require("striptags");
// YAML parser
const yaml = require("js-yaml");
// Checks if NPM package exists
const NPMExists = require('npm-exists');
// MoleculerJS modules
const MODULES_PATH = "modules.yml";
// Output dir
const SITE_MODULES = "out/site_modules.yml";
/**
* Transforms data from `modules.yml` format into another (acceptable my moleculer/site)
*
* @param {object} modules
*/
async function transform(modules) {
const obj = {};
for (const moduleKey of Object.keys(modules)) {
const entry = modules[moduleKey];
if (toTransform(entry)) {
// Sanitize all entries
const promises = await Promise.all(entry.entries.map(elem => sanitizeEntry(elem)))
obj[moduleKey] = promises
} else {
// Sub-topics
for (const subModuleKey of Object.keys(entry)) {
const subEntry = entry[subModuleKey];
if (toTransform(subEntry)) {
// Sanitize all entries
const promises = await Promise.all(subEntry.entries.map(elem => sanitizeEntry(elem)))
obj[subModuleKey] = promises
}
}
}
}
return obj;
}
/**
* Sanitize the an entry
*
* @param {object} entry
*/
async function sanitizeEntry(entry) {
try {
// Slice initial escape char
if (
entry.desc.startsWith("/") === true ||
entry.desc.startsWith("\\") === true
) {
entry.desc = entry.desc.slice(1);
}
// Convert Markdown syntax into HTML syntax
entry.desc = marked(entry.desc);
// Remove unwanted HTML tags
// Prepare the description for moleculer/site
entry.desc = striptags(entry.desc, ["a"]);
if(!entry.name) {
console.log(desc)
}
// Check if module is registered at NPM
const moduleExists = await NPMExists(entry.name)
// Set CSS value for display tag
// This is going to be used to build modules page
entry.display = moduleExists ? 'unset': 'none'
return entry;
} catch (error) {
console.log(error);
console.log(entry);
process.exit(1);
}
}
/**
* Checks if a list of modules should be published on site or not
*/
function toTransform(entry) {
const keys = Object.keys(entry);
return (
keys.includes("to-pub-on-site") === true && entry["to-pub-on-site"] === true
);
}
async function main() {
try {
// Load module files
const payload = await readFile(MODULES_PATH, { encoding: "utf8" });
// Parse yaml payload
const modules = yaml.safeLoad(payload, "utf8");
// Transform into site acceptable format
const siteModules = await transform(modules);
// Store new file
await writeFile(
SITE_MODULES,
yaml.safeDump(siteModules, { lineWidth: 500 })
);
} catch (error) {
console.log(error);
process.exit(1);
}
}
main();