Merge pull request #17 from makermelissa/main
Improve reading and writing file integrity
This commit is contained in:
commit
1bd2db0508
2 changed files with 12 additions and 4 deletions
|
|
@ -3,7 +3,7 @@
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"registry": "https://npm.pkg.github.com"
|
"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",
|
"description": "A JavaScript Module to help with interfacing to the REPL on CircuitPython Devices over serial",
|
||||||
"main": "repl.js",
|
"main": "repl.js",
|
||||||
"exports": {
|
"exports": {
|
||||||
|
|
|
||||||
14
repl.js
14
repl.js
|
|
@ -101,6 +101,9 @@ with open("${path}", "wb") as f:
|
||||||
async _writeTextFile(path, contents, offset=0, modificationTime=null) {
|
async _writeTextFile(path, contents, offset=0, modificationTime=null) {
|
||||||
// The contents needs to be converted from a UInt8Array to a string
|
// The contents needs to be converted from a UInt8Array to a string
|
||||||
contents = String.fromCharCode.apply(null, contents);
|
contents = String.fromCharCode.apply(null, contents);
|
||||||
|
// Preserve slashes and slash chracters (must be first)
|
||||||
|
contents = contents.replace(/\\/g, '\\\\');
|
||||||
|
// Preserve quotes
|
||||||
contents = contents.replace(/"/g, '\\"');
|
contents = contents.replace(/"/g, '\\"');
|
||||||
|
|
||||||
let code = `
|
let code = `
|
||||||
|
|
@ -163,17 +166,22 @@ with open("${path}", "rb") as f:
|
||||||
|
|
||||||
async _readTextFile(path) {
|
async _readTextFile(path) {
|
||||||
try {
|
try {
|
||||||
|
// Read and print out the contents of the file within backtick delimiters
|
||||||
let code = `
|
let code = `
|
||||||
with open("${path}", "r") as f:
|
with open("${path}", "r") as f:
|
||||||
print(f.read())
|
print("\`" + f.read() + "\`")
|
||||||
`;
|
`;
|
||||||
let result = await this._repl.runCode(code);
|
let result = await this._repl.runCode(code);
|
||||||
if (await this._checkReplErrors()) {
|
if (await this._checkReplErrors()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove last 2 bytes from the result because \r\n is added to the end
|
// Strip down to code within first and last backtick delimiters
|
||||||
return result.slice(0, -2);
|
let sliceStart = result.indexOf("`") + 1;
|
||||||
|
let sliceEnd = result.lastIndexOf("`");
|
||||||
|
result = result.slice(sliceStart, sliceEnd);
|
||||||
|
|
||||||
|
return result;
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue