@@ -9,14 +9,10 @@ const dateOnlyString = isoWithoutMsOrZ.replace(/T.*/, '');
99const { Worker } = require ( 'worker_threads' ) ;
1010const os = require ( 'os' ) ;
1111
12- const log = require ( 'electron-log' ) ;
13- const { error } = require ( 'node:console' ) ;
1412
15- log . info ( 'Application starting...' ) ;
1613
17- process . on ( 'uncaughtException' , ( error ) => {
18- log . error ( 'Uncaught Exception:' , error ) ;
19- } ) ;
14+
15+
2016
2117const tempDir = path . join ( os . tmpdir ( ) , `electron-ilr_file_creator-xmls` ) ;
2218let XMLfilePath = ""
@@ -26,6 +22,19 @@ const formatDateTime = (date) => {
2622 const hhmmss = date . toTimeString ( ) . split ( ' ' ) [ 0 ] . replace ( / : / g, '' ) ;
2723 return `${ yyyymmdd } -${ hhmmss } ` ;
2824} ;
25+ function convertAcademicYear ( yearString ) {
26+ // Validate input
27+ if ( ! / ^ \d { 4 } $ / . test ( yearString ) ) {
28+ throw new Error ( 'Invalid input. Please provide a 4-digit year string.' ) ;
29+ }
30+
31+ // Extract first two and last two digits
32+ const firstTwoDigits = yearString . slice ( 0 , 2 ) ;
33+ const lastTwoDigits = yearString . slice ( 2 ) ;
34+
35+ // Convert to formatted academic year
36+ return `20${ firstTwoDigits } -${ lastTwoDigits } ` ;
37+ }
2938let xmlBase = {
3039
3140 Header : {
@@ -75,7 +84,7 @@ function createWindow() {
7584}
7685
7786ipcMain . on ( 'log-message' , ( event , message ) => {
78- console . log ( 'Renderer:' , message ) ; // Log to terminal
87+ console . log ( 'Renderer:' , message ) ;
7988} ) ;
8089app . whenReady ( ) . then ( ( ) => {
8190 createWindow ( ) ;
@@ -471,12 +480,8 @@ for (let i = 1; i < dataArray.length; i++) {
471480 } , {
472481 encoding : 'utf-8' ,
473482 } )
474- //make version specific
475- /*something like this
476- let str = "abcd";
477- let result = str.replace(/^(.)(.)(.)(.?)$/, "$1$2-$3$4");
478- console.log(result); // Outputs: "ab-cd"*/
479- . att ( 'xmlns' , 'ESFA/ILR/2024-25' )
483+
484+ . att ( 'xmlns' , `ESFA/ILR/${ convertAcademicYear ( version . split ( '.' ) [ 0 ] ) } ` )
480485 . att ( 'xmlns:xsi' , 'http://www.w3.org/2001/XMLSchema-instance' )
481486 . end ( { pretty : true } ) ;
482487
@@ -487,54 +492,46 @@ console.log(result); // Outputs: "ab-cd"*/
487492
488493 fs . writeFile ( XMLfilePath , xml , ( err ) => {
489494 if ( err ) {
490- console . error ( err ) ;
491495 event . reply ( 'xml-creation-failed' , err . message ) ;
492496 } else {
493- console . log ( "The XML file was saved successfully." ) ;
494497 event . reply ( 'xml-created' , `ILR-10085696-${ version . split ( '.' ) [ 0 ] } -${ formatDateTime ( currentDate ) } -01.xml` ) ;
495- log . info ( 'xml file created' ) ;
496498
497499 }
498500
499501 } ) ;
500502 let xsd = fs . readFileSync ( path . join ( __dirname , "ILR-2024-25-schemafile-January.xsd" ) , 'utf-8' ) ;
501- log . info ( 'schema declared' ) ;
502503
503504
504-
505- log . info ( 'Attempting to create worker...' ) ;
506- // log.info('xml before worker creation ', xml, ' xsd before worker creation ', xsd)
505+ // This worker is required because the library used is old and busted and causes a memory leak.
506+ //The worker can validate the xml before the memory leak causes it to crash but if we
507+ // used the library in the main process the crash would shut down the app
508+ // We can check if there is a better library for validating xmls against schemas created in the future
509+ // but at the time of creation there was not (seems to work better outside the developer environment anyway)
507510 const worker = new Worker ( path . join ( __dirname , 'xmlValidator.js' ) , {
508511 workerData : { xml, xsd }
509512 } ) ;
510- log . info ( 'Worker created successfully' ) ;
511513
512514
513- log . info ( 'worker created' )
514515worker . on ( 'message' , ( result ) => {
515- log . info ( 'information received' )
516-
516+ // this currently never happens because the way the DFE formated their XMLs always generates two warnings but if they
517+ // fix that in future versions its worth thinking about.
517518 if ( result . valid ) {
518- console . log ( "The XML is valid!" ) ;
519519
520520 // event.reply('xml-validation-success', result);
521521 } else {
522- log . info ( "results being sent " , result )
523522 event . reply ( 'xml-validation-errors' , result ) ;
524523
525524 }
526525} ) ;
527526
528527worker . on ( 'error' , ( error ) => {
529528 console . error ( "Worker error:" , error ) ;
530- log . error ( 'Worker error:' , error ) ;
531529 event . reply ( 'xml-validation-errors' , [ error . message ] ) ;
532530} ) ;
533531
534532worker . on ( 'exit' , ( code ) => {
535533 if ( code !== 0 ) {
536534 console . error ( `Worker stopped with exit code ${ code } ` ) ;
537- log . error ( `Worker stopped with exit code ${ code } ` )
538535
539536 }
540537} ) ;
@@ -545,7 +542,6 @@ worker.on('exit', (code) => {
545542} catch ( error ) {
546543 console . error ( "An error occurred during XML validation:" , error ) ;
547544}
548- log . info ( 'end of csv upload' )
549545} ) ;
550546
551547ipcMain . on ( "openSave" , event => {
0 commit comments