|
1 | 1 | // const {xsd} = require("./schema-for-validate") |
2 | 2 | const xmllint = require('xmllint'); |
3 | | -const { app, BrowserWindow, ipcMain, globalShortcut } = require("electron"); |
| 3 | +const { app, BrowserWindow, ipcMain, globalShortcut, dialog, shell } = require("electron"); |
4 | 4 | const path = require("node:path"); |
5 | 5 | const fs = require("fs"); |
6 | 6 | const xmlbuilder = require("xmlbuilder"); |
7 | 7 | const currentDate = new Date(Date()); |
8 | 8 | const isoWithoutMsOrZ = currentDate.toISOString().split('.')[0]; |
9 | 9 | const dateOnlyString = isoWithoutMsOrZ.replace(/T.*/, ''); |
10 | 10 | const { Worker } = require('worker_threads'); |
| 11 | +const os = require('os'); |
11 | 12 |
|
| 13 | +const log = require('electron-log'); |
| 14 | +const { error } = require('node:console'); |
12 | 15 |
|
| 16 | +log.info('Application starting...'); |
| 17 | + |
| 18 | +process.on('uncaughtException', (error) => { |
| 19 | + log.error('Uncaught Exception:', error); |
| 20 | +}); |
| 21 | + |
| 22 | +const tempDir = path.join(os.tmpdir(), `electron-${app.name}-xmls`); |
| 23 | +let XMLfilePath = "" |
| 24 | +let versionForExport = "" |
| 25 | +const formatDateTime = (date) => { |
| 26 | + const yyyymmdd = date.toISOString().split('T')[0].replace(/-/g, ''); |
| 27 | + const hhmmss = date.toTimeString().split(' ')[0].replace(/:/g, ''); |
| 28 | + return `${yyyymmdd}-${hhmmss}`; |
| 29 | +}; |
13 | 30 | let xmlBase = { |
14 | 31 |
|
15 | 32 | Header: { |
@@ -72,6 +89,7 @@ app.whenReady().then(() => { |
72 | 89 |
|
73 | 90 | ipcMain.on("upload-csv", (event, dataArray, version) => { |
74 | 91 | try { |
| 92 | + versionForExport = version |
75 | 93 | if (dataArray.some((learner, learnerIndex) => |
76 | 94 | learner.some((item, index) => { |
77 | 95 | let exceptionIndices = [0, 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]; |
@@ -478,68 +496,104 @@ console.log(result); // Outputs: "ab-cd"*/ |
478 | 496 | .att('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance') |
479 | 497 | .end({ pretty: true }); |
480 | 498 |
|
481 | | - const formatDateTime = (date) => { |
482 | | - const yyyymmdd = date.toISOString().split('T')[0].replace(/-/g, ''); |
483 | | - const hhmmss = date.toTimeString().split(' ')[0].replace(/:/g, ''); |
484 | | - return `${yyyymmdd}-${hhmmss}`; |
485 | | - }; |
| 499 | + |
486 | 500 |
|
487 | 501 |
|
488 | | - |
| 502 | + XMLfilePath = path.join(tempDir, `ILR-10085696-${version.split('.')[0]}-${formatDateTime(currentDate)}-01.xml`) |
489 | 503 |
|
490 | | - fs.writeFile(`ILR-10085696-${version.split('.')[0]}-${formatDateTime(currentDate)}-01.xml`, xml, (err) => { |
| 504 | + fs.writeFile(XMLfilePath, xml, (err) => { |
491 | 505 | if (err) { |
492 | 506 | console.error(err); |
493 | 507 | event.reply('xml-creation-failed', err.message); |
494 | 508 | } else { |
495 | 509 | console.log("The XML file was saved successfully."); |
496 | 510 | event.reply('xml-created', `ILR-10085696-${version.split('.')[0]}-${formatDateTime(currentDate)}-01.xml`); |
| 511 | + log.info('xml file created'); |
| 512 | + |
497 | 513 | } |
498 | 514 |
|
499 | 515 | }); |
500 | | - let xsd = fs.readFileSync("ILR-2024-25-schemafile-January.xsd", 'utf-8'); |
| 516 | + let xsd = fs.readFileSync(path.join(__dirname, "ILR-2024-25-schemafile-January.xsd"), 'utf-8'); |
| 517 | + log.info('schema declared'); |
501 | 518 |
|
502 | 519 |
|
503 | 520 |
|
504 | | -const worker = new Worker('./xmlValidator.js', { |
505 | | - workerData: { xml, xsd } |
506 | | -}); |
| 521 | + log.info('Attempting to create worker...'); |
| 522 | + // log.info('xml before worker creation ', xml, ' xsd before worker creation ', xsd) |
| 523 | + const worker = new Worker(path.join(__dirname, 'xmlValidator.js'), { |
| 524 | + workerData: { xml, xsd } |
| 525 | + }); |
| 526 | + log.info('Worker created successfully'); |
| 527 | + |
507 | 528 |
|
| 529 | +log.info('worker created') |
508 | 530 | worker.on('message', (result) => { |
| 531 | + log.info('information received') |
| 532 | + |
509 | 533 | if (result.valid) { |
510 | 534 | console.log("The XML is valid!"); |
| 535 | + |
511 | 536 | // event.reply('xml-validation-success', result); |
512 | 537 | } else { |
| 538 | + log.info("results being sent ", result) |
513 | 539 | event.reply('xml-validation-errors', result); |
| 540 | + |
514 | 541 | } |
515 | 542 | }); |
516 | 543 |
|
517 | 544 | worker.on('error', (error) => { |
518 | 545 | console.error("Worker error:", error); |
| 546 | + log.error('Worker error:', error); |
519 | 547 | event.reply('xml-validation-errors', [error.message]); |
520 | 548 | }); |
521 | 549 |
|
522 | 550 | worker.on('exit', (code) => { |
523 | 551 | if (code !== 0) { |
524 | 552 | console.error(`Worker stopped with exit code ${code}`); |
| 553 | + log.error(`Worker stopped with exit code ${code}`) |
| 554 | + |
525 | 555 | } |
526 | 556 | }); |
527 | 557 |
|
528 | | - |
529 | | - |
530 | | - |
531 | | - |
532 | | - |
533 | | - xml = null; |
534 | | - xsd = null; |
535 | 558 |
|
536 | 559 |
|
537 | 560 | } |
538 | 561 | } catch (error) { |
539 | 562 | console.error("An error occurred during XML validation:", error); |
540 | 563 | } |
| 564 | +log.info('end of csv upload') |
541 | 565 | }); |
542 | 566 |
|
| 567 | +ipcMain.on("openSave",event => { |
| 568 | +async function saveDialouge() { |
| 569 | + |
| 570 | + try { |
| 571 | + |
| 572 | + |
| 573 | + // Show save dialog |
| 574 | + const result = await dialog.showSaveDialog({ |
| 575 | + title: 'Export XML File', |
| 576 | + defaultPath: path.join(app.getPath('documents'), `ILR-10085696-${versionForExport.split('.')[0]}-${formatDateTime(currentDate)}-01.xml`), |
| 577 | + filters: [ |
| 578 | + { name: 'XML Files', extensions: ['xml'] } |
| 579 | + ] |
| 580 | + }); |
| 581 | +console.log('result.cancled', result.canceled, " result filepath " , result.filePath) |
| 582 | + // If user didn't cancel |
| 583 | + if (!result.canceled && result.filePath) { |
| 584 | + // Copy from temp to chosen location |
| 585 | + console.log('copy file from ',XMLfilePath, ' to ', result.filePath) |
| 586 | + await fs.promises.copyFile(XMLfilePath, result.filePath); |
| 587 | + } |
| 588 | + else{ |
| 589 | + console.log('result canceled or has no file path') |
| 590 | + } |
| 591 | +} catch (error) { |
| 592 | + console.error('Error exporting temporary XML:', error); |
| 593 | + throw error; |
| 594 | +}} |
| 595 | +saveDialouge(); |
| 596 | +}); |
543 | 597 |
|
544 | 598 |
|
545 | 599 | app.on("window-all-closed", () => { |
|
0 commit comments