-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
294 lines (273 loc) · 10.9 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
var inquirer = require('inquirer');
const log = require('loglevel');
const performance = require('performance-now');
const fs = require('fs');
const path = require('path');
const dateFormat = require('dateformat');
const csv = require('fast-csv');
const Table = require('cli-table2');
const ProgressBar = require('progress');
const SageBooker = require('../SAGE-BookingSimulator');
require('dotenv').config();
let suffix = 0;
function createScreenshotDirectory(localSuffix = 0) {
const screenshotDir = path.join(__dirname,
dateFormat(new Date(), "yyyymmdd") + (localSuffix > 0 ? '_' + localSuffix : ''));
if (!fs.existsSync(screenshotDir)) {
fs.mkdirSync(screenshotDir);
} else {
suffix++;
return createScreenshotDirectory(suffix);
}
return screenshotDir;
}
function readBookingPositions(inFile) {
return new Promise(function (resolve, reject) {
let bookingPositions = [];
log.info('Reading projects and registrations from file: ' + inFile);
if (!fs.existsSync(inFile)) {
reject("File does not exist.");
}
else {
const projectsAndRegistrationsFile = fs.createReadStream(inFile);
const csvStream = csv.fromStream(projectsAndRegistrationsFile, {
delimiter: ';',
headers: true,
comment: '#',
ignoreEmpty: true
}).transform(function (data) {
return {
"Project": data.Project,
"Registrations": data.Registrations.split(",")
}
}).on('data', async (data) => {
log.trace(data);
bookingPositions.push(data);
log.trace("Booking position list now contains: " + bookingPositions.length);
}).on('end', async (data) => {
log.trace('No more rows.');
log.trace("Read " + bookingPositions.length + " projects and their registrations.");
resolve(bookingPositions);
});
}
});
}
function createBookings(bookingPositions, bookings) {
bookings = bookings || [];
return createMoreBookings(bookingPositions, bookings).then(moreBookings => {
if (moreBookings) {
return createBookings(bookingPositions, bookings);
} else {
return bookings;
}
});
}
function createMoreBookings(bookingPositions, bookings) {
return new Promise(function (resolve, reject) {
createNewBooking(bookingPositions).then(booking => {
if (booking) {
bookings.push(booking);
}
var moreBookingsFunction = inquirer.createPromptModule();
moreBookingsFunction([{
type: "confirm",
name: "moreBookings",
message: "More bookings?"
}]).then(answers => {
resolve(answers.moreBookings);
});
});
});
}
function createNewBooking(bookingPositions) {
return new Promise(function (resolve, reject) {
let booking = {};
log.trace(bookingPositions);
var selectProject = inquirer.createPromptModule();
selectProject([{
type: "list",
name: "project",
message: "Which project?",
choices: bookingPositions.map(x => x.Project)
}]).then(answers => {
booking.project = answers.project;
//log.info(booking);
var selectRegistration = inquirer.createPromptModule();
selectRegistration([{
type: "list",
name: "registration",
message: "Which registration?",
choices: bookingPositions.filter(x => x.Project == answers.project)[0].Registrations
}]).then(answers => {
booking.registration = answers.registration;
//log.info(booking);
var selectDate = inquirer.createPromptModule();
selectDate.registerPrompt('datetime', require('inquirer-datepicker-prompt'));
selectDate([{
type: "datetime",
name: "date",
message: "On what date?",
format: ['dd', '.', 'mm', '.', 'yyyy']
}]).then(answers => {
booking.date = answers.date;
//log.info(booking);
var selectDuration = inquirer.createPromptModule();
selectDuration.registerPrompt('datetime', require('inquirer-datepicker-prompt'));
selectDuration([{
type: "datetime",
name: "duration",
message: "For how long?",
initial: new Date(0, 0, 0, 0, 0, 0, 0),
time: {
min: '12:15 AM',
max: '03:00 PM',
minutes: {
interval: 15
}
},
format: ['HH', ':', 'MM']
}]).then(answers => {
booking.duration = answers.duration.toLocaleTimeString("de-DE");
// log.info(booking);
var enterComment = inquirer.createPromptModule();
enterComment([{
type: "input",
name: "comment",
message: "What did you do?"
}]).then(answers => {
booking.comment = answers.comment;
//log.info(booking);
var confirm = inquirer.createPromptModule();
confirm([{
type: "confirm",
name: "confirm",
message: "Is this correct?"
}]).then(answers => {
//log.info(booking);
if (answers.confirm) {
resolve(booking);
} else {
resolve(null);
}
});
});
});
});
});
});
});
}
function createSageBookings(bookings) {
var table = new Table({
head: ['Project', 'Registration', 'Date', 'Duration', 'Comment']
});
const sageBookings = [];
bookings.forEach(booking => {
table.push([
booking.project, booking.registration,
convertDate(booking.date), booking.duration,
booking.comment
]);
let sageBooking = {};
function convertDate(date) {
let convertedDate = dateFormat(date, "dd.mm.yyyy");
return convertedDate;
}
function convertDuration(duration) {
let convertedDuration = duration.split(":");
convertedDuration = convertedDuration[0] + convertedDuration[1];
return convertedDuration;
}
sageBooking.Project = booking.project;
sageBooking.Registration = booking.registration;
sageBooking.Date = convertDate(booking.date);
sageBooking.Duration = convertDuration(booking.duration);
sageBooking.Comment = booking.comment;
// log.info(sageBooking);
sageBookings.push(sageBooking);
});
// log.info(sageBookings);
log.info(table.toString());
return sageBookings;
}
function writeSageBookings(outFile, sageBookings) {
csv.writeToStream(fs.createWriteStream(outFile), sageBookings, {
headers: true,
delimiter: ';'
});
log.trace("Sage Bookings written to file: " + outFile);
}
log.setLevel('info');
var selectFunction = inquirer.createPromptModule();
selectFunction([{
type: "list",
name: "getOrBook",
message: "What do you want, bro?",
choices: [{
name: "Get projects and booking positions",
value: "get"
},
{
name: "Define and book project times",
value: "book"
}]
}]).then(answers => {
log.trace(answers);
if (answers.getOrBook == "get") {
var bar = undefined;
const sageBooker = new SageBooker();
sageBooker.on('start', async (totalProjects) => {
bar = new ProgressBar('Getting projects... [:bar] :current/:total :etas', {
total: totalProjects
});
}).on('data', async (project, registration) => {
bar.tick();
// log.info(project);
// log.info(registration);
}).on('end', async () => {
const endTime = performance();
log.info("\nDone. This process took you " + ((endTime - startTime) / 1000 / 60).toFixed(2) + " minutes.");
});
const startTime = performance();
sageBooker.getProjects("Projects+Registrations.csv", {
interactiveMode: true,
screenshotDir: createScreenshotDirectory()
});
} else if (answers.getOrBook == "book") {
readBookingPositions("Projects+Registrations.csv").then(
(bookingPositions) => {
createBookings(bookingPositions).then(bookings => {
log.trace(bookings);
const sageBookings = createSageBookings(bookings);
writeSageBookings("out.csv", sageBookings);
var confirmBookings = inquirer.createPromptModule();
confirmBookings([{
type: "confirm",
name: "confirmBookings",
message: "Do you really want to book this?"
}]).then(answers => {
if (answers.confirmBookings) {
const bar = new ProgressBar('Booking times... [:bar] :current/:total :etas', {
total: sageBookings.length
});
const sageBooker = new SageBooker();
sageBooker.on('data', async (data) => {
bar.tick();
// log.info(data);
}).on('end', async () => {
const endTime = performance();
log.info("\nDone. This process took you " + ((endTime - startTime) / 1000 / 60).toFixed(2) + " minutes.");
});
const startTime = performance();
sageBooker.bookProjects("out.csv", {
interactiveMode: true,
screenshotDir: createScreenshotDirectory()
});
}
});
});
}).catch(message => {
log.error(message);
});
}
});