-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
75 lines (60 loc) · 1.9 KB
/
build.js
File metadata and controls
75 lines (60 loc) · 1.9 KB
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
const fs = require('fs');
const Promise = require('bluebird');
const marked = require('marked');
const updatesFolder = 'updates';
readdir(updatesFolder)
.then(files => {
Promise.allSettled(files.map(file => {
const match = file.match(/([0-9]{4}-[0-9]{2}-[0-9]{2}).md/u);
if (match) {
const date = match[1];
console.log('reading', file);
return read(updatesFolder + '/' + file).then(content => {
return {
date,
content
};
})
}
return Promise.resolve(null);
})).then(promises => {
const updates = promises
.map(promise => promise.value())
.filter(update => update)
.map(update => ({
...update,
content_html: marked(update.content),
}));
updates.sort((a, b) => {
const aDate = Date.parse(a.date);
const bDate = Date.parse(b.date);
return bDate - aDate;
});
fs.writeFile('public/updates.json', JSON.stringify(updates), function (err) {
if (err) {
throw err;
}
console.log('public/updates.json generation completed!');
});
});
});
function read(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf-8', function (err, content) {
if (err) {
reject(err);
}
resolve(content);
});
});
}
function readdir(path) {
return new Promise((resolve, reject) => {
fs.readdir(path, {}, function (err, content) {
if (err) {
reject(err);
}
resolve(content);
});
});
}