-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbuild.js
164 lines (143 loc) · 5.1 KB
/
build.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
const fs = require('fs');
const obj = require('./list.json');
const ical = require('ical-generator');
const countryList = require('country-list');
const cal = ical();
const year = 2020;
const items = [];
const duplicates = [];
let content = `# ${year} Engineering Conferences
A list of ${year} conferences.
A list of [${year - 1} conferences](https://github.com/ryanburgess/${year - 1}-conferences).
[2020 Conferences cancelled due to Covid-19](https://docs.google.com/spreadsheets/d/1pTxPrJXlUvWrIubfIuTHJJJW1eOyTXaq4M3ojZF9PRk/edit#gid=0).
_**You can also add all conferences directly into your calendar by importing the \`.ics\` file into Google Calendar etc.**_
_**The \`.ics\` file can be downloaded [here](https://rawgit.com/ryanburgess/2019-conferences/master/2019-conferences.ics), but it's recommended to add it via URL (if your client supports that). Thus, you will dynamically get all updates.**_
`;
// create contributing instructions
const contribute = `
## Contributing
1. Fork it
2. Create your feature branch (\`git checkout -b my-new-feature\`)
3. Add your conference to \`list.json\`
4. Run \`npm install\` to install local dependencies
5. Run \`npm run build\` to build the README and generate the .ics file
6. Commit your changes (\`git commit -am "Add some feature"\`)
7. Push to the branch (\`git push origin my-new-feature\`)
8. Create new Pull Request
`;
// messages
const messages = {
'success': {
'updated': 'Updated conference list'
},
'fail': {
'char': 'Must contain 5 characters. Format: mm-dd',
}
}
// month names for date function
const monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
// human readable date function
let humanDate = ( from, to ) => {
// to mm-dd
let dayMonthTo = '', dayTo, toMonthName;
if( to ) {
let toArr = to.split('-');
let eventTo = new Date( year, ( toArr[0] - 1 ), toArr[1] );
let toMonthIndex = eventTo.getMonth();
dayTo = eventTo.getDate();
toMonthName = monthNames[toMonthIndex];
dayMonthTo = ` - ${dayTo} ${toMonthName}`;
}
// from mm-dd
let fromArr = from.split('-');
let eventFrom = new Date( year, ( fromArr[0] - 1 ), fromArr[1] );
let fromDay = eventFrom.getDate();
let fromMonthIndex = eventFrom.getMonth();
let fromMonthName = monthNames[fromMonthIndex];
// default return
const defaultReturn = `${fromDay} ${fromMonthName}${dayMonthTo}, ${year}`;
// if same month, use single month name
if( to ) {
if( fromMonthName === toMonthName ) {
return `${fromDay} - ${dayTo} ${fromMonthName}, ${year}`;
} else {
return defaultReturn;
}
} else {
return defaultReturn;
}
}
// sort object by dateFrom
obj.sort(function(a, b) {
let aFromArr = a.dateFrom.split('-');
let bFromArr = b.dateFrom.split('-');
a = aFromArr[0] + aFromArr[1];
b = bFromArr[0] + bFromArr[1];
return a - b;
});
// rows
const rows = [];
// create heading for conference list
content += `
# Conference List
`;
Array.prototype.contains = function(obj) {
let i = this.length;
while (i--) {
if (this[i] == obj) {
return true;
}
}
return false;
}
// create list of conferences
for (const conference of obj) {
if(!items.contains(conference.title)){
items.push(conference.title);
const country = String(conference.country).trim();
const code = String(countryList.getCode(country.replace('USA', 'United States')) || country).toLowerCase();
const flag = code.length === 2 ? `<img src="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.2.1/flags/4x3/${code}.svg" height="16" alt="${conference.country} flag icon" />` : '';
if( conference.dateFrom.length !== 5 ) process.exit( console.log(`${conference.title} - dateFrom: ${messages.fail.char}`) );
if( conference.dateTo.length !== 0 && conference.dateTo.length !== 5 ) process.exit( console.log(`${conference.title} - dateTo: ${messages.fail.char}`) );
let humanReadableDate = humanDate( `${conference.dateFrom}`, `${conference.dateTo}` );
rows.push([
`[${conference.title}](${conference.url})`,
humanReadableDate,
`${flag} ${conference.where}`,
]);
}
}
content += `
| Conference | Date | Where |
|------------|------|-------|
`;
content += rows.map(cols => `| ${cols.join(' | ')} |`).join('\n');
content += '\n';
// add contribute information after list of conferences
content += contribute;
// create README with the list of conferences
fs.writeFile('./README.md', content, function (err) {
if (err) throw err;
console.log( messages.success.updated );
});
// create ical file
obj.forEach(event => {
cal.createEvent({
start: new Date(`${event.dateFrom}-${year}`),
end: new Date(`${event.dateTo ? event.dateTo : event.dateFrom}-${year}`),
summary: event.title,
description: event.url,
location: event.where,
});
});
const outputCal = cal.toString();
const outputFile = `${year}-conferences.ics`;
fs.writeFile(outputFile, outputCal, (err) => {
console.log(err ? err : `Exported all ${year} conferences into ${outputFile}. This file can be imported from any calendar like Google Calendar.`)
});