-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cli] Refactor
vc switch
command (#6257)
Refactors the `vc switch` command to more closely match how the team picker works on the Vercel dashboard: * Converts to TypeScript * Adds separator between user scope and team scopes * Sorts teams by name * No longer places the current scope at the top of the list (but the current scope is still selected by default)
- Loading branch information
1 parent
711da37
commit 7b509ae
Showing
11 changed files
with
280 additions
and
308 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Packages | ||
import chalk from 'chalk'; | ||
|
||
// Utilities | ||
import Client from '../../util/client'; | ||
import listInput from '../../util/input/list'; | ||
import getUser from '../../util/get-user'; | ||
import getTeams from '../../util/get-teams'; | ||
import { Team, GlobalConfig } from '../../types'; | ||
import { writeToConfigFile } from '../../util/config/files'; | ||
|
||
const updateCurrentTeam = (config: GlobalConfig, team?: Team) => { | ||
if (team) { | ||
config.currentTeam = team.id; | ||
} else { | ||
delete config.currentTeam; | ||
} | ||
|
||
writeToConfigFile(config); | ||
}; | ||
|
||
export default async function main(client: Client, desiredSlug?: string) { | ||
const { config, output } = client; | ||
const personalScopeSelected = !config.currentTeam; | ||
|
||
output.spinner('Fetching teams information'); | ||
const [user, teams] = await Promise.all([getUser(client), getTeams(client)]); | ||
|
||
const currentTeam = personalScopeSelected | ||
? undefined | ||
: teams.find(team => team.id === config.currentTeam); | ||
|
||
if (!personalScopeSelected && !currentTeam) { | ||
output.error(`You are not a part of the current team anymore.`); | ||
return 1; | ||
} | ||
|
||
if (!desiredSlug) { | ||
const teamChoices = teams | ||
.slice(0) | ||
.sort((a, b) => { | ||
return a.name.toLowerCase().localeCompare(b.name.toLowerCase()); | ||
}) | ||
.map(({ id, slug, name }) => { | ||
let title = `${name} (${slug})`; | ||
const selected = id === currentTeam?.id; | ||
|
||
if (selected) { | ||
title += ` ${chalk.bold('(current)')}`; | ||
} | ||
|
||
return { | ||
name: title, | ||
value: slug, | ||
short: slug, | ||
selected, | ||
}; | ||
}); | ||
|
||
// Add the User scope entry at the top | ||
const suffix = personalScopeSelected ? ` ${chalk.bold('(current)')}` : ''; | ||
|
||
const choices = [ | ||
{ separator: 'Personal Account' }, | ||
{ | ||
name: `${user.email} (${user.username})${suffix}`, | ||
value: user.email, | ||
short: user.username, | ||
selected: personalScopeSelected, | ||
}, | ||
{ separator: 'Teams' }, | ||
...teamChoices, | ||
]; | ||
|
||
output.stopSpinner(); | ||
desiredSlug = await listInput({ | ||
message: 'Switch to:', | ||
choices, | ||
eraseFinalAnswer: true, | ||
}); | ||
} | ||
|
||
// Abort | ||
if (!desiredSlug) { | ||
output.log('No changes made.'); | ||
return 0; | ||
} | ||
|
||
if (desiredSlug === user.username || desiredSlug === user.email) { | ||
// Switch to user's personal account | ||
if (personalScopeSelected) { | ||
output.log('No changes made'); | ||
return 0; | ||
} | ||
|
||
updateCurrentTeam(config); | ||
|
||
output.success(`Your account (${chalk.bold(desiredSlug)}) is now active!`); | ||
return 0; | ||
} | ||
|
||
// Switch to selected team | ||
const newTeam = teams.find(team => team.slug === desiredSlug); | ||
|
||
if (!newTeam) { | ||
output.error( | ||
`You do not have permission to access scope ${chalk.bold(desiredSlug)}.` | ||
); | ||
return 1; | ||
} | ||
|
||
if (newTeam.slug === currentTeam?.slug) { | ||
output.log('No changes made'); | ||
return 0; | ||
} | ||
|
||
updateCurrentTeam(config, newTeam); | ||
|
||
output.success( | ||
`The team ${chalk.bold(newTeam.name)} (${newTeam.slug}) is now active!` | ||
); | ||
return 0; | ||
} |
Oops, something went wrong.