UNPKG

@swell/cli

Version:

Swell's command line interface/utility

46 lines (45 loc) 1.68 kB
import { confirm } from '@inquirer/prompts'; import { Flags } from '@oclif/core'; import { inflect } from 'inflection'; import config from '../lib/config.js'; import style from '../lib/style.js'; import { SwellCommand } from '../swell-command.js'; export default class Logout extends SwellCommand { static description = 'Log out of all stores available in your current session.'; static flags = { local: Flags.boolean({ description: 'clear local authentication data', }), }; async run() { const { flags } = await this.parse(Logout); const user = config.getUser(); const storesCount = (config.get('stores') || []).length; if (!config.getDefaultStore()) { config.clear(); this.log('You are not logged in.'); return; } const continueLogout = await confirm({ message: `You are logged in as ${style.currentUser(user.email)} ${style.dim(`(${storesCount} ${inflect('stores', storesCount)})`)}. Are you sure you want to log out?`, }); if (!continueLogout) { return; } if (flags.local) { config.clear(); this.log('Cleared local authentication data.'); return; } // logout from remote const response = await this.api.post({ adminPath: 'user/logout' }, {}); if (response === null || response.success) { // clear locally stored config config.clear(); this.log(`Logged out from ${style.currentUser(user.email)}.`); } else { this.logError('Logout failed.'); } } }