-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
generate-whats-new-ids.js
41 lines (35 loc) · 1.07 KB
/
generate-whats-new-ids.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
const vfileGlob = require('vfile-glob');
const { read, write } = require('to-vfile');
const generateWhatsNewIds = async () => {
const file = await read('./src/data/whats-new-ids.json', 'utf-8');
const data = JSON.parse(file.contents);
let largestID = Object.values(data).reduce(
(num, id) => Math.max(parseInt(id, 10), num),
0
);
return new Promise((resolve) => {
const currentWhatsNewPaths = [];
vfileGlob('./src/content/whats-new/**/*.md').subscribe({
next: (file) => {
const slug = file.path
.replace(/.*?src\/content/, '')
.replace('.md', '');
currentWhatsNewPaths.push(slug);
if (!data[slug]) {
data[slug] = String(++largestID);
}
},
complete: async () => {
Object.entries(data).forEach(([path]) => {
if (!currentWhatsNewPaths.includes(path)) {
delete data[path];
}
});
file.contents = JSON.stringify(data, null, 2);
await write(file, 'utf-8');
resolve();
},
});
});
};
generateWhatsNewIds();