/
tasks.js
129 lines (110 loc) · 3.09 KB
/
tasks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'use strict';
const chalk = require('chalk');
const { execSync } = require('child_process');
const fs = require('fs');
const showBanner = require('node-banner');
/**
* Fetch the respective task corresponding to the supplied key
*
* @param {String} key - key that corresponds to a specific task
* @returns {Promise<void>}
*/
const fetchTask = async key => {
await showBanner(
'teachcode',
` Learn to code effectively ${`\t`.repeat(4)} Powered by MadHacks`,
);
console.log();
let exercises;
let fileName;
if (!fs.existsSync(process.cwd() + '/config.json')) {
console.log(
chalk.red.bold(
' Make sure that you are within the teachcode-solutions directory!',
),
);
console.log();
console.log(
chalk.cyan.bold('\tcd teachcode-solutions may resolve the issue!'),
);
console.log();
process.exit(1);
}
// Reading the user-config information.
let userConfig = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
const {
learningTrack,
userName,
userSubmittedFiles,
taskCount,
keys,
} = userConfig;
if (!key) {
key = keys.slice(-1).pop();
}
if (learningTrack === 'Python') {
fileName = `task${taskCount + 1}.py`;
exercises = require('../workspace/python/tasks');
} else {
fileName = `task${taskCount + 1}.js`;
exercises = require('../workspace/js/tasks');
}
// Holding reference to keys of all the completed tasks
let previousKeys = keys.slice(0, keys.length - 1);
let taskAlreadyCompleted = [];
previousKeys.some((item, index) => {
if (item === key) {
taskAlreadyCompleted = [true, index];
return true;
}
});
if (taskAlreadyCompleted[0]) {
console.log();
console.log(chalk.yellow.bold(' This task is already completed!'));
console.log();
console.log();
console.log(
chalk.green.bold(
`User: ${userName}${`\t`.repeat(4)}Progress: ${taskCount + 1}/${
exercises.length
}`,
),
);
console.log();
console.log(chalk.green(`${exercises[taskAlreadyCompleted[1]].task}`));
console.log();
process.exit(1);
}
if (taskCount === exercises.length) {
console.log();
console.log(chalk.red.bold('No more tasks available!'));
process.exit(1);
}
if (keys.slice(-1).pop() === key) {
if (userSubmittedFiles.indexOf(fileName) === -1) {
userSubmittedFiles.push(fileName);
}
fs.writeFileSync('./config.json', JSON.stringify(userConfig));
console.log();
console.log(
chalk.cyan.bold(
`User: ${userName}${`\t`.repeat(6)}Progress: ${taskCount + 1}/${
exercises.length
}`,
),
);
// Displaying respective task within the the console screen.
console.log();
console.log(chalk.green(`${exercises[taskCount].task}`));
console.log();
let createCmd = process.platform !== 'win32' ? 'touch' : 'notepad';
execSync(`${createCmd} ${fileName}`);
} else {
console.log();
console.log(
chalk.red.bold("Make sure that you've grabbed the key correctly!"),
);
console.log();
}
};
module.exports = fetchTask;