forked from netlify/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch.js
More file actions
43 lines (36 loc) · 1.41 KB
/
switch.js
File metadata and controls
43 lines (36 loc) · 1.41 KB
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
const Command = require('@netlify/cli-utils')
const chalk = require('chalk')
const inquirer = require('inquirer')
const LoginCommand = require('./login')
class SwitchCommand extends Command {
async run() {
const LOGIN_NEW = 'I would like to login to a new account'
const availableUsersChoices = Object.values(this.netlify.globalConfig.get('users')).reduce(
(prev, current) => Object.assign(prev, { [current.id]: current.name ? `${current.name} (${current.email})` : current.email }), {})
await this.config.runHook('analytics', {
eventName: 'command',
payload: {
command: 'switch',
},
});
const { accountSwitchChoice } = await inquirer.prompt([
{
type: 'list',
name: 'accountSwitchChoice',
message: 'Please select the account you want to use:',
choices: [...Object.entries(availableUsersChoices).map(([, val]) => val), LOGIN_NEW],
},
])
if (accountSwitchChoice === LOGIN_NEW) {
await LoginCommand.run(['--new'])
} else {
const selectedAccount = Object.entries(availableUsersChoices).find(([k, v]) => v === accountSwitchChoice)
this.netlify.globalConfig.set('userId', selectedAccount[0])
this.log('')
this.log(`You're now using ${chalk.bold(selectedAccount[1])}.`)
}
return this.exit()
}
}
SwitchCommand.description = `Switch your active Netlify account`
module.exports = SwitchCommand