UNPKG

now-flow

Version:

Add deployment workflows to Zeit now

166 lines (129 loc) 3.23 kB
#!/usr/bin/env node // Packages const mri = require('mri') const chalk = require('chalk') const fetch = require('node-fetch') const ora = require('ora') // Utilities const logo = require('../../../util/output/logo') const { handleError } = require('../util/error') const { readConfigFile, writeToConfigFile, readAuthConfigFile, writeToAuthConfigFile } = require('../../../util/config-files') const error = require('../../../util/output/error') const exit = require('../../../util/exit') /*eslint-disable */ const getProcess = () => process /*eslint-enable */ const help = () => { console.log(` ${chalk.bold(`${logo} now logout`)} ${chalk.dim('Options:')} -h, --help Output usage information -A ${chalk.bold.underline('FILE')}, --local-config=${chalk.bold.underline( 'FILE' )} Path to the local ${'`now.json`'} file -Q ${chalk.bold.underline('DIR')}, --global-config=${chalk.bold.underline( 'DIR' )} Path to the global ${'`.now`'} directory ${chalk.dim('Examples:')} ${chalk.gray('–')} Logout from the CLI: ${chalk.cyan('$ now logout')} `) } let argv let apiUrl let endpoint const main = async ctx => { argv = mri(ctx.argv.slice(2), { boolean: ['help'], alias: { help: 'h' } }) apiUrl = ctx.apiUrl endpoint = apiUrl + '/user/tokens/' argv._ = argv._.slice(1) if (argv.help || argv._[0] === 'help') { help() await exit(0) } logout() } module.exports = async ctx => { try { await main(ctx) } catch (err) { handleError(err) getProcess().exit(1) } } const requestHeaders = token => ({ headers: { Authorization: `bearer ${token}` } }) const getTokenId = async token => { const result = await fetch(endpoint, requestHeaders(token)) const tokenList = await result.json() if (!tokenList.tokens) { return } const tokenInfo = tokenList.tokens.find(t => token === t.token) if (!tokenInfo) { return } return tokenInfo.id } const revokeToken = async (token, tokenId) => { const details = { method: 'DELETE' } Object.assign(details, requestHeaders(token)) const result = await fetch(endpoint + encodeURIComponent(tokenId), details) if (!result.ok) { console.error(error('Not able to log out')) } } const logout = async () => { const spinner = ora({ text: 'Logging out...' }).start() const configContent = readConfigFile() if (configContent.sh) { delete configContent.sh } const authContent = 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 writeToConfigFile(configContent) await writeToAuthConfigFile(authContent) } catch (err) { spinner.fail('Couldn\'t remove config while logging out') getProcess().exit(1) } let tokenId try { tokenId = await getTokenId(related.token) } catch (err) { spinner.fail('Not able to get token id on logout') getProcess().exit(1) } if (!tokenId) { return } try { await revokeToken(related.token, tokenId) } catch (err) { spinner.fail('Could not revoke token on logout') getProcess().exit(1) } spinner.succeed('Logged out!') }