-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.js
59 lines (49 loc) · 1.73 KB
/
publish.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
/**
* Created by bjdmeest on 10/06/2016.
* Modified by Dylan Van Assche on 12/02/2021.
* Modified by Thomas Delva on 13/12/2021
*/
var fs = require('fs');
var path = require('path');
var dateString = (new Date()).toISOString().split('T')[0].replace(/-/g, '');
try {
if (!fs.existsSync(path.resolve(__dirname, 'docs'))) {
fs.mkdirSync(path.resolve(__dirname, 'docs'));
}
fs.mkdirSync(path.resolve(__dirname, 'docs', dateString));
} catch (e) {
}
var files = fs.readdirSync(path.resolve(__dirname, 'docs'));
var dirs = [];
for (var i = 0; i < files.length; i++) {
if (fs.lstatSync(path.resolve(__dirname, 'docs', files[i])).isDirectory()) {
dirs.push(files[i]);
}
}
dirs.sort();
dirs.reverse();
if (dirs[0] === "resources") {
dirs.splice(0, 1);
}
if (dirs[0] === dateString) {
dirs.splice(0, 1);
}
// First version, set previous version to this one
if (dirs.length == 0) {
dirs.push(dateString)
}
var html = fs.readFileSync('./rendered.html', 'utf8');
html = html.replace(/%thisDate%/g, dateString);
html = html.replace(/%prevDate%/g, dirs[0]);
fs.writeFileSync(path.resolve(__dirname, 'docs', 'index.html'), html);
fs.writeFileSync(path.resolve(__dirname, 'docs', dateString, 'index.html'), html);
// copying entire resources folder might start taking up lots of space, could be optimized
if (fs.existsSync(path.resolve(__dirname, 'resources'))) {
fs.cpSync(path.resolve(__dirname, 'resources'),
path.resolve(__dirname, 'docs', dateString, 'resources'),
{recursive: true});
fs.rmSync(path.resolve(__dirname, 'docs', 'resources'), {recursive: true})
fs.cpSync(path.resolve(__dirname, 'resources'),
path.resolve(__dirname, 'docs', 'resources'),
{recursive: true});
}