Compare commits

...

3 commits

Author SHA1 Message Date
Melissa LeBlanc-Williams
1bd2db0508
Merge pull request #17 from makermelissa/main
Improve reading and writing file integrity
2025-02-12 11:13:54 -08:00
Melissa LeBlanc-Williams
cb07e13320 Merge branch 'main' of https://github.com/adafruit/circuitpython-repl-js 2025-02-12 11:11:49 -08:00
Melissa LeBlanc-Williams
a1bcb77f51 Improve reading and writing file integrity 2025-02-12 11:11:32 -08:00
2 changed files with 12 additions and 4 deletions

View file

@ -3,7 +3,7 @@
"publishConfig": {
"registry": "https://npm.pkg.github.com"
},
"version": "2.0.1",
"version": "3.2.4",
"description": "A JavaScript Module to help with interfacing to the REPL on CircuitPython Devices over serial",
"main": "repl.js",
"exports": {

14
repl.js
View file

@ -101,6 +101,9 @@ with open("${path}", "wb") as f:
async _writeTextFile(path, contents, offset=0, modificationTime=null) {
// The contents needs to be converted from a UInt8Array to a string
contents = String.fromCharCode.apply(null, contents);
// Preserve slashes and slash chracters (must be first)
contents = contents.replace(/\\/g, '\\\\');
// Preserve quotes
contents = contents.replace(/"/g, '\\"');
let code = `
@ -163,17 +166,22 @@ with open("${path}", "rb") as f:
async _readTextFile(path) {
try {
// Read and print out the contents of the file within backtick delimiters
let code = `
with open("${path}", "r") as f:
print(f.read())
print("\`" + f.read() + "\`")
`;
let result = await this._repl.runCode(code);
if (await this._checkReplErrors()) {
return null;
}
// Remove last 2 bytes from the result because \r\n is added to the end
return result.slice(0, -2);
// Strip down to code within first and last backtick delimiters
let sliceStart = result.indexOf("`") + 1;
let sliceEnd = result.lastIndexOf("`");
result = result.slice(sliceStart, sliceEnd);
return result;
} catch(error) {
return null;
}