-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
110 lines (100 loc) · 2.81 KB
/
server.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
const program = require('commander');
const path = require('path');
let fs = require('fs');
let fspromise = fs.promises;
const inquirer = require('inquirer');
const readline = require('readline');
let totalSubject = [];
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let i = 0;
let prompt = async ({ message, name }, answer) => {
i++;
if (i <= answer) {
return inquirer
.prompt({ message, name })
.then(function ({ name: entry }) {
totalSubject.push(entry.toLocaleLowerCase());
return prompt({ message: `Subject ${i + 1} `, name }, answer);
});
}
};
let schedule = {};
const daysInWeek = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
let j = 0;
let prompt1 = async (day, index) => {
j++;
if (j <= 6) {
return inquirer
.prompt([
{
type: 'list',
name: 'selected',
message: `${day} class ${j}`,
choices: JSON.parse(fs.readFileSync('./sub.json')),
},
])
.then(({ selected }) => {
if (selected === 'break') {
console.log(schedule[index]);
schedule[day].push(-1);
} else {
schedule[day].push(totalSubject.indexOf(selected));
}
return prompt1(day);
})
.catch((e) => console.log(e))
}
};
program.version('0.0.1').description('BB CLI program');
program
.command('setup')
.alias('n')
.description('add timetable')
.action(() => {
rl.question(
'Enter total number of subjects including labs and lifeskill and mentoring ',
(answer) => {
prompt({ message: `Subject 1 `, name: 'name' }, answer).then(
async () => {
try {
totalSubject = totalSubject.sort();
totalSubject.push('break')
await fspromise.writeFile(
path.resolve('./sub.json'),
JSON.stringify(totalSubject)
);
async function finalPrompt(index) {
index++;
schedule[daysInWeek[index]] = [];
if (index < 7) {
return await prompt1(daysInWeek[index], index)
.then(() => {
j = 0;
console.log('yo');
console.log(index);
fs.writeFileSync( path.resolve('./schedule.json'), JSON.stringify(schedule, null, 2));
finalPrompt(index);
});
}
}
await finalPrompt(0);
} catch (error) {
console.log('Crap happens');
}
}
);
}
);
});
program.parse(process.argv);