-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
275 lines (260 loc) · 8.73 KB
/
index.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Node packages
const inquirer = require('inquirer');
const fs = require('fs');
const path = require('path');
// Constructors for employee, engineer, intern, and manager class'
const Employee = require('./lib/Employee');
const Engineer = require('./lib/Engineer');
const Intern = require('./lib/Intern');
const Manager = require('./lib/Manager');
// Use to connect html template with prompt response
const generateTeamString = require('./src/template')
// Set absolute path btw current directory and 'dist' for generated HTML
const DIST_DIR = path.resolve(__dirname, 'dist')
// Join path segments and normalize resulting path
const generatePath = path.join(DIST_DIR, 'index.html');
// Empty array to push user responses into
const userTeam = [];
// Prompt arrays
const managerPrompt = [
{
type: "input",
name: "managerName",
message: "What is the team manager's name?",
validate: (answer) => {
if (answer === "") {
return "This question cannot go unanswered!";
}
return true;
},
},
{
type: "input",
name: "managerId",
message: "What is the team manager's employee ID?",
validate: (answer) => {
if (answer === "") {
return "This question cannot go unanswered!";
} else if (isNaN(answer)) {
return "Employee ID must be entered in numbers"
}
return true;
},
},
{
type: "input",
name: "managerEmail",
message: "What is the team manager's email?",
validate: (answer) => {
//user must enter a valid email format
valid = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(answer);
if (valid) {
return true;
} else if (answer === "") {
return "This question cannot go unanswered!";
}
return "Please enter a valid email address";
},
},
{
type: "input",
name: "managerOffice",
message: "What is the team manager's office number?",
validate: (answer) => {
if (answer === "") {
return "This question cannot go unanswered!";
}
return true;
},
}
];
const menuPrompt = [
{
type: "list",
name: "menuOpt",
message: "Would you like to add another employee to your team?",
choices: [
"Engineer",
"Intern",
"Exit & Generate Team"
]
}
];
const engineerPrompt = [
{
type: "input",
name: "engineerName",
message: "What is the Engineer's name?",
validate: (answer) => {
if (answer === "") {
return "This question cannot go unanswered!";
}
return true;
},
},
{
type: "input",
name: "engineerId",
message: "What is the Engineer's employee ID?",
validate: (answer) => {
if (answer === "") {
return "This question cannot go unanswered!";
} else if (isNaN(answer)) {
return "Employee ID must be entered in numbers"
}
return true;
},
},
{
type: "input",
name: "engineerEmail",
message: "What is the Engineer's email?",
validate: (answer) => {
//user must enter a valid email format
valid = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(answer);
if (valid) {
return true;
} else if (answer === "") {
return "This question cannot go unanswered!";
}
return "Please enter a valid email address";
},
},
{
type: "input",
name: "engineerGitHub",
message: "What is the Engineer's GitHub?",
validate: (answer) => {
if (answer === "") {
return "This question cannot go unanswered!";
}
return true;
},
}
];
const internPrompt = [
{
type: "input",
name: "internName",
message: "What is the Intern's name?",
validate: (answer) => {
if (answer === "") {
return "This question cannot go unanswered!";
}
return true;
},
},
{
type: "input",
name: "internId",
message: "What is the Intern's employee ID?",
validate: (answer) => {
if (answer === "") {
return "This question cannot go unanswered!";
} else if (isNaN(answer)) {
return "Employee ID must be entered in numbers"
}
return true;
},
},
{
type: "input",
name: "internEmail",
message: "What is the Intern's email?",
validate: (answer) => {
//user must enter a valid email format
valid = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(answer);
if (valid) {
return true;
} else if (answer === "") {
return "This question cannot go unanswered!";
}
return "Please enter a valid email address";
},
},
{
type: "input",
name: "internSchool",
message: "What is the Intern's School?",
validate: (answer) => {
if (answer === "") {
return "This question cannot go unanswered!";
}
return true;
},
}
];
// Prompt user to answer questions about their team
function initPrompt() {
console.info("If you don't know the answer to one of the questions, enter 'TBA'")
//Prompt user to enter the team manager’s name, employee ID, email address, and office number
function addManager() {
inquirer.prompt(managerPrompt)
.then(answers => {
// Store managerInfo w the classes imported w Manager
const managerInfo = new Manager(answers.managerName, answers.managerId, answers.managerEmail, answers.managerOffice)
// push managerInfo into empty array
userTeam.push(managerInfo);
// then show user the nav menu
teamMenu()
})
}
// Call addManager = first user prompt
addManager();
// Prompt user with a menu with the option to add an engineer or an intern or to finish building my team
teamMenu = () => {
inquirer.prompt(menuPrompt)
.then(answer => {
switch(answer.menuOpt) {
// If user selects "Engineer", run func addEngineer
case "Engineer":
addEngineer()
break;
// If user selects "Intern", run func addIntern
case "Intern":
addIntern();
break;
// Else (user selects "Exit"), run func generateHTML
default:
generateHtml();
}
})
}
// Engineer opt. = enter the engineer’s name, ID, email, and GitHub username, and I am taken back to the menu
function addEngineer() {
inquirer.prompt(engineerPrompt)
.then(answers => {
// Store engineerInfo w the classes imported w Engineer
const engineerInfo = new Engineer(answers.engineerName, answers.engineerId, answers.engineerEmail, answers.engineerGitHub)
// push engineerInfo into empty array
userTeam.push(engineerInfo);
// then run..
teamMenu()
})
}
// Intern opt. = enter the intern’s name, ID, email, and school, and I am taken back to the menu
function addIntern() {
inquirer.prompt(internPrompt)
.then(answers => {
// Store interInfo w the classes imported w Engineer
const internInfo = new Intern(answers.internName, answers.internId, answers.internEmail, answers.internSchool)
// push internInfo into empty array
userTeam.push(internInfo);
// then run..
teamMenu()
})
}
// Exit opt. = exit the application, and the HTML is generated
generateHtml = () => {
//Info from user input is passed into the html in template.js and returned as 'finalString' = html template + userTeam info
const finalString = generateTeamString(userTeam);
console.log(userTeam);
//Write file in filepath with generated information, and error for callback, else console success msg
fs.writeFile(generatePath, finalString, err => {
if (err) return err
console.info("Your team profile has been generated!");
});
}
}
// Call init on npm start
initPrompt();