Skip to content

Commit

Permalink
Made team switching work again
Browse files Browse the repository at this point in the history
  • Loading branch information
leo committed Sep 2, 2017
1 parent 0586987 commit c3f9a0e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
9 changes: 5 additions & 4 deletions src/providers/sh/commands/teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ const main = async ctx => {
exit(0)
}

const {authConfig: { credentials }, config: { sh }} = ctx
const {authConfig: { credentials }, config} = ctx
const {token} = argv.token || credentials.find(item => item.provider === 'sh')

try {
await run({ token, sh })
await run({ token, config })
} catch (err) {
if (err.userError) {
error(err.message)
Expand All @@ -131,7 +131,8 @@ module.exports = async ctx => {
}
}

async function run({ token, sh: { currentTeam } }) {
async function run({ token, config }) {
const {currentTeam} = config.sh
const teams = new NowTeams({ apiUrl, token, debug, currentTeam })
const args = argv._

Expand All @@ -149,7 +150,7 @@ async function run({ token, sh: { currentTeam } }) {
await change({
teams,
args,
token
config
})
break
}
Expand Down
40 changes: 27 additions & 13 deletions src/providers/sh/commands/teams/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,33 @@ const chalk = require('chalk')
// Utilities
const wait = require('../../../../util/output/wait')
const listInput = require('../../../../util/input/list')
const cfg = require('../../util/cfg')
const exit = require('../../../../util/exit')
const success = require('../../../../util/output/success')
const info = require('../../../../util/output/info')
const error = require('../../../../util/output/error')
const param = require('../../../../util/output/param')
const {writeToConfigFile} = require('../../../../util/config-files')

async function updateCurrentTeam({ cfg, newTeam } = {}) {
delete newTeam.created
delete newTeam.creator_id
await cfg.merge({ currentTeam: newTeam })
const updateCurrentTeam = async (config, newTeam) => {
if (newTeam) {
delete newTeam.created
delete newTeam.creator_id

config.sh.currentTeam = newTeam
} else {
delete config.sh.currentTeam
}

writeToConfigFile(config)
}

module.exports = async function({ teams, args, token }) {
module.exports = async function({ teams, args, config }) {
let stopSpinner = wait('Fetching teams')
const list = (await teams.ls()).teams
let { user, currentTeam } = await cfg.read({ token })

let { user, currentTeam } = config.sh
const accountIsCurrent = !currentTeam

stopSpinner()

if (accountIsCurrent) {
Expand All @@ -32,19 +41,22 @@ module.exports = async function({ teams, args, token }) {

if (args.length !== 0) {
const desiredSlug = args[0]

const newTeam = list.find(team => team.slug === desiredSlug)

if (newTeam) {
await updateCurrentTeam({ cfg, newTeam })
await updateCurrentTeam(config, newTeam)
success(`The team ${chalk.bold(newTeam.name)} is now active!`)
return exit()
}

if (desiredSlug === user.username) {
stopSpinner = wait('Saving')
await cfg.remove('currentTeam')
await updateCurrentTeam(config)

stopSpinner()
return success(`Your account (${chalk.bold(desiredSlug)}) is now active!`)
}

error(`Could not find membership for team ${param(desiredSlug)}`)
return exit(1)
}
Expand Down Expand Up @@ -107,8 +119,10 @@ module.exports = async function({ teams, args, token }) {
info('No changes made')
return exit()
}

stopSpinner = wait('Saving')
await cfg.remove('currentTeam')
await updateCurrentTeam(config)

stopSpinner()
return success(`Your account (${chalk.bold(choice)}) is now active!`)
}
Expand All @@ -119,8 +133,8 @@ module.exports = async function({ teams, args, token }) {
}

stopSpinner = wait('Saving')
await updateCurrentTeam({ cfg, newTeam })
stopSpinner()
await updateCurrentTeam(config, newTeam)

stopSpinner()
success(`The team ${chalk.bold(newTeam.name)} is now active!`)
}

0 comments on commit c3f9a0e

Please sign in to comment.