forked from lando/pantheon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (48 loc) · 1.94 KB
/
index.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
'use strict';
const _ = require('lodash');
const PantheonApi = require('./lib/client');
const removeInvalidGlobalTokens = (lando, cachedToken) => {
const globalTokens = lando.cache.get('pantheon.tokens');
const filteredTokens = _(globalTokens)
.filter(token => token.token !== cachedToken)
.value();
lando.cache.set('pantheon.tokens', filteredTokens, {persist: true});
};
const clearInvalidTokenFromToolingCache = (lando, cacheKey, meta, data) => {
lando.cache.set(cacheKey, _.omit(meta, ['token']), {persist: true});
lando.cache.remove(_.get(data, 'options._app.toolingCache'));
lando.cli.clearTaskCaches();
};
const errorIs401 = error => error.message.includes('failed with code 401');
const checkTokens = (lando, command) => {
lando.events.on(`cli-${command}-answers`, data => {
if (_.get(data, 'options._app.recipe') === 'pantheon') {
// Gather the things we need
const cacheKey = _.get(data, 'options._app.metaCache');
const meta = lando.cache.get(cacheKey);
const cachedToken = _.get(meta, 'token', null);
if (!_.isEmpty(cachedToken)) {
// Make sure our token is good beforehand
return new PantheonApi(cachedToken, lando.log).auth().catch(error => {
if (errorIs401(error)) {
// Remove the bad token from the global token cache.
removeInvalidGlobalTokens(lando, cachedToken);
// Set or things correctly for downstream
clearInvalidTokenFromToolingCache(lando, cacheKey, meta, data);
// This is the money option
delete data.options.auth;
console.log(lando.cli.makeArt('badToken'));
}
});
}
}
});
};
module.exports = lando => {
// Add additional things to cleanse
lando.log.alsoSanitize('pantheon-auth');
// Attempt to remove a revoked or bad app token so the user is prompted
_.forEach(['pull', 'push', 'switch'], command => {
checkTokens(lando, command);
});
};