forked from VueMeetups/vuemeetups.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-events.js
103 lines (83 loc) · 2.79 KB
/
generate-events.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
const fs = require('fs');
const axios = require('axios');
const eventsTimeline = {};
function getYearFromFile(fileName) {
return fileName.replace('.json', '');
}
function getGetOrdinal(n) {
const s = ['th', 'st', 'nd', 'rd'],
v = n % 100;
return `${n}${(s[(v-20)%10]||s[v]||s[0])}`;
}
function addEvent(event) {
const date = new Date(event.startDate);
const year = date.getFullYear();
const locale = 'en-us';
const month = date.toLocaleString(locale, { month: 'long' });
if (!eventsTimeline[year][month]) {
eventsTimeline[year][month] = [];
}
eventsTimeline[year][month].push(event);
}
function removeVueVixensEvents(staticEventsData) {
for (const fileName of staticEventsData) {
const year = getYearFromFile(fileName);
Object.keys(eventsTimeline[year]).forEach((month) => {
const vvEvents = eventsTimeline[year][month]
.filter((event) => event.tag && event.tag === 'vuevixens');
if (vvEvents) {
vvEvents.forEach((event) => {
const existingIndex = eventsTimeline[year][month].findIndex((item) => item.id === event.id);
eventsTimeline[year][month].splice(existingIndex, 1);
});
}
});
}
}
async function getVueVixensEvents() {
let response = await axios.get(`https://api.storyblok.com/v1/cdn/stories/upcoming?version=published&cv=1541163074263&token=${process.env.VV_TOKEN}`);
return response.data.story.content.body.map((event) => ({
id: `vm-${event._uid}`,
date: getGetOrdinal(new Date(event.date).getDate()),
startDate: event.date,
endDate: event.date,
organiser: 'Vue Vixens',
organiserLink: 'https://vuevixens.org',
name: event.name,
eventLink: `https://vuevixens.org/${event.link.cached_url}`,
type: 'workshop',
tag: 'vuevixens',
location: event.location,
}));
}
function getEvents(asyncEvents) {
asyncEvents.forEach((event) => {
addEvent(event)
});
}
function readEventsFromFile(staticEventsData) {
for (const fileName of staticEventsData) {
const year = getYearFromFile(fileName);
const fileNamePath = `./docs/.vuepress/data/${fileName}`;
const data = fs.readFileSync(fileNamePath);
eventsTimeline[year] = JSON.parse(data);
}
}
function writeEventsToFile(staticEventsData) {
for (const fileName of staticEventsData) {
const year = getYearFromFile(fileName);
const fileNamePath = `./docs/.vuepress/data/${fileName}`;
fs.writeFile(fileNamePath, JSON.stringify(eventsTimeline[year], null, 2), () => {});
}
}
async function main() {
const staticEventsData = fs.readdirSync('./docs/.vuepress/data/').filter((fileName) => /.+\.json$/.test(fileName));
console.log('Generating events...')
let asyncEvents = await getVueVixensEvents();
readEventsFromFile(staticEventsData);
removeVueVixensEvents(staticEventsData);
getEvents(asyncEvents);
writeEventsToFile(staticEventsData);
console.log('All events saved to JSON');
}
main();