-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
240 lines (200 loc) · 7.29 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
const Manager = require("./lib/Manager");
const Engineer = require("./lib/Engineer");
const Intern = require("./lib/Intern");
const inquirer = require("inquirer");
const path = require("path");
const fs = require("fs");
const util = require("util");
const chalk = require('chalk');
const OUTPUT_DIR = path.resolve(__dirname, "output");
const outputPath = path.join(OUTPUT_DIR, "team.html");
const outputCSSPath = path.join(OUTPUT_DIR, "style.css");
// Promisify fs.writeFile() to use promises
const writeFileAsync = util.promisify(fs.writeFile);
const render = require("./src/page-template.js");
// Object array of team members
const employees = [];
// Validate an email address
function validateEmail(value) {
if (!value.trim()){
return chalk.red('Email Address is required.');
}
// Checking for valid email using regex
// From https://emailregex.com/
// General Email Regex (RFC 5322 Official Standard)
const regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (!regex.test(value)){
return chalk.red('Please enter a valid email address.');
}
return true;
}
// Function to prompt for Manager details
const promptManager = async function() {
return inquirer.prompt([
// Name
{
type: 'input',
name: 'name',
message: chalk.yellow("Enter your Name:"),
validate: value => value.trim() ? true : chalk.red('Name is required.'),
},
// Employee ID
{
type: 'input',
name: 'id',
message: chalk.yellow("Enter your Employee ID:"),
validate: value => value.trim() ? true : chalk.red('Employee ID is required.'),
},
// Email address
{
type: 'input',
name: 'email',
message: chalk.yellow("Enter your Email Address:"),
validate: function(email){return validateEmail(email)},
},
// Office number
{
type: 'input',
name: 'office',
message: chalk.yellow("Enter your Office Number:"),
validate: value => value.trim() ? true : chalk.red('Office Number is required.'),
},
])
};
// Function to prompt and add an Engineer to the team
const promptEngineer = async function() {
return inquirer.prompt([
// Engineer's Name
{
type: 'input',
name: 'name',
message: chalk.yellow("Enter Engineer's Name:"),
validate: value => value.trim() ? true : chalk.red('Name is required.'),
},
// ID
{
type: 'input',
name: 'id',
message: chalk.yellow("Enter Engineer's Employee ID:"),
validate: value => value.trim() ? true : chalk.red('Employee ID is required.'),
},
// Email
{
type: 'input',
name: 'email',
message: chalk.yellow("Enter Engineer's Email Address:"),
validate: function(email){return validateEmail(email)},
},
// GitHub username
{
type: 'input',
name: 'github',
message: chalk.yellow("Enter Engineer's GitHub username:"),
validate: value => value.trim() ? true : chalk.red('GitHub username is required.'),
},
])
};
// Function to prompt and add an Intern to the team
const promptIntern = async function() {
return inquirer.prompt([
// Intern’s name
{
type: 'input',
name: 'name',
message: chalk.yellow("Enter Intern's Name:"),
validate: value => value.trim() ? true : chalk.red('Name is required.'),
},
// ID
{
type: 'input',
name: 'id',
message: chalk.yellow("Enter Intern's Employee ID:"),
validate: value => value.trim() ? true : chalk.red('Employee ID is required.'),
},
// Email
{
type: 'input',
name: 'email',
message: chalk.yellow("Enter Intern's Email Address:"),
validate: function(email){return validateEmail(email)},
},
// School
{
type: 'input',
name: 'school',
message: chalk.yellow("Enter Intern's School:"),
validate: value => value.trim() ? true : chalk.red('School is required.'),
},
])
};
// Function to Show Menu
async function showMenu(){
const choices = [
{name: chalk.green('Add an engineer...'), value: 'engineer'},
{name: chalk.green('Add an intern...'), value: 'intern'},
{name: chalk.green('Finish building the team.'), value: 'finish'},
];
console.clear();
console.log(""); // Add space
inquirer.prompt([
{
type: 'list',
name: 'menu',
message: chalk.yellow("What would you like to do now?"),
choices: choices
}
])
.then(async function (answers) {
switch (answers.menu) {
case 'engineer':
// Ask for engineer details
const engineerData = await promptEngineer();
const engineer = new Engineer(engineerData.name, engineerData.id, engineerData.email, engineerData.github);
// Add engineer details to employees
employees.push(engineer);
showMenu();
break;
case 'intern':
// Ask for intern details
const internData = await promptIntern();
const intern = new Intern(internData.name, internData.id, internData.email, internData.school);
// Add intern details to employees
employees.push(intern);
showMenu();
break;
default:
// 'Finish building the team.'
console.clear();
console.log(chalk.blue("\nRendering HTML..."));
// Render HTML
await writeFileAsync(outputPath, render(employees));
console.log(chalk.green(`HTML file saved to ${outputPath}`));
// Copy the css from style
fs.copyFile('./src/style.css', './output/style.css', (err)=>{if (err){console.error(chalk.red("Error copying CSS file"), err)}});
console.log(chalk.green(`CSS file saved to ${outputCSSPath}`));
break;
}
})
// Catch errors and write to console
.catch(function (err) {
// Check if error was unable to open the output directory
if ((err.code === 'ENOENT') && (err.syscall === 'open') && (err.path).includes(outputPath)) {
console.error(chalk.red("\nError: The output directory doesn't exist."));
} else {
console.error(chalk.red("\nAn error occurred\n"), err);
}
})
}
// Function to start collecting team information
async function main() {
console.log(chalk.underline.magenta("\nTeam Profile Generator\n"));
console.log(chalk.green("Add Manager"));
// Ask for manager details
const managerData = await promptManager();
const manager = new Manager(managerData.name, managerData.id, managerData.email, managerData.office);
// Add manager details to employees
employees.push(manager);
showMenu();
}
// Start process by calling main()
main();