Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Commit

Permalink
Merge pull request #395 from zapier/refactor-login
Browse files Browse the repository at this point in the history
refactor `zapier login` to async
  • Loading branch information
xavdid committed Feb 1, 2019
2 parents a058e6d + 6c5fc2d commit a552ca0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 57 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
100 changes: 47 additions & 53 deletions src/commands/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 = {};
Expand Down

0 comments on commit a552ca0

Please sign in to comment.