@@ -25,9 +25,10 @@ class MicroPythonBoard {
25
25
async open ( device ) {
26
26
if ( device ) {
27
27
this . device = device
28
- }
29
- if ( ! this . device ) {
30
- throw new Error ( `No device specified` )
28
+ } else {
29
+ return Promise . reject (
30
+ new Error ( `No device specified` )
31
+ )
31
32
}
32
33
if ( this . serial && this . serial . isOpen ) {
33
34
await this . serial . close ( )
@@ -66,8 +67,10 @@ class MicroPythonBoard {
66
67
timeout = null ,
67
68
data_consumer = ( ) => false
68
69
} = options || { }
70
+ const parser = this . serial . pipe (
71
+ new ReadlineParser ( { delimiter : ending } )
72
+ )
69
73
return new Promise ( ( resolve , reject ) => {
70
- const parser = this . serial . pipe ( new ReadlineParser ( { delimiter : ending } ) )
71
74
let waiting = 0
72
75
if ( timeout ) {
73
76
waiting = setTimeout ( ( ) => {
@@ -189,11 +192,31 @@ class MicroPythonBoard {
189
192
return Promise . reject ( )
190
193
}
191
194
192
- async fs_ls ( ) {
195
+ async fs_exists ( filePath ) {
196
+ filePath = filePath || ''
197
+ let command = `try:\n`
198
+ command += ` f = open("${ filePath } ", "r")\n`
199
+ command += ` print(1)\n`
200
+ command += `except OSError:\n`
201
+ command += ` print(0)\n`
193
202
await this . enter_raw_repl ( )
194
- const output = await this . exec_raw ( {
195
- command : `import uos\nprint(uos.listdir())`
196
- } )
203
+ let output = await this . exec_raw ( { command : command } )
204
+ await this . exit_raw_repl ( )
205
+ // Extract output
206
+ output = output . split ( 'OK' )
207
+ let result = output [ 2 ] || ''
208
+ return Promise . resolve ( result [ 0 ] === '1' )
209
+ }
210
+
211
+ async fs_ls ( folderPath ) {
212
+ folderPath = folderPath || ''
213
+ let command = `import uos\n`
214
+ command += `try:\n`
215
+ command += ` print(uos.listdir("${ folderPath } "))\n`
216
+ command += `except OSError:\n`
217
+ command += ` print([])\n`
218
+ await this . enter_raw_repl ( )
219
+ const output = await this . exec_raw ( { command : command } )
197
220
await this . exit_raw_repl ( )
198
221
return Promise . resolve ( output )
199
222
}
@@ -261,29 +284,34 @@ class MicroPythonBoard {
261
284
const output = await this . exec_raw ( {
262
285
command : `import uos\nuos.mkdir('${ filePath } ')`
263
286
} )
264
- console . log ( output )
265
287
return this . exit_raw_repl ( )
266
288
}
267
289
return Promise . reject ( )
268
290
}
269
291
270
292
async fs_rmdir ( ) {
271
293
if ( filePath ) {
294
+ let command = `import uos\n`
295
+ command += `try:\n`
296
+ command += ` uos.rmdir("${ filePath } ")\n`
297
+ command += `except OSError:\n`
298
+ command += ` print(0)\n`
272
299
await this . enter_raw_repl ( )
273
- const output = await this . exec_raw ( {
274
- command : `import uos\nuos.rmdir('${ filePath } ')`
275
- } )
300
+ const output = await this . exec_raw ( { command : command } )
276
301
return this . exit_raw_repl ( )
277
302
}
278
303
return Promise . reject ( )
279
304
}
280
305
281
306
async fs_rm ( filePath ) {
282
307
if ( filePath ) {
308
+ let command = `import uos\n`
309
+ command += `try:\n`
310
+ command += ` uos.remove("${ filePath } ")\n`
311
+ command += `except OSError:\n`
312
+ command += ` print(0)\n`
283
313
await this . enter_raw_repl ( )
284
- const output = await this . exec_raw ( {
285
- command : `import uos\nuos.remove('${ filePath } ')`
286
- } )
314
+ const output = await this . exec_raw ( { command : command } )
287
315
return this . exit_raw_repl ( )
288
316
}
289
317
return Promise . reject ( )
0 commit comments