@@ -406,7 +406,10 @@ export class FileSystem extends TypedEventTarget<EventMap> {
406406
407407 async statistics ( ) : Promise < Statistics > {
408408 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+ }
410413 const files = fs . ls ( ) ;
411414 let numMagicModules = 0 ;
412415 for ( const file of files ) {
@@ -418,12 +421,13 @@ export class FileSystem extends TypedEventTarget<EventMap> {
418421 return {
419422 files : files . length ,
420423 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 ) ,
427431 magicModules : numMagicModules ,
428432 } ;
429433 }
@@ -450,8 +454,18 @@ export class FileSystem extends TypedEventTarget<EventMap> {
450454 ) ;
451455 }
452456
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+
453466 async toHexForSave ( ) : Promise < string > {
454467 const fs = await this . initialize ( ) ;
468+ await this . removeMainFileIfEmpty ( fs ) ;
455469 return fs . getUniversalHex ( ) ;
456470 }
457471
@@ -465,6 +479,7 @@ export class FileSystem extends TypedEventTarget<EventMap> {
465479 try {
466480 const fs = await this . initialize ( ) ;
467481 const boardId = BoardId . forVersion ( boardVersion ) . id ;
482+ await this . removeMainFileIfEmpty ( fs ) ;
468483 return fs . getIntelHex ( boardId ) ;
469484 } catch ( e : any ) {
470485 throw new FlashDataError ( e . message ) ;
0 commit comments