Skip to content

Commit 8d2a57f

Browse files
upy-fs-hex: On hex files import, only throw overwrite error at the end.
So that all the new files are correctly imported.
1 parent bdcd706 commit 8d2a57f

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/micropython-fs-hex.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,27 @@ export class MicropythonFsHex implements FsInterface {
139139
* instance.
140140
*
141141
* @throws {Error} When there is a problem reading the files from the hex.
142-
* @throws {Error} When a filename already exists in this instance.
142+
* @throws {Error} When a filename already exists in this instance (all other
143+
* files are still imported).
143144
*
144145
* @param intelHex - MicroPython hex string with files.
145146
* @param overwrite - Flag to overwrite existing files in this instance.
146147
* @returns A filename list of added files.
147148
*/
148149
importFilesFromIntelHex(intelHex: string, overwrite?: boolean): string[] {
149150
const files = getIntelHexFiles(intelHex);
151+
const existingFiles: string[] = [];
150152
Object.keys(files).forEach((filename) => {
151153
if (!overwrite && this.exists(filename)) {
152-
throw new Error(`File "${filename}" from hex already exists.`);
154+
existingFiles.push(filename);
155+
} else {
156+
this.write(filename, files[filename]);
153157
}
154-
this.write(filename, files[filename]);
155158
});
159+
// Only throw the error at the end so that all other files are imported
160+
if (existingFiles.length) {
161+
throw new Error(`Files "${existingFiles}" from hex already exists.`);
162+
}
156163
return Object.keys(files);
157164
}
158165

0 commit comments

Comments
 (0)