-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
167 lines (144 loc) · 4.71 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
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 OUTPUT_DIR = path.resolve(__dirname, "output");
const render = require("./src/page-template.js");
const { managerQs, internQs, engineerQs } = require("./src/questions.js");
async function init() {
const team = await getTeam();
// Create array to store Employee instances
const empArr = [];
// Add Manager to array
const manager = new Manager(team.manager.name, team.manager.id, team.manager.email, team.manager.officeNum);
empArr.push(manager);
// Add Engineers to array
const engineers = team.engineers;
if (engineers.length !== 0) {
engineers.forEach(engineer => {
const eng = new Engineer(engineer.name, engineer.id, engineer.email, engineer.github);
empArr.push(eng);
});
}
// Add Interns to array
const interns = team.interns;
if (interns.length !== 0) {
interns.forEach(intern => {
const intr = new Intern(intern.name, intern.id, intern.email, intern.school);
empArr.push(intr);
});
}
// Render page
const page = render(empArr);
// Generate output folder + team.html
generateHtml(page);
}
init();
// ** FUNCTIONS ** //
async function getTeam() {
// Get Manager
const manager = await makeEmployee(managerQs);
// Initialize Eng / Int arrays
const engineers = [];
const interns = [];
let continueLoop = true; // Control variable for the loop
while (continueLoop) {
// Ask for next Choice
const choice = await nextChoice();
switch (choice) {
case 1:
const engineer = await makeEmployee(engineerQs);
engineers.push(engineer);
break;
case 2:
const intern = await makeEmployee(internQs);
interns.push(intern);
break;
default:
continueLoop = false;
}
}
return { manager: manager, engineers: engineers, interns: interns};
}
// function to initialize program
async function makeEmployee(questions) {
try {
const answers = await inquirer.prompt(questions);
return answers;
} catch (error) {
if (error.isTtyError) {
// Prompt couldn't be rendered in the current environment
console.error(error);
} else {
// Something else went wrong
console.error(error);
}
}
}
async function nextChoice() {
const choices = [
"Add an engineer",
"Add an intern",
"Finish building the team"
];
const question = {
type: 'list',
name: 'licence',
message: "Prefered licence type?",
choices: [...choices],
};
try {
const answer = await inquirer.prompt(question);
switch (answer.licence) {
case "Add an engineer":
return 1;
case "Add an intern":
return 2;
case "Finish building the team":
return 3;
default:
return null; // Or some other default value if needed
}
} catch (error) {
if (error.isTtyError) {
console.error(error);
} else {
console.error(error);
}
}
}
function generateHtml(page) {
// Check if OUTPUT_DIR exists
fs.stat(OUTPUT_DIR, (err, stats) => {
if (err) {
// Directory does not exist
fs.mkdir(OUTPUT_DIR, { recursive: true }, (mkdirErr) => {
if (mkdirErr) {
console.error("Error creating directory:", mkdirErr);
} else {
// Write file after creating directory
const filePath = path.join(OUTPUT_DIR, "team.html");
fs.writeFile(filePath, page, (writeErr) => {
if (writeErr) {
console.error("Error writing file:", writeErr);
} else {
console.log("File written successfully.");
}
});
}
});
} else {
// Directory exists
const filePath = path.join(OUTPUT_DIR, "team.html");
fs.writeFile(filePath, page, (writeErr) => {
if (writeErr) {
console.error("Error writing file:", writeErr);
} else {
console.log("File written successfully.");
}
});
}
});
}