@@ -188,6 +188,13 @@ export class SketchesServiceImpl
188
188
}
189
189
190
190
async loadSketch ( uri : string ) : Promise < SketchWithDetails > {
191
+ return this . doLoadSketch ( uri ) ;
192
+ }
193
+
194
+ private async doLoadSketch (
195
+ uri : string ,
196
+ detectInvalidSketchNameError = true
197
+ ) : Promise < SketchWithDetails > {
191
198
const { client, instance } = await this . coreClient ;
192
199
const req = new LoadSketchRequest ( ) ;
193
200
const requestSketchPath = FileUri . fsPath ( uri ) ;
@@ -202,17 +209,19 @@ export class SketchesServiceImpl
202
209
if ( err ) {
203
210
let rejectWith : unknown = err ;
204
211
if ( isNotFoundError ( err ) ) {
205
- const invalidMainSketchFilePath = await isInvalidSketchNameError (
206
- err ,
207
- requestSketchPath
208
- ) ;
209
- if ( invalidMainSketchFilePath ) {
210
- rejectWith = SketchesError . InvalidName (
211
- err . details ,
212
- FileUri . create ( invalidMainSketchFilePath ) . toString ( )
212
+ rejectWith = SketchesError . NotFound ( err . details , uri ) ;
213
+ // detecting the invalid sketch name error is not for free as it requires multiple filesystem access.
214
+ if ( detectInvalidSketchNameError ) {
215
+ const invalidMainSketchFilePath = await isInvalidSketchNameError (
216
+ err ,
217
+ requestSketchPath
213
218
) ;
214
- } else {
215
- rejectWith = SketchesError . NotFound ( err . details , uri ) ;
219
+ if ( invalidMainSketchFilePath ) {
220
+ rejectWith = SketchesError . InvalidName (
221
+ err . details ,
222
+ FileUri . create ( invalidMainSketchFilePath ) . toString ( )
223
+ ) ;
224
+ }
216
225
}
217
226
}
218
227
reject ( rejectWith ) ;
@@ -318,7 +327,7 @@ export class SketchesServiceImpl
318
327
319
328
let sketch : Sketch | undefined = undefined ;
320
329
try {
321
- sketch = await this . loadSketch ( uri ) ;
330
+ sketch = await this . doLoadSketch ( uri , false ) ;
322
331
this . logger . debug (
323
332
`Loaded sketch ${ JSON . stringify (
324
333
sketch
@@ -391,7 +400,7 @@ export class SketchesServiceImpl
391
400
) ) {
392
401
let sketch : SketchWithDetails | undefined = undefined ;
393
402
try {
394
- sketch = await this . loadSketch ( uri ) ;
403
+ sketch = await this . doLoadSketch ( uri , false ) ;
395
404
} catch { }
396
405
if ( ! sketch ) {
397
406
needsUpdate = true ;
@@ -414,14 +423,14 @@ export class SketchesServiceImpl
414
423
415
424
async cloneExample ( uri : string ) : Promise < Sketch > {
416
425
const [ sketch , parentPath ] = await Promise . all ( [
417
- this . loadSketch ( uri ) ,
426
+ this . doLoadSketch ( uri , false ) ,
418
427
this . createTempFolder ( ) ,
419
428
] ) ;
420
429
const destinationUri = FileUri . create (
421
430
path . join ( parentPath , sketch . name )
422
431
) . toString ( ) ;
423
432
const copiedSketchUri = await this . copy ( sketch , { destinationUri } ) ;
424
- return this . loadSketch ( copiedSketchUri ) ;
433
+ return this . doLoadSketch ( copiedSketchUri , false ) ;
425
434
}
426
435
427
436
async createNewSketch ( ) : Promise < Sketch > {
@@ -478,7 +487,7 @@ export class SketchesServiceImpl
478
487
fs . mkdir ( sketchDir , { recursive : true } ) ,
479
488
] ) ;
480
489
await fs . writeFile ( sketchFile , inoContent , { encoding : 'utf8' } ) ;
481
- return this . loadSketch ( FileUri . create ( sketchDir ) . toString ( ) ) ;
490
+ return this . doLoadSketch ( FileUri . create ( sketchDir ) . toString ( ) , false ) ;
482
491
}
483
492
484
493
/**
@@ -529,7 +538,7 @@ export class SketchesServiceImpl
529
538
uri : string
530
539
) : Promise < SketchWithDetails | undefined > {
531
540
try {
532
- const sketch = await this . loadSketch ( uri ) ;
541
+ const sketch = await this . doLoadSketch ( uri , false ) ;
533
542
return sketch ;
534
543
} catch ( err ) {
535
544
if ( SketchesError . NotFound . is ( err ) || SketchesError . InvalidName . is ( err ) ) {
@@ -554,7 +563,7 @@ export class SketchesServiceImpl
554
563
}
555
564
// Nothing to do when source and destination are the same.
556
565
if ( sketch . uri === destinationUri ) {
557
- await this . loadSketch ( sketch . uri ) ; // Sanity check.
566
+ await this . doLoadSketch ( sketch . uri , false ) ; // Sanity check.
558
567
return sketch . uri ;
559
568
}
560
569
@@ -575,7 +584,10 @@ export class SketchesServiceImpl
575
584
if ( oldPath !== newPath ) {
576
585
await fs . rename ( oldPath , newPath ) ;
577
586
}
578
- await this . loadSketch ( FileUri . create ( destinationPath ) . toString ( ) ) ; // Sanity check.
587
+ await this . doLoadSketch (
588
+ FileUri . create ( destinationPath ) . toString ( ) ,
589
+ false
590
+ ) ; // Sanity check.
579
591
resolve ( ) ;
580
592
} catch ( e ) {
581
593
reject ( e ) ;
@@ -597,7 +609,7 @@ export class SketchesServiceImpl
597
609
}
598
610
599
611
async archive ( sketch : Sketch , destinationUri : string ) : Promise < string > {
600
- await this . loadSketch ( sketch . uri ) ; // sanity check
612
+ await this . doLoadSketch ( sketch . uri , false ) ; // sanity check
601
613
const { client } = await this . coreClient ;
602
614
const archivePath = FileUri . fsPath ( destinationUri ) ;
603
615
// The CLI cannot override existing archives, so we have to wipe it manually: https://github.com/arduino/arduino-cli/issues/1160
0 commit comments