From 6c5fc2dff28bc8d553937f345f47c231131024d5 Mon Sep 17 00:00:00 2001 From: David Brownman Date: Thu, 31 Jan 2019 15:17:29 -0500 Subject: [PATCH] move login to async --- package.json | 8 ++-- src/commands/login.js | 100 ++++++++++++++++++++---------------------- 2 files changed, 51 insertions(+), 57 deletions(-) diff --git a/package.json b/package.json index 3d367c3..1087af3 100644 --- a/package.json +++ b/package.json @@ -23,10 +23,10 @@ "version": "npm run docs && npm run gen-completions && git add docs/* goodies/* README.md", "postversion": "git push && git push --tags", "prepublish": "npm run build", - "build": "rm -rf lib && node_modules/babel-cli/bin/babel.js src -d lib --copy-files", - "watch": "rm -rf lib && node_modules/babel-cli/bin/babel.js --watch src -d lib --copy-files", - "lint": "node_modules/.bin/eslint zapier.js src", - "lint-snippets": "node_modules/.bin/eslint snippets --rule 'no-unused-vars: 0' --ignore-pattern snippets/next.js", + "build": "rm -rf lib && babel src -d lib --copy-files", + "watch": "rm -rf lib && babel --watch src -d lib --copy-files", + "lint": "eslint zapier.js src", + "lint-snippets": "eslint snippets --rule 'no-unused-vars: 0' --ignore-pattern snippets/next.js", "pretest": "npm run build", "test": "NODE_ENV=test mocha -t 50000 --recursive lib/tests && npm run lint && npm run lint-snippets", "smoke-test": "npm run build && NODE_ENV=test mocha -t 30000 --recursive lib/smoke-tests", diff --git a/src/commands/login.js b/src/commands/login.js index 05e8cd8..7213d17 100644 --- a/src/commands/login.js +++ b/src/commands/login.js @@ -6,7 +6,8 @@ const utils = require('../utils'); const QUESTION_USERNAME = 'What is your Zapier login email address? (Ctrl-C to cancel)'; const QUESTION_PASSWORD = 'What is your Zapier login password?'; -const login = (context, firstTime = true) => { + +const login = async (context, firstTime = true) => { const checks = [ utils .readCredentials() @@ -17,59 +18,52 @@ const login = (context, firstTime = true) => { .then(() => true) .catch(() => false) ]; - return Promise.all(checks) - .then(([credentialsPresent, credentialsGood]) => { - if (!credentialsPresent) { - context.line( - colors.yellow( - `Your ${constants.AUTH_LOCATION} has not been set up yet.\n` - ) - ); - } else if (!credentialsGood) { - context.line( - colors.red( - `Your ${ - constants.AUTH_LOCATION - } looks like it has invalid credentials.\n` - ) - ); - } else { - context.line( - colors.green( - `Your ${ - constants.AUTH_LOCATION - } looks valid. You may update it now though.\n` - ) - ); - } - return utils.getInput(QUESTION_USERNAME); - }) - .then(username => { - return Promise.all([ - username, - utils.getInput(QUESTION_PASSWORD, { secret: true }) - ]); - }) - .then(([username, password]) => { - return utils.createCredentials(username, password).then(data => data.key); - }) - .then(deployKey => { - return utils.writeFile( - constants.AUTH_LOCATION, - utils.prettyJSONstringify({ - [constants.AUTH_KEY]: deployKey - }) - ); + const [credentialsPresent, credentialsGood] = await Promise.all(checks); + + if (!credentialsPresent) { + context.line( + colors.yellow( + `Your ${constants.AUTH_LOCATION} has not been set up yet.\n` + ) + ); + } else if (!credentialsGood) { + context.line( + colors.red( + `Your ${ + constants.AUTH_LOCATION + } looks like it has invalid credentials.\n` + ) + ); + } else { + context.line( + colors.green( + `Your ${ + constants.AUTH_LOCATION + } looks valid. You may update it now though.\n` + ) + ); + } + const username = await utils.getInput(QUESTION_USERNAME); + const password = await utils.getInput(QUESTION_PASSWORD, { secret: true }); + + const deployKey = (await utils.createCredentials(username, password)).key; + + await utils.writeFile( + constants.AUTH_LOCATION, + utils.prettyJSONstringify({ + [constants.AUTH_KEY]: deployKey }) - .then(utils.checkCredentials) - .then(() => { - context.line( - `Your deploy key has been saved to ${constants.AUTH_LOCATION}. ` - ); - if (firstTime) { - context.line('Now try `zapier init .` to start a new local app.\n'); - } - }); + ); + + await utils.checkCredentials(); + + context.line( + `Your deploy key has been saved to ${constants.AUTH_LOCATION}. ` + ); + + if (firstTime) { + context.line('Now try `zapier init .` to start a new local app.\n'); + } }; login.argsSpec = []; login.argOptsSpec = {};