@@ -32,9 +32,10 @@ class MicroPythonBoard {
32
32
async open ( device ) {
33
33
if ( device ) {
34
34
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
+ )
38
39
}
39
40
if ( this . serial && this . serial . isOpen ) {
40
41
await this . serial . close ( )
@@ -73,8 +74,10 @@ class MicroPythonBoard {
73
74
timeout = null ,
74
75
data_consumer = ( ) => false
75
76
} = options || { }
77
+ const parser = this . serial . pipe (
78
+ new ReadlineParser ( { delimiter : ending } )
79
+ )
76
80
return new Promise ( ( resolve , reject ) => {
77
- const parser = this . serial . pipe ( new ReadlineParser ( { delimiter : ending } ) )
78
81
let waiting = 0
79
82
if ( timeout ) {
80
83
waiting = setTimeout ( ( ) => {
@@ -196,11 +199,31 @@ class MicroPythonBoard {
196
199
return Promise . reject ( )
197
200
}
198
201
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`
200
209
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 } )
204
227
await this . exit_raw_repl ( )
205
228
return Promise . resolve ( output )
206
229
}
@@ -267,29 +290,34 @@ class MicroPythonBoard {
267
290
const output = await this . exec_raw ( {
268
291
command : `import uos\nuos.mkdir('${ filePath } ')`
269
292
} )
270
- console . log ( output )
271
293
return this . exit_raw_repl ( )
272
294
}
273
295
return Promise . reject ( )
274
296
}
275
297
276
298
async fs_rmdir ( ) {
277
299
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`
278
305
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 } )
282
307
return this . exit_raw_repl ( )
283
308
}
284
309
return Promise . reject ( )
285
310
}
286
311
287
312
async fs_rm ( filePath ) {
288
313
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`
289
319
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 } )
293
321
return this . exit_raw_repl ( )
294
322
}
295
323
return Promise . reject ( )
0 commit comments