Created
August 9, 2018 04:45
-
-
Save Tolsee/8f0ca8400ca1381567e0a66a51a035a0 to your computer and use it in GitHub Desktop.
Create google events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gEvent | |
.command('create [summary...]') | |
.option('-s, --start [date]', 'Set start date[chrono based date parsing]') | |
.option('-e, --end [date]', 'Set end date[chrono based date parsing]') | |
.option('-l, --location [eventLocation]', 'Set location of the event') | |
.option('--notify', 'Notify me about the event') | |
.validate(function (args) { | |
if (!args.options.start) { | |
return 'start date is missing'; | |
} else if (!args.options.end) { | |
return 'end date is missing'; | |
} else { | |
return true; | |
} | |
}) | |
.action(async function (args, callback) { | |
const calender = google.calendar({ | |
version: 'v3', | |
auth: oauth2Client | |
}); | |
let event = { | |
summary: args.summary.join(' ') | |
}; | |
let time = chrono.parseDate(args.options.start); | |
if (time) { | |
event.start = { dateTime: time.toISOString() }; | |
} else { | |
this.log(Yellow('Warning: cannot parse \'start\' date')); | |
event.start = { dateTime: (new Date()).toISOString() }; | |
} | |
time = chrono.parseDate(args.options.end); | |
if (time) { | |
event.end = { dateTime: time.toISOString() }; | |
} else { | |
this.log(Yellow('Warning: cannot parse \'end\' date')); | |
event.end = { dateTime: (new Date()).toISOString() }; | |
} | |
if(args.options.notify) { | |
event.sendNotifications = true; | |
} | |
if(args.options.location) { | |
event.location = args.options.location; | |
} | |
const options = { | |
calendarId: 'primary', | |
resource: event | |
}; | |
const res = await calender.events.insert(options); | |
const { summary, start, end, location, htmlLink } = res.data; | |
this.log(Summary(summary)); | |
this.log(); | |
const startTime = moment(start.dateTime).format('MMMM Do YYYY, h:mm:ss a'); | |
const endTime = moment(end.dateTime).format('MMMM Do YYYY, h:mm:ss a'); | |
this.log(Time('Start Time: '), startTime, ' ', Time('Start Time: '), endTime); | |
this.log(Location(location || 'Location is not available.')); | |
this.log(htmlLink); | |
this.log(); | |
this.log(); | |
callback(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment