Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Google Calendar plugin to v1.1.0 #829

Merged
merged 2 commits into from
Aug 13, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update Google Calendar plugin to v1.2.0
  • Loading branch information
kodie committed Aug 11, 2017
commit 2a8ae3fd504da1120e1da25a002126a44aaa96b6
63 changes: 37 additions & 26 deletions Time/googlecal.30m.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/*
<bitbar.title>Google Calendar</bitbar.title>
<bitbar.version>v1.1.0</bitbar.version>
<bitbar.version>v1.2.0</bitbar.version>
<bitbar.author>Kodie Grantham</bitbar.author>
<bitbar.author.github>kodie</bitbar.author.github>
<bitbar.desc>Shows upcoming events from your Google Calendar - Be sure to read the installation instructions here: https://github.com/kodie/bitbar-googlecal</bitbar.desc>
Expand All @@ -13,7 +13,7 @@
<bitbar.abouturl>https://github.com/kodie/bitbar-googlecal</bitbar.abouturl>
*/

var ver = '1.1.0';
var ver = '1.2.0';

var defaults = {
clientId: '529707498278-prhql6kn67hevctqkt0qkgeha51bdhv7.apps.googleusercontent.com',
Expand All @@ -27,6 +27,7 @@ var defaults = {
days: 7,
eventColor: false,
eventFont: false,
eventLength: 80,
eventSize: false,
expandEvents: true,
limit: 25,
Expand Down Expand Up @@ -138,8 +139,12 @@ try {
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(cfg.clientId, cfg.clientSecret, cfg.clientRedirect);
} catch(e) {
console.log(`Run Installation | bash="cd ${dirPlugins} && npm install ${npmDeps} && node ${process.argv[1]} getNewToken"`);
footer();
if (process.env.BitBar) {
console.log(`Run Installation | bash="cd ${dirPlugins} && npm install ${npmDeps} && rm package-lock.json || true && node ${process.argv[1]} getNewToken"`);
footer();
} else {
console.log(e);
}
process.exit(1);
}

Expand Down Expand Up @@ -201,37 +206,20 @@ function getNewToken() {

function refreshToken(oauth2Client, cb) {
oauth2Client.refreshAccessToken(function(error, token) {
function refreshError(msg) {
fs.unlinkSync(cfg.tokenFile);

console.log('Error refreshing token: ' + msg);
console.log('You may need to re-authorize this plugin with Google.');

if (process.env.BitBar) {
console.log('Click here to view your currently authorized apps | href=https://myaccount.google.com/permissions');
} else {
console.log('Visit this URL to view your currently authorized apps: https://myaccount.google.com/permissions');
}

console.log('You will want to find this plugin and click \'Remove\'.');
console.log('After you have done that, refresh this plugin and run the setup process again.');

footer();
process.exit(1);
}

if (!error) {
oauth2Client.credentials = Object.assign({}, oauth2Client.credentials, token);

fs.writeFile(cfg.tokenFile, JSON.stringify(oauth2Client.credentials), function(error, data) {
if (!error) {
cb(oauth2Client);
} else {
refreshError(error.message);
footer([error.message]);
process.exit(1);
}
});
} else {
refreshError(error.message);
footer([error.message]);
process.exit(1);
}
});
}
Expand Down Expand Up @@ -347,6 +335,7 @@ function listEvents(oauth2Client) {

if (cfg.eventColor) { str += ` color=${cfg.eventColor}`; }
if (cfg.eventFont) { str += ` font=${cfg.eventFont}`; }
if (cfg.eventLength) { str += ` length=${cfg.eventLength}`; }
if (cfg.eventSize) { str += ` size=${cfg.eventSize}`; }
}

Expand Down Expand Up @@ -376,7 +365,29 @@ function run() {
var token = fs.readFileSync(cfg.tokenFile, 'utf8');
if (token) { oauth2Client.credentials = JSON.parse(token); }

if (!oauth2Client.credentials.refresh_token || new Date(Date.now()) > oauth2Client.credentials.expiry_date) {
if (!oauth2Client.credentials.refresh_token) {
fs.unlinkSync(cfg.tokenFile);

console.log('No refresh token set');
console.log('You need to re-authorize this plugin with Google');

if (process.env.BitBar) {
console.log('Click here to view your currently authorized apps | href=https://myaccount.google.com/permissions');
} else {
console.log('Visit this URL to view your currently authorized apps: https://myaccount.google.com/permissions');
}

console.log('You will want to find this plugin and click \'Remove\'');

if (process.env.BitBar) {
console.log(`After you have done that, click here to re-authorize this plugin | bash=${process.argv[1]} param1=getNewToken refresh=false terminal=true`);
} else {
console.log('After you have done that, refresh this plugin and run the setup process again');
}

footer();
process.exit(1);
} else if (new Date(Date.now()) > oauth2Client.credentials.expiry_date) {
refreshToken(oauth2Client, listEvents);
} else {
listEvents(oauth2Client);
Expand Down