Skip to content

Commit 38236c9

Browse files
Remove main file if empty during hex creation
This creates a "clean" MicroPython hex. When the REPL is launched it already includes "from microbit import *".
1 parent bc195f3 commit 38236c9

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

src/fs/fs.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/project/project-actions.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,6 @@ export class ProjectActions {
499499
throw new Error("Device connection doesn't support flash");
500500
}
501501

502-
this.logging.event({
503-
type: "flash",
504-
detail: await this.projectStats(),
505-
});
506-
507502
if (this.device.status === ConnectionStatus.NOT_SUPPORTED) {
508503
this.webusbNotSupportedError(finalFocusRef);
509504
return;
@@ -552,6 +547,12 @@ export class ProjectActions {
552547
this.handleWebUSBError(e, ConnectionAction.FLASH, finalFocusRef);
553548
}
554549
}
550+
551+
// Get the project stats after flashing as this will remove the main file if empty.
552+
this.logging.event({
553+
type: "flash",
554+
detail: await this.projectStats(),
555+
});
555556
};
556557

557558
/**
@@ -561,11 +562,6 @@ export class ProjectActions {
561562
finalFocusRef: FinalFocusRef,
562563
saveViaWebUsbNotSupported?: boolean
563564
) => {
564-
this.logging.event({
565-
type: "save",
566-
detail: await this.projectStats(),
567-
});
568-
569565
if (!(await this.ensureProjectName(finalFocusRef))) {
570566
return;
571567
}
@@ -581,6 +577,13 @@ export class ProjectActions {
581577
});
582578
return;
583579
}
580+
581+
// Get the project stats after hex creation as this will remove the main file if empty.
582+
this.logging.event({
583+
type: "save",
584+
detail: await this.projectStats(),
585+
});
586+
584587
const blob = new Blob([download], {
585588
type: "application/octet-stream",
586589
});

0 commit comments

Comments
 (0)