This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.js
173 lines (120 loc) · 3.77 KB
/
build.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
Removes ./_build/ directory contents
puts contents from templates in ./assets/ directory
and fills them with newly generated data
*/
require('string.prototype.startswith');
(function () {
var fs = require('fs-extra');
var cry = require('crypto');
var md = require('markdown').markdown;
var config = {
templatesDir: './assets/',
resultsDir: './_build/',
docsDirs: [
'events',
'functions',
'huds',
'iterators',
'tibia-11',
'types',
'variables',
'widgets'
]
};
activate();
function activate () {
prepareDir();
replaceDocsData();
replaceContributorsList();
replaceResourcesPath();
}
function prepareDir () {
// remove public directory contents
fs.emptyDirSync(config.resultsDir);
// copy file templates to public directory
fs.copySync(config.templatesDir, config.resultsDir);
}
function replaceDocsData () {
// Replace docsData
replaceInFile({
sourceFilePath: config.templatesDir + 'js/docs.js',
destFilePath: config.resultsDir + 'js/docs.js',
valueToReplace: cry.createHash('md5').update('docsData').digest('hex'),
valueToSet: JSON.stringify(getDocsData()),
valueTemplate: "'{{ value }}'"
});
}
function replaceContributorsList () {
// Replace contributors list
replaceInFile({
sourceFilePath: config.templatesDir + 'docs.html',
destFilePath: config.resultsDir + 'docs.html',
valueToReplace: cry.createHash('md5').update('contributors').digest('hex'),
valueToSet: md.toHTML(fs.readFileSync(__dirname + '/CONTRIBUTORS.md').toString())
});
}
function replaceResourcesPath () {
var pattern = '{{ resourcesPath }}';
// resources for local
replaceInFile({
sourceFilePath: config.templatesDir + 'docs.html',
destFilePath: config.resultsDir + 'docs_local.html',
valueToReplace: pattern,
valueToSet: 'https://www.tibiawindbot.com/'
});
// resources for production
replaceInFile({
sourceFilePath: config.templatesDir + 'docs.html',
destFilePath: config.resultsDir + 'docs.html',
valueToReplace: pattern,
valueToSet: ''
});
}
function replaceInFile (options) {
if (!options || !options.sourceFilePath || !options.destFilePath || !options.valueToReplace || !options.hasOwnProperty('valueToSet')) {
throw 'Bad options';
}
var fileContent = fs.readFileSync(options.sourceFilePath).toString();
if (options.valueTemplate) {
options.valueToReplace = options.valueTemplate.replace('{{ value }}', options.valueToReplace);
}
fs.createFileSync(options.destFilePath);
fs.outputFile(
options.destFilePath,
fileContent.replace(new RegExp(options.valueToReplace, 'g'), options.valueToSet)
);
}
function getDocsData () {
var result = [];
var requireItem = function (pathPieces) {
pathPieces.unshift(__dirname);
return require(pathPieces.join('/'));
};
// moduleDirs
fs.readdirSync(__dirname).forEach(function (moduleDir) {
if (config.docsDirs.indexOf(moduleDir) > -1) { // This is an allowed directory
// gather module information
var curModule = requireItem([moduleDir, '_index.json']);
curModule.items = [];
var sectionDirs = fs.readdirSync(__dirname + '/' + moduleDir);
sectionDirs.forEach(function (sectionDir) {
if (sectionDir !== '_index.json') {
// gather section information
var curSection = requireItem([moduleDir, sectionDir, '_index.json']);
curSection.items = [];
var entryFiles = fs.readdirSync(__dirname + '/' + moduleDir + '/' + sectionDir);
entryFiles.forEach(function (entryFile) {
if (entryFile !== '_index.json') {
curSection.items.push(requireItem([moduleDir, sectionDir, entryFile]));
}
});
curModule.items.push(curSection);
}
});
result.push(curModule);
}
});
return result;
}
}());