From 8b306e657b30e5983eb08c1caa03eb8ec19fc65d Mon Sep 17 00:00:00 2001 From: Matheus Fernandes Date: Fri, 5 May 2017 12:04:09 -0700 Subject: [PATCH] Fix `--token` support Closes #499 Closes #474 Closes #484 --- bin/now-alias.js | 4 ++-- bin/now-billing.js | 4 ++-- bin/now-certs.js | 4 ++-- bin/now-deploy.js | 4 ++-- bin/now-dns.js | 4 ++-- bin/now-domains.js | 4 ++-- bin/now-list.js | 4 ++-- bin/now-logs.js | 4 ++-- bin/now-open.js | 4 ++-- bin/now-remove.js | 4 ++-- bin/now-scale.js | 4 ++-- bin/now-secrets.js | 4 ++-- bin/now-teams.js | 10 +++++----- bin/now-upgrade.js | 4 ++-- bin/teams/add.js | 7 +++++-- bin/teams/invite.js | 9 +++++---- bin/teams/switch.js | 4 ++-- lib/cfg.js | 8 +++++--- 18 files changed, 48 insertions(+), 42 deletions(-) diff --git a/bin/now-alias.js b/bin/now-alias.js index b3f0a3d..1a52786 100755 --- a/bin/now-alias.js +++ b/bin/now-alias.js @@ -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) diff --git a/bin/now-billing.js b/bin/now-billing.js index 0b42ab4..f09e36f 100644 --- a/bin/now-billing.js +++ b/bin/now-billing.js @@ -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) diff --git a/bin/now-certs.js b/bin/now-certs.js index 8feec74..8c9272b 100755 --- a/bin/now-certs.js +++ b/bin/now-certs.js @@ -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) diff --git a/bin/now-deploy.js b/bin/now-deploy.js index bb4ee46..1c45830 100755 --- a/bin/now-deploy.js +++ b/bin/now-deploy.js @@ -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) { @@ -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) diff --git a/bin/now-dns.js b/bin/now-dns.js index 4dfe099..3b1e92f 100755 --- a/bin/now-dns.js +++ b/bin/now-dns.js @@ -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) diff --git a/bin/now-domains.js b/bin/now-domains.js index 64ee0ac..f67fc83 100755 --- a/bin/now-domains.js +++ b/bin/now-domains.js @@ -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) diff --git a/bin/now-list.js b/bin/now-list.js index 2c9251f..ca60ab1 100755 --- a/bin/now-list.js +++ b/bin/now-list.js @@ -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) diff --git a/bin/now-logs.js b/bin/now-logs.js index d928392..f8ebaa6 100644 --- a/bin/now-logs.js +++ b/bin/now-logs.js @@ -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) diff --git a/bin/now-open.js b/bin/now-open.js index 55a42e0..6eabb63 100644 --- a/bin/now-open.js +++ b/bin/now-open.js @@ -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) diff --git a/bin/now-remove.js b/bin/now-remove.js index 238a4bf..e4c46c1 100755 --- a/bin/now-remove.js +++ b/bin/now-remove.js @@ -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) diff --git a/bin/now-scale.js b/bin/now-scale.js index 14ac3a5..288806d 100755 --- a/bin/now-scale.js +++ b/bin/now-scale.js @@ -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) diff --git a/bin/now-secrets.js b/bin/now-secrets.js index 211bfd9..ee6b765 100755 --- a/bin/now-secrets.js +++ b/bin/now-secrets.js @@ -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) diff --git a/bin/now-teams.js b/bin/now-teams.js index 4b001e9..6280c40 100644 --- a/bin/now-teams.js +++ b/bin/now-teams.js @@ -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) @@ -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 } diff --git a/bin/now-upgrade.js b/bin/now-upgrade.js index 4b5e5a9..f1f9ffb 100644 --- a/bin/now-upgrade.js +++ b/bin/now-upgrade.js @@ -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) diff --git a/bin/teams/add.js b/bin/teams/add.js index 4355940..dbfcc07 100644 --- a/bin/teams/add.js +++ b/bin/teams/add.js @@ -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 @@ -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')}` }) diff --git a/bin/teams/invite.js b/bin/teams/invite.js index 9d03e34..22815cd 100644 --- a/bin/teams/invite.js +++ b/bin/teams/invite.js @@ -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]) diff --git a/bin/teams/switch.js b/bin/teams/switch.js index 0ae497a..48e3e92 100644 --- a/bin/teams/switch.js +++ b/bin/teams/switch.js @@ -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() diff --git a/lib/cfg.js b/lib/cfg.js index 36d88d3..e098924 100644 --- a/lib/cfg.js +++ b/lib/cfg.js @@ -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 { @@ -60,7 +62,7 @@ async function read({ force = false, token, apiUrl } = {}) { return {} } - if (!existing) { + if (!existing.token) { return {} }