-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
212 lines (189 loc) · 6.41 KB
/
main.js
File metadata and controls
212 lines (189 loc) · 6.41 KB
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
const fs = require("fs");
const os = require("os");
const path = require("node:path");
const xmlbuilder = require("xmlbuilder");
const xmllint = require("xmllint");
const { pushLearners } = require("./src/utils/pushLearners");
const { Worker } = require("worker_threads");
const {
app,
BrowserWindow,
ipcMain,
globalShortcut,
dialog,
shell
} = require("electron");
// ====== FILE SYSTEM ======
const tempDir = path.join(os.tmpdir(), `electron-ilr_file_creator-xmls`);
if (!fs.existsSync(tempDir)) fs.mkdirSync(tempDir, { recursive: true })
let XMLfilePath = "";
// ====== DATES & VERSIONS ======
const currentDate = new Date(Date());
const isoWithoutMsOrZ = currentDate
.toISOString()
.split(".")[0];
const dateOnlyString = isoWithoutMsOrZ
.replace(/T.*/, "");
const formatDateTime = (date) => {
const yyyymmdd = date
.toISOString()
.split("T")[0]
.replace(/-/g, "");
const hhmmss = date
.toTimeString()
.split(" ")[0]
.replace(/:/g, "");
return `${yyyymmdd}-${hhmmss}`;
};
function convertAcademicYear(yearString) {
if (!/^\d{4}$/.test(yearString)) throw new Error("Invalid input. Please provide a 4-digit year string.");
const firstTwoDigits = yearString.slice(0, 2);
const lastTwoDigits = yearString.slice(2);
return `20${firstTwoDigits}-${lastTwoDigits}`;
}
// ====== XML SCHEMA ======
let versionForExport = "";
let xmlBase = {
Header: {
CollectionDetails: {
Collection: "ILR",
Year: "2324",
FilePreparationDate: dateOnlyString,
},
Source: {
ProtectiveMarking: "OFFICIAL-SENSITIVE-Personal",
UKPRN: "10085696",
SoftwareSupplier: "Education & Skills Funding Agency",
SoftwarePackage: "ILR Learner Entry",
Release: "2324.1.92.0",
SerialNo: "01",
DateTime: isoWithoutMsOrZ,
},
},
LearningProvider: {
UKPRN: "10085696",
},
Learner: [],
};
// ====== START UP ======
ipcMain.on("log-message", (event, message) => { console.log("Renderer:", message) });
function createWindow() {
const win = new BrowserWindow({
width: 1200,
height: 720,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
// TODO: these are bad practice and I should change if time can set isolation to true and set a file allowing only the apis I know I want to use
},
});
win.loadFile("index.html");
globalShortcut.register("CommandOrControl+Shift+I", () => {
win.webContents.toggleDevTools();
});
}
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
// ====== EVENTS ======
ipcMain.on("upload-csv", (event, dataArray, version) => {
try {
versionForExport = version;
xmlBase.Header.Source.Release = version;
xmlBase.Header.CollectionDetails.Year = version.split(".")[0];
// Test for Empty Values
const CheckBoxPattern = /0 checked out/;
for (let I = 1; I < dataArray.length; I++) {
for (let i = 0; i < dataArray[I].length; i++) {
if (CheckBoxPattern.test(dataArray[I][i])) { dataArray[I][i] = "" }
}
}
// ====== MAIN EVENT RIGHT HERE FOLKS ======
pushLearners(dataArray, xmlBase);
// ====== EXPORT ======
let xml = xmlbuilder
.create({ Message: xmlBase }, { encoding: "utf-8" })
.att("xmlns", `ESFA/ILR/${convertAcademicYear(version.split(".")[0])}`)
.att("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
.end({ pretty: true });
XMLfilePath = path.join(
tempDir,
`ILR-10085696-${version.split(".")[0]}-${formatDateTime(currentDate)}-01.xml`,
);
fs.writeFile(XMLfilePath, xml, (err) => {
if (err) {
event.reply("xml-creation-failed", err.message);
} else {
event.reply(
"xml-created",
`ILR-10085696-${version.split(".")[0]}-${formatDateTime(currentDate)}-01.xml`,
);
}
});
let xsd = fs.readFileSync(path.join(__dirname, "schemafile.xsd"), "utf-8");
// ====== WALDO THE WOBBLY WORKER ======
/* TODO: Replace the leaky library
This worker is required because the library used is old and busted and causes a memory leak.
The worker can validate the xml before the memory leak causes it to crash but if we
used the library in the main process the crash would shut down the app
We can check if there is a better library for validating xmls against schemas created in the future
but at the time of creation there was not (seems to work better outside the developer environment anyway)
*/
const worker = new Worker(path.join(__dirname, "xmlValidator.js"), {
workerData: { xml, xsd },
});
worker.on("message", (result) => {
// this currently never happens because the way the DFE formated their XMLs always generates two warnings but if they
// fix that in future versions its worth thinking about.
if (result.valid) {
// event.reply('xml-validation-success', result);
} else {
event.reply("xml-validation-errors", result);
}
});
worker.on("error", (error) => {
console.error("Worker error:", error);
event.reply("xml-validation-errors", [error.message]);
});
worker.on("exit", (code) => {
if (code !== 0) console.error(`Worker stopped with exit code ${code}`)
});
} catch (error) {
console.error("An error occurred during XML validation:", error);
event.reply("xml-creation-failed", error.message);
}
});
ipcMain.on("openSave", (event) => {
async function saveDialogue() {
try {
const result = await dialog.showSaveDialog({
title: "Export XML File",
defaultPath: path.join(
app.getPath("documents"),
`ILR-10085696-${versionForExport.split(".")[0]}-${formatDateTime(currentDate)}-01.xml`,
),
filters: [{ name: "XML Files", extensions: ["xml"] }],
});
console.log(
"result.cancled",
result.canceled,
" result filepath ",
result.filePath,
);
if (!result.canceled && result.filePath) {
console.log("copy file from ", XMLfilePath, " to ", result.filePath);
await fs.promises.copyFile(XMLfilePath, result.filePath);
} else {
console.log("result canceled or has no file path");
}
} catch (error) {
console.error("Error exporting temporary XML:", error);
throw error;
}
}
saveDialogue();
});
app.on("window-all-closed", () => { app.quit() });