From ce2fe449cdb1d390c19cb446a6b787af5aec858f Mon Sep 17 00:00:00 2001 From: Leo Lamprecht Date: Fri, 1 Sep 2017 20:25:54 +0200 Subject: [PATCH] Made loggin out from .sh work --- src/providers/sh/commands/bin/logout.js | 37 +++++++++++++++++-------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/providers/sh/commands/bin/logout.js b/src/providers/sh/commands/bin/logout.js index 36ad99b..5e6ffae 100644 --- a/src/providers/sh/commands/bin/logout.js +++ b/src/providers/sh/commands/bin/logout.js @@ -7,9 +7,14 @@ const fetch = require('node-fetch') const ora = require('ora') // Utilities -const cfg = require('../lib/cfg') const logo = require('../lib/utils/output/logo') const { handleError } = require('../lib/error') +const { + readConfigFile, + writeToConfigFile, + readAuthConfigFile, + writeToAuthConfigFile +} = require('../../../../util/config-files') const help = () => { console.log(` @@ -36,21 +41,15 @@ let endpoint const main = async ctx => { argv = minimist(ctx.argv.slice(2), { - string: ['config'], boolean: ['help'], alias: { - help: 'h', - config: 'c' + help: 'h' } }) apiUrl = argv.url || 'https://api.zeit.co' endpoint = apiUrl + '/www/user/tokens/' - if (argv.config) { - cfg.setConfigFile(argv.config) - } - if (argv.help) { help() process.exit(0) @@ -109,10 +108,24 @@ const logout = async () => { text: 'Logging out...' }).start() - const config = await cfg.read() + const configContent = JSON.parse(readConfigFile()) + + + if (configContent.sh) { + delete configContent.sh + } + + const authContent = JSON.parse(readAuthConfigFile()) + const {credentials} = authContent + const related = credentials.find(item => item.provider === 'sh') + const index = credentials.indexOf(related) + + credentials.splice(index, 1) + authContent.credentials = credentials try { - await cfg.removeFile() + await writeToConfigFile(configContent) + await writeToAuthConfigFile(authContent) } catch (err) { spinner.fail(`Couldn't remove config while logging out`) process.exit(1) @@ -121,7 +134,7 @@ const logout = async () => { let tokenId try { - tokenId = await getTokenId(argv.token || config.token) + tokenId = await getTokenId(argv.token || related.token) } catch (err) { spinner.fail('Not able to get token id on logout') process.exit(1) @@ -132,7 +145,7 @@ const logout = async () => { } try { - await revokeToken(argv.token || config.token, tokenId) + await revokeToken(argv.token || related.token, tokenId) } catch (err) { spinner.fail('Could not revoke token on logout') process.exit(1)