-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
123 lines (107 loc) · 4.26 KB
/
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
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
const Mustache = require('mustache');
const fs = require('fs');
const {
formatList,
formatMiddleware,
} = require('./helper.js');
const commandMap = JSON.parse(
fs.readFileSync('commandMap.json', { encoding: 'utf8' })
);
const statistics = {
categories: 0,
commands: 0
};
const templates = {
commandFull: fs.readFileSync(`./templates/_command-full.md`, { encoding: 'utf8' })
}
// Sets up the the commands and categories variables, each variable will eventually hold
// all the formatted command or category data, the command map wil be sorted into
// "short" and "full" types for generating different types of command outputs.
const commands = {};
const categories = [];
for (let categoryName in commandMap) {
statistics.categories++;
// Creates the command "short" and "full" arrays that
// should be stored in the commands map.
let categoryCommandsFull = [];
let categoryCommandsShort = [];
// Stores the default prefix for the command category, this
// will be used later when creating command triggers.
let categoryPrefix = commandMap[categoryName].prefix;
for (let commandName in commandMap[categoryName].commands) {
statistics.commands++;
// Creates the command variable used for the templates
let command = commandMap[categoryName].commands[commandName];
// If the command priority is hidden we'll skip it here as
// well to prevent creating it in the markdown later.
if (command.priority === 'HIDDEN') {
continue;
}
// Sets up some generated data from the command, to
// make generating the templates a bit easier.
command.trigger = categoryPrefix + command.triggers[0];
command.shortDescription = command.description.split('\n')[0];
let middlewares = formatMiddleware(command.middlewares);
command.middlewares = middlewares === false || middlewares.length === 0
? false : '**Special permissions required to run this command**<br>' + middlewares.join('<br>');
// Creates the command relationships, setting them to false by default, otherwise
// storing them as a list of objects with the "name" and "command" keys.
let commandRelationships = false;
if (command.relationships != null) {
commandRelationships = [];
for (let commandRelationship of command.relationships) {
let parts = commandRelationship.split('::');
if (parts.length === 2) {
commandRelationships.push({
name: commandMap[parts[0]].commands[parts[1]].name,
command: parts[1]
});
}
}
}
// Stores the command in its short and full versions.
categoryCommandsShort.push({ commandName, command });
categoryCommandsFull.push({
command: Mustache.render(templates.commandFull, {
commandUsage: formatList(command.usage, command),
commandExample: formatList(command.example, command),
commandRelationships,
commandName,
command,
})
});
}
// Stors the "short" and "full" version of the commands under the current category.
commands[categoryName.toLowerCase()] = {
short: categoryCommandsShort,
full: categoryCommandsFull,
};
}
/**
* Generates the output based of the created data and the templates.
*/
fs.readdir('./templates', (err, files) => {
for (let file of files) {
if (file.substr(0, 1) == '_') {
continue;
}
console.log(`Formatting ${file}`);
// Writes the generated ouput to a file with the same name as
// the template, storing it in the "output" directory.
fs.writeFile(
`./output/${file}`,
Mustache.render(
fs.readFileSync(`./templates/${file}`, { encoding: 'utf8' }), {
version: '{{version}}',
statistics,
commands,
}
),
err => {
if (err) {
return console.error(`Failed to save ${file}`, err);
}
}
);
}
});