@@ -8,41 +8,75 @@ import { codepy } from './pythontemplates/code'
88import { bootpy } from './pythontemplates/boot'
99import { pog_serialpy } from './pythontemplates/pog_serial'
1010import { keymappy } from './pythontemplates/keymap'
11- import { writePogConfViaSerial } from './index'
11+ import { writePogConfViaSerial , mainWindow } from './index'
1212
13- export const saveConfiguration = ( data : string ) => {
13+ export const saveConfiguration = async ( data : string ) => {
1414 const { pogConfig, serial, writeFirmware } = JSON . parse ( data )
1515 if ( serial ) {
1616 // write by serial to current keyboard
1717 console . log ( 'writing firmware via usb serial' )
1818 writePogConfViaSerial ( JSON . stringify ( pogConfig , null , 0 ) )
19- } else {
20- // write pog.json
21- console . log ( 'writing firmware via usb files' , 'overwriting Firmware:' , writeFirmware )
22- fs . writeFile ( currentKeyboard . path + '/pog.json' , JSON . stringify ( pogConfig , null , 4 ) , ( e ) => {
23- if ( e ) {
24- console . log ( 'error writing pog.json' , e )
25- } else {
26- console . log ( 'pog.json written successfully' )
27- }
28- } )
29-
30- const files = [
31- { name : 'pog.py' , contents : pogpy } ,
32- { name : 'code.py' , contents : codepy } ,
33- { name : 'coordmaphelper.py' , contents : coordmaphelperpy } ,
34- { name : 'customkeys.py' , contents : customkeyspy } ,
35- { name : 'boot.py' , contents : bootpy } ,
36- { name : 'pog_serial.py' , contents : pog_serialpy } ,
37- { name : 'keymap.py' , contents : keymappy } ,
38- { name : 'kb.py' , contents : kbpy }
39- ]
40- for ( const file of files ) {
41- if ( ! fs . existsSync ( currentKeyboard . path + '/' + file . name ) || writeFirmware ) {
42- fs . writeFile ( currentKeyboard . path + '/' + file . name , file . contents , ( ) => {
43- console . log ( file . name + 'File written successfully' )
44- } )
45- }
19+ return
20+ }
21+
22+ // write via mounted USB drive
23+ console . log ( 'writing firmware via usb files' , 'overwriting Firmware:' , writeFirmware )
24+
25+ type WriteTask = { name : string ; path : string ; contents : string }
26+ const tasks : WriteTask [ ] = [ ]
27+
28+ // Always write pog.json
29+ tasks . push ( {
30+ name : 'pog.json' ,
31+ path : currentKeyboard . path + '/pog.json' ,
32+ contents : JSON . stringify ( pogConfig , null , 4 )
33+ } )
34+
35+ const files = [
36+ { name : 'pog.py' , contents : pogpy } ,
37+ { name : 'code.py' , contents : codepy } ,
38+ { name : 'coordmaphelper.py' , contents : coordmaphelperpy } ,
39+ { name : 'customkeys.py' , contents : customkeyspy } ,
40+ { name : 'boot.py' , contents : bootpy } ,
41+ { name : 'pog_serial.py' , contents : pog_serialpy } ,
42+ { name : 'keymap.py' , contents : keymappy } ,
43+ { name : 'kb.py' , contents : kbpy }
44+ ]
45+
46+ for ( const file of files ) {
47+ const targetPath = currentKeyboard . path + '/' + file . name
48+ if ( ! fs . existsSync ( targetPath ) || writeFirmware ) {
49+ tasks . push ( { name : file . name , path : targetPath , contents : file . contents } )
4650 }
4751 }
52+
53+ const total = tasks . length
54+ let completed = 0
55+
56+ for ( const task of tasks ) {
57+ try {
58+ await fs . promises . writeFile ( task . path , task . contents )
59+ completed += 1
60+ mainWindow ?. webContents . send ( 'save-configuration-progress' , {
61+ state : 'writing' ,
62+ filename : task . name ,
63+ completed,
64+ total
65+ } )
66+ } catch ( e ) {
67+ console . error ( `error writing ${ task . name } ` , e )
68+ mainWindow ?. webContents . send ( 'save-configuration-progress' , {
69+ state : 'error' ,
70+ filename : task . name ,
71+ completed,
72+ total
73+ } )
74+ }
75+ }
76+
77+ mainWindow ?. webContents . send ( 'save-configuration-progress' , {
78+ state : 'done' ,
79+ completed,
80+ total
81+ } )
4882}
0 commit comments