diff --git a/src/fs/fs.ts b/src/fs/fs.ts index ba93f24e5..e81b01497 100644 --- a/src/fs/fs.ts +++ b/src/fs/fs.ts @@ -406,7 +406,10 @@ export class FileSystem extends TypedEventTarget { async statistics(): Promise { const fs = await this.initialize(); - const currentMainFile = fs.readBytes(MAIN_FILE); + let currentMainFile; + if (fs.exists(MAIN_FILE)) { + currentMainFile = fs.readBytes(MAIN_FILE); + } const files = fs.ls(); let numMagicModules = 0; for (const file of files) { @@ -418,12 +421,13 @@ export class FileSystem extends TypedEventTarget { return { files: files.length, storageUsed: fs.getStorageUsed(), - lines: - this.cachedInitialProject && - this.cachedInitialProject.files[MAIN_FILE] === - fromByteArray(currentMainFile) - ? undefined - : lineNumFromUint8Array(currentMainFile), + lines: !currentMainFile + ? 0 + : this.cachedInitialProject && + this.cachedInitialProject.files[MAIN_FILE] === + fromByteArray(currentMainFile) + ? undefined + : lineNumFromUint8Array(currentMainFile), magicModules: numMagicModules, }; } @@ -450,8 +454,18 @@ export class FileSystem extends TypedEventTarget { ); } + private async removeMainFileIfEmpty(fs: MicropythonFsHex): Promise { + if (fs.exists(MAIN_FILE)) { + const currentMainFile = await fs.read(MAIN_FILE); + if (!currentMainFile) { + fs.remove(MAIN_FILE); + } + } + } + async toHexForSave(): Promise { const fs = await this.initialize(); + await this.removeMainFileIfEmpty(fs); return fs.getUniversalHex(); } @@ -465,6 +479,7 @@ export class FileSystem extends TypedEventTarget { try { const fs = await this.initialize(); const boardId = BoardId.forVersion(boardVersion).id; + await this.removeMainFileIfEmpty(fs); return fs.getIntelHex(boardId); } catch (e: any) { throw new FlashDataError(e.message); diff --git a/src/project/project-actions.tsx b/src/project/project-actions.tsx index 6ac24c17e..f66c50278 100644 --- a/src/project/project-actions.tsx +++ b/src/project/project-actions.tsx @@ -499,11 +499,6 @@ export class ProjectActions { throw new Error("Device connection doesn't support flash"); } - this.logging.event({ - type: "flash", - detail: await this.projectStats(), - }); - if (this.device.status === ConnectionStatus.NOT_SUPPORTED) { this.webusbNotSupportedError(finalFocusRef); return; @@ -552,6 +547,12 @@ export class ProjectActions { this.handleWebUSBError(e, ConnectionAction.FLASH, finalFocusRef); } } + + // Get the project stats after flashing as this will remove the main file if empty. + this.logging.event({ + type: "flash", + detail: await this.projectStats(), + }); }; /** @@ -561,11 +562,6 @@ export class ProjectActions { finalFocusRef: FinalFocusRef, saveViaWebUsbNotSupported?: boolean ) => { - this.logging.event({ - type: "save", - detail: await this.projectStats(), - }); - if (!(await this.ensureProjectName(finalFocusRef))) { return; } @@ -581,6 +577,13 @@ export class ProjectActions { }); return; } + + // Get the project stats after hex creation as this will remove the main file if empty. + this.logging.event({ + type: "save", + detail: await this.projectStats(), + }); + const blob = new Blob([download], { type: "application/octet-stream", });