Skip to content

Commit

Permalink
Fix --token support
Browse files Browse the repository at this point in the history
Closes #499
Closes #474
Closes #484
  • Loading branch information
matheuss committed May 5, 2017
1 parent b0593c1 commit 8b306e6
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 42 deletions.
4 changes: 2 additions & 2 deletions bin/now-alias.js
Expand Up @@ -104,11 +104,11 @@ if (argv.help) {
exit(0)
} else {
Promise.resolve().then(async () => {
const config = await cfg.read()
const config = await cfg.read({token: argv.token})

let token
try {
token = argv.token || config.token || (await login(apiUrl))
token = config.token || (await login(apiUrl))
} catch (err) {
error(`Authentication error – ${err.message}`)
exit(1)
Expand Down
4 changes: 2 additions & 2 deletions bin/now-billing.js
Expand Up @@ -90,11 +90,11 @@ if (argv.help || !subcommand) {
exit(0)
} else {
Promise.resolve().then(async () => {
const config = await cfg.read()
const config = await cfg.read({token: argv.token})

let token
try {
token = argv.token || config.token || (await login(apiUrl))
token = config.token || (await login(apiUrl))
} catch (err) {
error(`Authentication error – ${err.message}`)
exit(1)
Expand Down
4 changes: 2 additions & 2 deletions bin/now-certs.js
Expand Up @@ -83,11 +83,11 @@ if (argv.help || !subcommand) {
exit(0)
} else {
Promise.resolve().then(async () => {
const config = await cfg.read()
const config = await cfg.read({token: argv.token})

let token
try {
token = argv.token || config.token || (await login(apiUrl))
token = config.token || (await login(apiUrl))
} catch (err) {
error(`Authentication error – ${err.message}`)
exit(1)
Expand Down
4 changes: 2 additions & 2 deletions bin/now-deploy.js
Expand Up @@ -200,7 +200,7 @@ if (deploymentName || wantsPublic) {
let alwaysForwardNpm

Promise.resolve().then(async () => {
let config = await cfg.read()
let config = await cfg.read({token: argv.token})
alwaysForwardNpm = config.forwardNpm

if (argv.h || argv.help) {
Expand All @@ -209,7 +209,7 @@ Promise.resolve().then(async () => {
} else if (argv.v || argv.version) {
console.log(version)
process.exit(0)
} else if (!(argv.token || config.token) || shouldLogin) {
} else if (!config.token || shouldLogin) {
let token
try {
token = await login(apiUrl)
Expand Down
4 changes: 2 additions & 2 deletions bin/now-dns.js
Expand Up @@ -77,11 +77,11 @@ if (argv.help || !subcommand) {
exit(0)
} else {
Promise.resolve().then(async () => {
const config = await cfg.read()
const config = await cfg.read({token: argv.token})

let token
try {
token = argv.token || config.token || (await login(apiUrl))
token = config.token || (await login(apiUrl))
} catch (err) {
error(`Authentication error – ${err.message}`)
exit(1)
Expand Down
4 changes: 2 additions & 2 deletions bin/now-domains.js
Expand Up @@ -119,11 +119,11 @@ if (argv.help || !subcommand) {
exit(0)
} else {
Promise.resolve().then(async () => {
const config = await cfg.read()
const config = await cfg.read({token: argv.token})

let token
try {
token = argv.token || config.token || (await login(apiUrl))
token = config.token || (await login(apiUrl))
} catch (err) {
error(`Authentication error – ${err.message}`)
exit(1)
Expand Down
4 changes: 2 additions & 2 deletions bin/now-list.js
Expand Up @@ -68,11 +68,11 @@ if (argv.config) {
}

Promise.resolve().then(async () => {
const config = await cfg.read()
const config = await cfg.read({token: argv.token})

let token
try {
token = argv.token || config.token || (await login(apiUrl))
token = config.token || (await login(apiUrl))
} catch (err) {
error(`Authentication error – ${err.message}`)
process.exit(1)
Expand Down
4 changes: 2 additions & 2 deletions bin/now-logs.js
Expand Up @@ -98,11 +98,11 @@ if (maybeURL(deploymentIdOrURL)) {

Promise.resolve()
.then(async () => {
const config = await cfg.read()
const config = await cfg.read({token: argv.token})

let token
try {
token = argv.token || config.token || login(apiUrl)
token = config.token || login(apiUrl)
} catch (err) {
error(`Authentication error – ${err.message}`)
process.exit(1)
Expand Down
4 changes: 2 additions & 2 deletions bin/now-open.js
Expand Up @@ -62,11 +62,11 @@ if (argv.config) {
}

Promise.resolve().then(async () => {
const config = await cfg.read()
const config = await cfg.read({token: argv.token})

let token
try {
token = argv.token || config.token || (await login(apiUrl))
token = config.token || (await login(apiUrl))
} catch (err) {
error(`Authentication error – ${err.message}`)
process.exit(1)
Expand Down
4 changes: 2 additions & 2 deletions bin/now-remove.js
Expand Up @@ -77,11 +77,11 @@ if (argv.config) {
}

Promise.resolve().then(async () => {
const config = await cfg.read()
const config = await cfg.read({token: argv.token})

let token
try {
token = (await argv.token) || config.token || login(apiUrl)
token = config.token || login(apiUrl)
} catch (err) {
error(`Authentication error – ${err.message}`)
process.exit(1)
Expand Down
4 changes: 2 additions & 2 deletions bin/now-scale.js
Expand Up @@ -81,11 +81,11 @@ if (argv.help) {
exit(0)
} else {
Promise.resolve().then(async () => {
const config = await cfg.read()
const config = await cfg.read({token: argv.token})

let token
try {
token = argv.token || config.token || (await login(apiUrl))
token = config.token || (await login(apiUrl))
} catch (err) {
error(`Authentication error – ${err.message}`)
exit(1)
Expand Down
4 changes: 2 additions & 2 deletions bin/now-secrets.js
Expand Up @@ -87,11 +87,11 @@ if (argv.help || !subcommand) {
exit(0)
} else {
Promise.resolve().then(async () => {
const config = await cfg.read()
const config = await cfg.read({token: argv.token})

let token
try {
token = argv.token || config.token || (await login(apiUrl))
token = config.token || (await login(apiUrl))
} catch (err) {
error(`Authentication error – ${err.message}`)
exit(1)
Expand Down
10 changes: 5 additions & 5 deletions bin/now-teams.js
Expand Up @@ -85,11 +85,11 @@ if (argv.help || !subcommand) {
exit(0)
} else {
Promise.resolve().then(async () => {
const config = await cfg.read()
const config = await cfg.read({token: argv.token})

let token
try {
token = argv.token || config.token || (await login(apiUrl))
token = config.token || (await login(apiUrl))
} catch (err) {
error(`Authentication error – ${err.message}`)
exit(1)
Expand All @@ -115,17 +115,17 @@ async function run({ token, config: { currentTeam } }) {
switch (subcommand) {
case 'switch':
case 'change': {
await require(resolve(__dirname, 'teams', 'switch.js'))(teams, args)
await require(resolve(__dirname, 'teams', 'switch.js'))({teams, args, token})
break
}
case 'add':
case 'create': {
await require(resolve(__dirname, 'teams', 'add.js'))(teams)
await require(resolve(__dirname, 'teams', 'add.js'))({teams, token})
break
}

case 'invite': {
await require(resolve(__dirname, 'teams', 'invite.js'))(teams, args)
await require(resolve(__dirname, 'teams', 'invite.js'))({teams, args, token})
break
}

Expand Down
4 changes: 2 additions & 2 deletions bin/now-upgrade.js
Expand Up @@ -80,11 +80,11 @@ if (argv.help) {
exit(0)
} else {
Promise.resolve().then(async () => {
const config = await cfg.read()
const config = await cfg.read({token: argv.token})

let token
try {
token = argv.token || config.token || (await login(apiUrl))
token = config.token || (await login(apiUrl))
} catch (err) {
error(`Authentication error – ${err.message}`)
exit(1)
Expand Down
7 changes: 5 additions & 2 deletions bin/teams/add.js
Expand Up @@ -34,7 +34,7 @@ function gracefulExit() {
const teamUrlPrefix = rightPad('Team URL', 14) + chalk.gray('zeit.co/')
const teamNamePrefix = rightPad('Team Name', 14)

module.exports = async function(teams) {
module.exports = async function({teams, token}) {
let slug
let team
let elapsed
Expand Down Expand Up @@ -118,7 +118,10 @@ module.exports = async function(teams) {
await cfg.merge({ currentTeam: team })
stopSpinner()

await require('./invite')(teams, [], {
await require('./invite')({
teams,
args: [],
token,
introMsg: 'Invite your team mates! When done, press enter on an empty field',
noopMsg: `You can invite team mates later by running ${cmd('now teams invite')}`
})
Expand Down
9 changes: 5 additions & 4 deletions bin/teams/invite.js
Expand Up @@ -60,12 +60,13 @@ function emailAutoComplete(value, teamSlug) {
return false
}

module.exports = async function(
module.exports = async function({
teams,
args,
{ introMsg, noopMsg = 'No changes made' } = {}
) {
const { user, currentTeam } = await cfg.read()
token,
introMsg, noopMsg = 'No changes made'
} = {}) {
const { user, currentTeam } = await cfg.read({token})

domains.push(user.email.split('@')[1])

Expand Down
4 changes: 2 additions & 2 deletions bin/teams/switch.js
Expand Up @@ -15,10 +15,10 @@ async function updateCurrentTeam({ cfg, newTeam } = {}) {
await cfg.merge({ currentTeam: newTeam })
}

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

Expand Down
8 changes: 5 additions & 3 deletions lib/cfg.js
Expand Up @@ -39,13 +39,15 @@ function save(data) {
* @return {Object}
*/
async function read({ force = false, token, apiUrl } = {}) {
let existing = null
let existing = {}
try {
existing = fs.readFileSync(file, 'utf8')
existing = JSON.parse(existing)
} catch (err) {}

if (!existing && force && token) {
// Will happen if `force`d or if `--token` is used and it's different from
// The one that's stored (which can be `undefined`)
if ((force && token) || (token && token !== existing.token) ) {
const user = await getUser({ token, apiUrl })
if (user) {
return {
Expand All @@ -60,7 +62,7 @@ async function read({ force = false, token, apiUrl } = {}) {
return {}
}

if (!existing) {
if (!existing.token) {
return {}
}

Expand Down

0 comments on commit 8b306e6

Please sign in to comment.