-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.js
63 lines (60 loc) · 1.9 KB
/
add.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
const fs = require('fs');
const prompt = require('prompt');
const events = fs.readFileSync('./events.json');
const fullList = JSON.parse(events);
// prompt schema
const schema = {
properties: {
event: {
description: 'Event title',
pattern: /([^\s]+)/g,
message: 'Name must be only letters, spaces, or dashes',
required: true
},
url: {
description: 'Event URL',
pattern: /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/,
message: 'Must be a valid URL',
required: true
},
when: {
description: 'Event dates (make sure to add the year)',
pattern: /([^\s]+)/g,
message: 'Name must be only letters, spaces, or dashes',
required: true
},
where: {
description: 'Event location',
pattern: /([^\s]+)/g,
message: 'Name must be only letters, spaces, or dashes',
required: true
},
who: {
description: 'Who is attending? (comma seperated)',
pattern: /([^\s]+)/g,
message: 'Name must be only letters, spaces, or dashes',
required: true
},
description: {
description: 'Description if we are attending or speaking',
pattern: /([^\s]+)/g,
message: 'Name must be only letters, spaces, or dashes',
required: true
}
}
};
// start prompt
prompt.start();
// prompt questions
prompt.get(schema, function (err, result) {
const names = [result.who];
// pull the year from date
const year = result.when.substr(result.when.length - 4);
// create a new object
const obj = {'event': result.event, 'url': result.url, 'date': result.when, 'where': result.where, 'year': year, 'who': names, 'desc': result.description};
// push new object to the full list
fullList.push(obj);
// sync the updated list with events.json
fs.writeFileSync('./events.json', JSON.stringify(fullList, null, 4));
console.log('New event added!');
});