-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (108 loc) · 4.3 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
const pdf = require('./pdf');
const excel = require('./excel');
const colors = require('colors');
const moment = require('moment-mini');
const file = require('./files');
const input = require('./input');
const excelPassListFile = './pass-list.xlsx';
const pdfTemplates = ['early', 'backstage', 'early-backstage'];
const passesPerPage = 10;
async function main() {
const timestamp = moment();
const rows = excel.getRows(excelPassListFile);
const accessEarly = rows.filter((r) => !r.Generated && simplifyString(r.AccessRequired) === 'early');
const accessBackstage = rows.filter((r) => !r.Generated && simplifyString(r.AccessRequired) === 'backstage');
const accessEarlyBackstage = rows.filter((r) => !r.Generated && simplifyString(r.AccessRequired) === 'earlybackstage');
if (rows.filter((r) => !r.Generated).length > 0){
console.log(`Ready to generate:
${colors.green(accessEarly.length.toString().padStart(3, '0'))} early passes
${colors.green(accessBackstage.length.toString().padStart(3, '0'))} backstage passes
${colors.green(accessEarlyBackstage.length.toString().padStart(3, '0'))} early & backstage passes`);
await input.askQuestion('Press enter to continue:');
accessEarly.length > 0 && generateFile(pdfTemplates[0], accessEarly, timestamp);
accessBackstage.length > 0 && generateFile(pdfTemplates[1], accessBackstage, timestamp);
accessEarlyBackstage.length > 0 && generateFile(pdfTemplates[2], accessEarlyBackstage, timestamp);
markRowsAsGenerated([...accessEarly, ...accessBackstage, ...accessEarlyBackstage], timestamp);
await excel.saveRows(rows, excelPassListFile);
return await input.askQuestion('Done. Press enter to quit.');
}
await input.askQuestion('No records to process. Press enter to quit.');
}
function markRowsAsGenerated(passList, timestamp) {
passList.map(pass => pass.Generated = timestamp.toDate());
}
function generateFile(templateFile, passList, timestamp) {
const totalCount = passList.length;
let page = 1;
const generatedFiles = [];
const outputFileName = `./export/${timestamp.format('YYYYMMDD-HHmmss')}-${templateFile}.pdf`;
for(let i=0; i<totalCount; i+=passesPerPage){
let replacements = generateReplacements(passList, i, passesPerPage);
const generateFilename = `./temp/${templateFile}-${page}.pdf`;
pdf.replaceText(`./templates/${templateFile}.pdf`, generateFilename, replacements);
page+=1;
generatedFiles.push(generateFilename);
}
pdf.mergePdfFiles(generatedFiles, outputFileName);
console.log(`Generated ${colors.green(totalCount.toString().padStart(3,'0'))} passes into ${colors.green(outputFileName)}`)
}
function generateReplacements(passList, pageIndex, passesPerPage) {
let replacements = [];
for(let i=0; i<passesPerPage; i++){
replacements = [...replacements, ...generatePassReplacement(passList, pageIndex + i, i)];
}
return replacements;
}
function generatePassReplacement(passList, passIndex, tokenIndex){
const pass = passList[passIndex];
const token = tokenIndex.toString();
if (pass){
return [
{
regEx: `n${token}`,
text: pass.Person
},
{
regEx: `d${token}`,
text: pass.Department
}
];
}
return [
{
regEx: `n${token}`,
text: 'THIS PASS IS VOID'
},
{
regEx: `d${token}`,
text: 'INVALID - DESTROY'
}
];
}
/**
* Simplify string to protect against users entering lowercase or incorrect values
* @param {string} input
*/
function simplifyString(input) {
return input.toLowerCase().replace(/ /g, '').replace('&', '').replace('and', '');
}
function init() {
try {
pdfTemplates.map((filename) => {
const fileAndPath = `./templates/${filename}.pdf`;
if (!file.checkExists(fileAndPath)) {
throw new Error(`File '${fileAndPath}' cannot be found!`);
}
});
file.createDirectory('./temp');
file.createDirectory('./export');
file.emptyDirectory('temp');
} catch (e) {
console.log(colors.red(e.message));
return false;
}
return true;
}
if (init()) {
main();
}