Skip to content

Commit 2004612

Browse files
committed
Update micropython.js
1 parent 6895749 commit 2004612

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

micropython.js

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ class MicroPythonBoard {
3232
async open(device) {
3333
if (device) {
3434
this.device = device
35-
}
36-
if (!this.device) {
37-
throw new Error(`No device specified`)
35+
} else {
36+
return Promise.reject(
37+
new Error(`No device specified`)
38+
)
3839
}
3940
if (this.serial && this.serial.isOpen) {
4041
await this.serial.close()
@@ -73,8 +74,10 @@ class MicroPythonBoard {
7374
timeout = null,
7475
data_consumer = () => false
7576
} = options || {}
77+
const parser = this.serial.pipe(
78+
new ReadlineParser({ delimiter: ending })
79+
)
7680
return new Promise((resolve, reject) => {
77-
const parser = this.serial.pipe(new ReadlineParser({ delimiter: ending }))
7881
let waiting = 0
7982
if (timeout) {
8083
waiting = setTimeout(() => {
@@ -196,11 +199,31 @@ class MicroPythonBoard {
196199
return Promise.reject()
197200
}
198201

199-
async fs_ls() {
202+
async fs_exists(filePath) {
203+
filePath = filePath || ''
204+
let command = `try:\n`
205+
command += ` f = open("${filePath}", "r")\n`
206+
command += ` print(1)\n`
207+
command += `except OSError:\n`
208+
command += ` print(0)\n`
200209
await this.enter_raw_repl()
201-
const output = await this.exec_raw({
202-
command: `import uos\nprint(uos.listdir())`
203-
})
210+
let output = await this.exec_raw({ command: command })
211+
await this.exit_raw_repl()
212+
// Extract output
213+
output = output.split('OK')
214+
let result = output[2] || ''
215+
return Promise.resolve(result[0] === '1')
216+
}
217+
218+
async fs_ls(folderPath) {
219+
folderPath = folderPath || ''
220+
let command = `import uos\n`
221+
command += `try:\n`
222+
command += ` print(uos.listdir("${folderPath}"))\n`
223+
command += `except OSError:\n`
224+
command += ` print([])\n`
225+
await this.enter_raw_repl()
226+
const output = await this.exec_raw({ command: command })
204227
await this.exit_raw_repl()
205228
return Promise.resolve(output)
206229
}
@@ -267,29 +290,34 @@ class MicroPythonBoard {
267290
const output = await this.exec_raw({
268291
command: `import uos\nuos.mkdir('${filePath}')`
269292
})
270-
console.log(output)
271293
return this.exit_raw_repl()
272294
}
273295
return Promise.reject()
274296
}
275297

276298
async fs_rmdir() {
277299
if (filePath) {
300+
let command = `import uos\n`
301+
command += `try:\n`
302+
command += ` uos.rmdir("${filePath}")\n`
303+
command += `except OSError:\n`
304+
command += ` print(0)\n`
278305
await this.enter_raw_repl()
279-
const output = await this.exec_raw({
280-
command: `import uos\nuos.rmdir('${filePath}')`
281-
})
306+
const output = await this.exec_raw({ command: command })
282307
return this.exit_raw_repl()
283308
}
284309
return Promise.reject()
285310
}
286311

287312
async fs_rm(filePath) {
288313
if (filePath) {
314+
let command = `import uos\n`
315+
command += `try:\n`
316+
command += ` uos.remove("${filePath}")\n`
317+
command += `except OSError:\n`
318+
command += ` print(0)\n`
289319
await this.enter_raw_repl()
290-
const output = await this.exec_raw({
291-
command: `import uos\nuos.remove('${filePath}')`
292-
})
320+
const output = await this.exec_raw({ command: command })
293321
return this.exit_raw_repl()
294322
}
295323
return Promise.reject()

0 commit comments

Comments
 (0)