Skip to content

Commit

Permalink
Merge pull request #12 from jaebradley/validate-github-username
Browse files Browse the repository at this point in the history
fix(prompts): add GitHub username validation
  • Loading branch information
jaebradley authored Apr 23, 2018
2 parents 9bc728e + 801b02c commit 78ec652
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
21 changes: 5 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
},
"dependencies": {
"@babel/runtime": "^7.0.0-beta.42",
"@octokit/rest": "^15.2.6",
"child-process-promise": "^2.2.1",
"commander": "^2.15.1",
"deepmerge": "^2.1.0",
Expand Down
14 changes: 14 additions & 0 deletions src/isGitHubUsernameValid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Octokit from '@octokit/rest';

const client = new Octokit();

const isGitHubUsernameValid = async (username) => {
try {
await client.users.getForUser({ username });
return true;
} catch (e) {
return false;
}
};

export default isGitHubUsernameValid;
10 changes: 9 additions & 1 deletion src/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import emailValidator from 'email-validator';
import validateNpmPackageName from 'validate-npm-package-name';
import isSemver from 'is-semver';

import isGitHubUsernameValid from './isGitHubUsernameValid';

const prompts = async () => (
inquirer.prompt([
{
Expand Down Expand Up @@ -57,7 +59,13 @@ const prompts = async () => (
name: 'gitHubUsername',
message: 'Input your GitHub Username',
type: 'input',
validate: answer => answer && answer.length > 0,
validate: async (username) => {
if (await isGitHubUsernameValid(username)) {
return true;
}

return `${username} is an invalid GitHub username`;
},
},
])
);
Expand Down

0 comments on commit 78ec652

Please sign in to comment.