Skip to content

Commit 80463d8

Browse files
chore: validate GH username
1 parent 6c6e1cb commit 80463d8

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/utils/github.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const chalk = require('chalk');
55
const execa = require('execa');
66
const inquirer = require('inquirer');
77

8-
const validate = require('../utils/validate');
8+
const validate = require('./validate');
99

1010
// Global reference to the GitHub username.
1111
let GHUserName;
@@ -27,8 +27,26 @@ const initializeGHWorkFlow = async () => {
2727
// Holding global reference to the GitHub username.
2828
GHUserName = userName;
2929

30-
// Check if the remote repository already exists and act accordingly.
31-
await checkIfRepositoryExists();
30+
// Check if it is a valid username
31+
if (!(await checkIfValidUser())) {
32+
await initializeGHWorkFlow();
33+
}
34+
};
35+
36+
/**
37+
* Checks if the remote repository exists
38+
*
39+
* @returns {Promise<boolean>}
40+
*/
41+
42+
const checkIfValidUser = async () => {
43+
const API_URL = `https://api.github.com/users/${GHUserName}`;
44+
try {
45+
await axios.get(API_URL);
46+
return true;
47+
} catch (err) {
48+
return false;
49+
}
3250
};
3351

3452
/**
@@ -102,7 +120,7 @@ const configureLocalRepo = async () => {
102120
const repoUrl = `https://github.com/${GHUserName}/teachcode-solutions`;
103121

104122
// Initialize an empty git repo.
105-
await execa('git', ['init']);
123+
await execa.shell('git init');
106124

107125
// Set the remote url.
108126
await execa.shell(`git remote add origin ${repoUrl}`);

0 commit comments

Comments
 (0)