This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
/
generateCalendar.js
67 lines (55 loc) · 1.82 KB
/
generateCalendar.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
'use strict';
const fs = require('fs');
const path = require('path');
const pify = require('pify');
const ical = require('ical-generator');
const readEvents = require('./helpers/readEvents');
const PATH_EVENTS = path.resolve(__dirname, '../events');
const PATH_DIST = path.resolve(__dirname, '../dist');
const PATH_ICAL_CALENDAR = path.resolve(PATH_DIST, 'calendar.ics');
const PATH_JSON_CALENDAR = path.resolve(PATH_DIST, 'calendar.json');
if (!fs.existsSync(PATH_DIST)){
fs.mkdirSync(PATH_DIST);
}
const writeFile = pify(fs.writeFile);
function prepareEvent(event) {
const data = {
uid: event.uid,
start: event.dateStart,
end: event.dateEnd,
summary: event.online ? `${event.name} (онлайн)` : event.name,
location: event.city,
description: event.link,
};
if (!event.withTime) {
data.allDay = true;
}
return data;
}
function generateICalCalendar(events) {
const cal = ical({
description: 'Конференции, митапы и другие события по фронтенду.',
domain: 'https://web-standards.ru/',
name: 'Фронтенд',
prodId: {
company: 'web-standards',
product: 'calendar',
language: 'RU'
},
events: events
});
return writeFile(PATH_ICAL_CALENDAR, cal.toString());
}
function generateJSONCalendar(events) {
return writeFile(PATH_JSON_CALENDAR, JSON.stringify(events));
}
function dropEventWithoutDateStart(event) {
return ('dateStart' in event);
}
readEvents(PATH_EVENTS)
.then(events => events.filter(dropEventWithoutDateStart).map(prepareEvent))
.then(data => Promise.all[generateICalCalendar(data), generateJSONCalendar(data)])
.catch((error) => {
console.error(error);
process.exit(1);
});