-
Notifications
You must be signed in to change notification settings - Fork 5
/
createIcs.js
125 lines (96 loc) · 3.29 KB
/
createIcs.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import fs from 'fs';
import YAML from 'yamljs';
import icalToolkit from 'ical-toolkit';
export default function (opt) {
var defaultData = {
calname: 'F2ETW 好聚好善! Seek Meetups',
title: '',
start: new Date().toJSON(),
end: new Date().toJSON(),
location: '',
url: '',
info: '',
org: {
name: '',
url: ''
}
};
var eventData = Object.assign(defaultData, YAML.load(opt.yaml));
// Create a builder
var builder = icalToolkit.createIcsFileBuilder();
/*
* Settings (All Default values shown below. It is optional to specify)
* */
builder.spacers = true; // Add space in ICS file, better human reading. Default: true
builder.NEWLINE_CHAR = '\r\n'; // Newline char to use.
builder.throwError = false; // If true throws errors, else returns error when you do .toString() to generate the file contents.
builder.ignoreTZIDMismatch = true; // If TZID is invalid, ignore or not to ignore!
/**
* Build ICS
* */
// Name of calander 'X-WR-CALNAME' tag.
builder.calname = eventData.calname;
// Cal timezone 'X-WR-TIMEZONE' tag. Optional. We recommend it to be same as tzid.
builder.timezone = 'Asia/Taipei';
// Time Zone ID. This will automatically add VTIMEZONE info.
builder.tzid = 'Asia/Taipei';
// Method
builder.method = 'REQUEST';
var eventConfig = (function () {
var timezoneOffset = new Date().getTimezoneOffset() * 60 * 1000; // s
var confObj = {
// Event start time, Required: type Date()
start: new Date(+new Date(eventData.start) + timezoneOffset),
// Event end time, Required: type Date()
end: new Date(+new Date(eventData.end) + timezoneOffset),
// transp. Will add TRANSP:OPAQUE to block calendar.
transp: 'OPAQUE',
// Event summary, Required: type String
summary: eventData.title,
// Location of event, optional.
location: eventData.location,
// Optional description of event.
description: eventData.url + '\n' + eventData.info,
// What to do on addition
method: 'PUBLISH',
// Status of event
status: 'CONFIRMED',
// Url for event on core application, Optional.
url: eventData.url
};
if (eventData.repeating) {
confObj.repeating = eventData.repeating;
}
// Optional if repeating event
// repeating: {
// freq: 'WEEKLY',
// count: 2,
// interval: 1
// },
// // Optional if all day event
// allDay: true,
// // Creation timestamp, Optional.
// stamp: new Date(),
// // Optional, floating time.
// floating: false,
return confObj;
})();
// Add events
builder.events.push(eventConfig);
// Optional tags on VCALENDAR level if you intent to add. Optional field
// builder.additionalTags = {
// 'SOMETAG': 'SOME VALUE'
// };
// Try to build
var icsFileContent = builder.toString();
// Check if there was an error (Only required if yu configured to return error, else error will be thrown.)
if (icsFileContent instanceof Error) {
console.log('Returned Error, you can also configure to throw errors!');
// handle error
} else {
let fileName = opt.yaml.replace(/^events\/(.+)$/, './ics/$1.ics');
fs.writeFile(fileName, icsFileContent);
}
// Here isteh ics file content.
// console.log(icsFileContent);
}