Skip to content

Commit 6820bdd

Browse files
refactor: stick to best practices
1 parent 868f193 commit 6820bdd

File tree

5 files changed

+43
-22
lines changed

5 files changed

+43
-22
lines changed

src/commands/init.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
22

33
const chalk = require('chalk');
4-
const { execSync } = require('child_process');
4+
const { shellSync } = require('execa');
55
const fs = require('fs');
66
const inquirer = require('inquirer');
77
const showBanner = require('node-banner');
88
const open = require('open');
9+
const ora = require('ora');
910

1011
// GitHub workflow helper methods.
1112
const {
@@ -44,6 +45,12 @@ const showInstructions = () => {
4445
console.log(chalk.cyan.bold(` 2. teachcode fetchtask`));
4546
};
4647

48+
/**
49+
* Opens up the default browser with information concerning
50+
* access token creation as required
51+
* @returns {Promise<void>}
52+
*/
53+
4754
const promptAccessTokenCreation = async () => {
4855
const instructionsUrl =
4956
'https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line';
@@ -87,13 +94,10 @@ const initTasks = async () => {
8794
console.log(
8895
chalk.redBright(
8996
` It seems that there is already a ${chalk.yellow(
90-
'Teach-Code-solutions',
97+
'teachcode-solutions',
9198
)} directory or ${chalk.yellow('config.json')} file existing in path`,
9299
),
93100
);
94-
console.log();
95-
console.log(chalk.redBright(' Exiting!!'));
96-
console.log();
97101
process.exit(1);
98102
}
99103

@@ -133,9 +137,10 @@ const initTasks = async () => {
133137
...userConfig,
134138
learningTrack: learningTrackOfChoice,
135139
userName,
136-
keys: userConfig.keys.push(key),
137140
};
138141

142+
userConfig.keys.push(key);
143+
139144
// Prompt for GitHub username.
140145
await initializeGHWorkFlow();
141146

@@ -146,17 +151,22 @@ const initTasks = async () => {
146151
await promptAccessTokenCreation();
147152
await createRepository();
148153

149-
execSync(`mkdir -p teachcode-solutions`);
154+
shellSync(`mkdir teachcode-solutions`);
150155
fs.writeFileSync(
151-
`teachcode-solutions/config.json`,
156+
'teachcode-solutions/config.json',
152157
JSON.stringify(userConfig, null, 2),
153158
);
154-
155-
process.chdir('teachcode-solutions');
156159
await configureLocalRepo();
157160
} else {
158-
// Clone the remote repository
159-
await cloneRepository();
161+
const spinner = ora('Fetching user progress').start();
162+
try {
163+
// Clone the remote repository
164+
await cloneRepository();
165+
} catch (err) {
166+
spinner.fail('Something went wrong');
167+
throw err;
168+
}
169+
spinner.stop();
160170
}
161171
showInstructions();
162172
};

src/commands/keys.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ const showKeys = async () => {
1717
);
1818
console.log();
1919

20-
if (!fs.existsSync(`${process.cwd()}/config.json`)) {
20+
if (!fs.existsSync(`./config.json`)) {
2121
console.log(chalk.red("Config file doesn't exist!"));
2222
console.log();
2323
process.exit(1);
2424
}
2525

26-
let userConfig = fs.readFileSync(process.cwd() + '/config.json', 'utf8');
26+
let userConfig = fs.readFileSync('./config.json', 'utf8');
2727
const { keys, userName, taskCount } = JSON.parse(userConfig);
2828

2929
console.log();

src/commands/submit.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,7 @@ const submitTask = async () => {
186186
process.exit(1);
187187
}
188188

189-
userConfig = JSON.parse(
190-
fs.readFileSync(process.cwd() + '/config.json', 'utf8'),
191-
);
189+
userConfig = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
192190
let { userName, userSubmittedFiles, learningTrack, taskCount } = userConfig;
193191

194192
if (learningTrack === 'Python') {

src/commands/tasks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ const fetchTask = async key => {
2222
let exercises;
2323
let fileName;
2424

25-
if (!fs.existsSync(process.cwd() + '/config.json')) {
25+
if (!fs.existsSync('./config.json')) {
2626
console.log(
2727
chalk.red.bold(
2828
' Make sure that you are within the teachcode-solutions directory!',
2929
),
3030
);
3131
console.log();
3232
console.log(
33-
chalk.cyan.bold('\tcd teachcode-solutions may resolve the issue!'),
33+
chalk.cyan.bold(' cd teachcode-solutions may resolve the issue!'),
3434
);
3535
console.log();
3636
process.exit(1);

src/utils/github.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const axios = require('axios');
44
const chalk = require('chalk');
55
const execa = require('execa');
66
const inquirer = require('inquirer');
7+
const ora = require('ora');
78

89
const validate = require('./validate');
910

@@ -29,6 +30,9 @@ const initializeGHWorkFlow = async () => {
2930

3031
// Check if it is a valid username
3132
if (!(await checkIfValidUser())) {
33+
console.log();
34+
console.log(chalk.red.bold(` ${userName} isn't a valid GitHub username`));
35+
console.log();
3236
await initializeGHWorkFlow();
3337
}
3438
};
@@ -114,10 +118,12 @@ const configureLocalRepo = async () => {
114118
const repoUrl = `https://github.com/${GHUserName}/teachcode-solutions`;
115119

116120
// Initialize an empty git repo.
117-
await execa.shell('git init');
121+
await execa.shell('git init', { cwd: 'teachcode-solutions' });
118122

119123
// Set the remote url.
120-
await execa.shell(`git remote add origin ${repoUrl}`);
124+
await execa.shell(`git remote add origin ${repoUrl}`, {
125+
cwd: 'teachcode-solutions',
126+
});
121127
};
122128

123129
/**
@@ -139,7 +145,14 @@ const makeLocalCommit = async taskCount => {
139145
*/
140146

141147
const pushToRemote = async () => {
142-
await execa.shell('git push origin master');
148+
const spinner = ora('Pushing to GitHub').start();
149+
try {
150+
await execa.shell('git push origin master');
151+
} catch (err) {
152+
spinner.fail('Something went wrong');
153+
throw err;
154+
}
155+
spinner.succeed('Saved user progress');
143156
};
144157

145158
module.exports = {

0 commit comments

Comments
 (0)