1 | const Command = require('../utils/command')
|
2 | const chalk = require('chalk')
|
3 | const inquirer = require('inquirer')
|
4 | const LoginCommand = require('./login')
|
5 |
|
6 | class SwitchCommand extends Command {
|
7 | async run() {
|
8 | const LOGIN_NEW = 'I would like to login to a new account'
|
9 | const availableUsersChoices = Object.values(this.netlify.globalConfig.get('users')).reduce(
|
10 | (prev, current) =>
|
11 | Object.assign(prev, { [current.id]: current.name ? `${current.name} (${current.email})` : current.email }),
|
12 | {}
|
13 | )
|
14 |
|
15 | await this.config.runHook('analytics', {
|
16 | eventName: 'command',
|
17 | payload: {
|
18 | command: 'switch'
|
19 | }
|
20 | })
|
21 |
|
22 | const { accountSwitchChoice } = await inquirer.prompt([
|
23 | {
|
24 | type: 'list',
|
25 | name: 'accountSwitchChoice',
|
26 | message: 'Please select the account you want to use:',
|
27 | choices: [...Object.entries(availableUsersChoices).map(([, val]) => val), LOGIN_NEW]
|
28 | }
|
29 | ])
|
30 |
|
31 | if (accountSwitchChoice === LOGIN_NEW) {
|
32 | await LoginCommand.run(['--new'])
|
33 | } else {
|
34 | const selectedAccount = Object.entries(availableUsersChoices).find(([k, v]) => v === accountSwitchChoice)
|
35 | this.netlify.globalConfig.set('userId', selectedAccount[0])
|
36 | this.log('')
|
37 | this.log(`You're now using ${chalk.bold(selectedAccount[1])}.`)
|
38 | }
|
39 |
|
40 | return this.exit()
|
41 | }
|
42 | }
|
43 |
|
44 | SwitchCommand.description = `Switch your active Netlify account`
|
45 |
|
46 | module.exports = SwitchCommand
|