@@ -406,7 +406,10 @@ export class FileSystem extends TypedEventTarget<EventMap> {
406
406
407
407
async statistics ( ) : Promise < Statistics > {
408
408
const fs = await this . initialize ( ) ;
409
- const currentMainFile = fs . readBytes ( MAIN_FILE ) ;
409
+ let currentMainFile ;
410
+ if ( fs . exists ( MAIN_FILE ) ) {
411
+ currentMainFile = fs . readBytes ( MAIN_FILE ) ;
412
+ }
410
413
const files = fs . ls ( ) ;
411
414
let numMagicModules = 0 ;
412
415
for ( const file of files ) {
@@ -418,12 +421,13 @@ export class FileSystem extends TypedEventTarget<EventMap> {
418
421
return {
419
422
files : files . length ,
420
423
storageUsed : fs . getStorageUsed ( ) ,
421
- lines :
422
- this . cachedInitialProject &&
423
- this . cachedInitialProject . files [ MAIN_FILE ] ===
424
- fromByteArray ( currentMainFile )
425
- ? undefined
426
- : lineNumFromUint8Array ( currentMainFile ) ,
424
+ lines : ! currentMainFile
425
+ ? 0
426
+ : this . cachedInitialProject &&
427
+ this . cachedInitialProject . files [ MAIN_FILE ] ===
428
+ fromByteArray ( currentMainFile )
429
+ ? undefined
430
+ : lineNumFromUint8Array ( currentMainFile ) ,
427
431
magicModules : numMagicModules ,
428
432
} ;
429
433
}
@@ -450,8 +454,18 @@ export class FileSystem extends TypedEventTarget<EventMap> {
450
454
) ;
451
455
}
452
456
457
+ private async removeMainFileIfEmpty ( fs : MicropythonFsHex ) : Promise < void > {
458
+ if ( fs . exists ( MAIN_FILE ) ) {
459
+ const currentMainFile = await fs . read ( MAIN_FILE ) ;
460
+ if ( ! currentMainFile ) {
461
+ fs . remove ( MAIN_FILE ) ;
462
+ }
463
+ }
464
+ }
465
+
453
466
async toHexForSave ( ) : Promise < string > {
454
467
const fs = await this . initialize ( ) ;
468
+ await this . removeMainFileIfEmpty ( fs ) ;
455
469
return fs . getUniversalHex ( ) ;
456
470
}
457
471
@@ -465,6 +479,7 @@ export class FileSystem extends TypedEventTarget<EventMap> {
465
479
try {
466
480
const fs = await this . initialize ( ) ;
467
481
const boardId = BoardId . forVersion ( boardVersion ) . id ;
482
+ await this . removeMainFileIfEmpty ( fs ) ;
468
483
return fs . getIntelHex ( boardId ) ;
469
484
} catch ( e : any ) {
470
485
throw new FlashDataError ( e . message ) ;
0 commit comments