-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
215 lines (175 loc) · 6 KB
/
app.js
File metadata and controls
215 lines (175 loc) · 6 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const { Manager } = require("./Develop/lib/Manager");
const { Engineer } = require("./Develop/lib/Engineer");
const { Intern } = require("./Develop/lib/Intern");
const inquirer = require("inquirer");
const chalkPipe = require('chalk-pipe');
const color = require('./Develop/chalk/colors');
const path = require("path");
const fs = require("fs");
const { validateEntries, validateNumbers, validateEmail } = require('./Develop/lib/validate');
const OUTPUT_DIR = path.resolve(__dirname, "output");
const outputPath = path.join(OUTPUT_DIR, "team.html");
const { render } = require("./Develop/lib/htmlRenderer");
const validate = require("./Develop/lib/validate");
const { clearScreenDown } = require("readline");
const teamMembers = [];
// Main Questions
const questions = [
{
type: 'input',
message: color.blue('what is your name?'),
name: 'name',
validate: validateEntries
},
{
type: 'input',
message: color.blue('what is your id number?'),
name: 'id',
validate: validateNumbers
},
{
type: 'input',
message: color.blue('what is your email?'),
name: 'email',
validate: validateEmail
},
{
type: 'list',
message: color.blue('Please check your role in the company'),
choices: [
color.red('Manager'),
color.green('Intern'),
color.purple('Engineer'),
],
name: 'role'
},
]
// Manager Questions
const managerQuestions = [
{
type: 'input',
message: color.red('What is you office number'),
name: 'officeNumber',
validate: validateNumbers
}
]
// Intern Questions
const internQuestions = [
{
type: 'input',
message: color.green('What school did you go to?'),
name: 'school',
validate: validateEntries
}
]
// Engineer Questions
const engineerQuestions = [
{
type: 'input',
message: color.purple('Please enter your github user name'),
name: 'gitHub',
validate: validateEntries
}
]
// confirm more employee
const moreEmployee = [
{
type: 'confirm',
message: 'Would you like to add more team members?',
name: 'confirm',
default: false
}
]
async function Question() {
try {
// first round of basic question to answer
let mainAnswers = await inquirer.prompt(questions);
// figures out the next role of questions that need to be answered
let role = await sendToNextPrompt(mainAnswers);
// get the role specific questions
let roleAnswers = await inquirer.prompt(role);
// all of the employee data
// compiled in one object
let employeeData = await { ...mainAnswers, ...roleAnswers };
// takes the employee and builds with the appropriate constructor
let employee = await buildEmployee(employeeData);
// push the new created employye to the teamMembers array
teamMembers.push(employee);
// console logging the array to check where we at
// console.log(teamMembers);
// ask if they would like to add more team members
let employeeAdd = await inquirer.prompt(moreEmployee);
// validate response for the next action
addMoreOrRender(employeeAdd.confirm);
}
catch (err) {
console.log(`theres was an error somewhere in the async ${err}`);
}
}
// builds a the new Employee with the specific constructor
function buildEmployee(employee) {
let name = employee.name;
let id = employee.id;
let email = employee.email;
let role = employee.role;
// checking to see the correct keys and values
// console.log('inside the build employee function',employee);
switch (role) {
case 'Manager': return new Manager(name, id, email, employee.officeNumber);
case 'Intern': return new Intern(name, id, email, employee.school);
case 'Engineer': return new Engineer(name, id, email, employee.gitHub);
default: return 'something went really wrong in building an employee';
}
}
// decodes the color UI code
function colorDecoder(role) {
// based on the unique id choose the correct role
if (role === '\u001b[31m\u001b[1mManager\u001b[22m\u001b[39m') {
return 'Manager';
} else if (role === '\u001b[32m\u001b[1mIntern\u001b[22m\u001b[39m') {
return 'Intern';
} else if (role === '\u001b[38;2;128;0;128m\u001b[1mEngineer\u001b[22m\u001b[39m') {
return 'Engineer';
}
}
// this function returns the specific role questions needed for the next prompt
function sendToNextPrompt(employee) {
// let role;
// testing the color ui
// console.log('this is inside the next prompt',employee);
employee.role = colorDecoder(employee.role);
// testing output
// console.log(employee);
switch (employee.role) {
case 'Manager': return managerQuestions;
case 'Intern': return internQuestions;
case 'Engineer': return engineerQuestions;
default: return `Something went really wrong! did you pick a role?`;
}
}
// restarts the whole thing again or returns for the next step
function addMoreOrRender(confirm) {
if (confirm) {
// return back to the top of the questions are add in another employee
return Question();
}
//// checking to see if this directory exist
fs.access(OUTPUT_DIR, (err) => {
if (err) {
// if it doesn't exist we make it and then create the file
console.log(`This directory does not exist, Creating now!!!`);
fs.mkdir(OUTPUT_DIR, (err) => (err) ? console.log(err) : writeHTML());
} else {
//// else we just create the html
writeHTML();
}
})
}
// function writes the html for the website
function writeHTML() {
let html = render(teamMembers)
fs.writeFile(outputPath, html, (err) => {
(err) ? console.log(err) : console.log('The file has been written succesfully!');
});
}
Question();