/
now-teams.js
142 lines (111 loc) · 3.24 KB
/
now-teams.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env node
// Native
const { resolve } = require('path')
// Packages
const chalk = require('chalk')
const minimist = require('minimist')
// Ours
const login = require('../lib/login')
const cfg = require('../lib/cfg')
const error = require('../lib/utils/output/error')
const NowTeams = require('../lib/teams')
const logo = require('../lib/utils/output/logo')
const exit = require('../lib/utils/exit')
const argv = minimist(process.argv.slice(2), {
string: ['config', 'token'],
boolean: ['help', 'debug'],
alias: {
help: 'h',
config: 'c',
debug: 'd',
token: 't',
switch: 'change'
}
})
const subcommand = argv._[0]
const help = () => {
console.log(
`
${chalk.bold(`${logo} now teams`)} <add | ls | rm | invite>
${chalk.dim('Options:')}
-h, --help Output usage information
-c ${chalk.bold.underline('FILE')}, --config=${chalk.bold.underline('FILE')} Config file
-d, --debug Debug mode [off]
-t ${chalk.bold.underline('TOKEN')}, --token=${chalk.bold.underline('TOKEN')} Login token
${chalk.dim('Examples:')}
${chalk.gray('–')} Add a new team:
${chalk.cyan('$ now teams add')}
${chalk.gray('–')} Switch to a team:
${chalk.cyan(`$ now switch <id>`)}
${chalk.gray('–')} If the id is omitted, you can choose interactively
${chalk.yellow('NOTE:')} When you switch, everything you add, list or remove will be scoped that team!
${chalk.gray('–')} Invite new members (interactively):
${chalk.cyan(`$ now teams invite`)}
${chalk.gray('–')} Invite a specific email:
${chalk.cyan(`$ now teams invite geist@zeit.co`)}
${chalk.gray('–')} Remove a team:
${chalk.cyan(`$ now teams rm <id>`)}
${chalk.gray('–')} If the id is omitted, you can choose interactively
`
)
}
// Options
const debug = argv.debug
const apiUrl = argv.url || 'https://api.zeit.co'
if (argv.config) {
cfg.setConfigFile(argv.config)
}
if (argv.help || !subcommand) {
help()
exit(0)
} else {
Promise.resolve().then(async () => {
const config = await cfg.read({token: argv.token})
let token
try {
token = config.token || (await login(apiUrl))
} catch (err) {
error(`Authentication error – ${err.message}`)
exit(1)
}
try {
await run({ token, config })
} catch (err) {
if (err.userError) {
error(err.message)
} else {
error(`Unknown error: ${err.stack}`)
}
exit(1)
}
})
}
async function run({ token, config: { currentTeam } }) {
const teams = new NowTeams({ apiUrl, token, debug, currentTeam })
const args = argv._.slice(1)
switch (subcommand) {
case 'switch':
case 'change': {
await require(resolve(__dirname, 'teams', 'switch.js'))({teams, args, token})
break
}
case 'add':
case 'create': {
await require(resolve(__dirname, 'teams', 'add.js'))({teams, token})
break
}
case 'invite': {
await require(resolve(__dirname, 'teams', 'invite.js'))({teams, args, token})
break
}
default: {
let code = 0
if (subcommand !== 'help') {
error('Please specify a valid subcommand: ls | add | rm | set-default')
code = 1
}
help()
exit(code)
}
}
}