@@ -32,6 +32,7 @@ import {
32
32
maybeNormalizeDrive ,
33
33
TempSketchPrefix ,
34
34
} from './is-temp-sketch' ;
35
+ import { join } from 'path' ;
35
36
36
37
const RecentSketches = 'recent-sketches.json' ;
37
38
@@ -47,6 +48,7 @@ export class SketchesServiceImpl
47
48
autoStart : true ,
48
49
concurrency : 1 ,
49
50
} ) ;
51
+ private bluePrintContent : string ;
50
52
51
53
@inject ( ILogger )
52
54
@named ( 'sketches-service' )
@@ -447,10 +449,11 @@ export class SketchesServiceImpl
447
449
const sketchDir = path . join ( parentPath , sketchName ) ;
448
450
const sketchFile = path . join ( sketchDir , `${ sketchName } .ino` ) ;
449
451
await fs . mkdir ( sketchDir , { recursive : true } ) ;
450
- let defaultContent = await this . loadDefault ( ) ;
452
+ this . bluePrintContent = this . bluePrintContent || await this . loadDefault ( ) ;
453
+
451
454
await fs . writeFile (
452
455
sketchFile ,
453
- defaultContent ,
456
+ this . bluePrintContent ,
454
457
{ encoding : 'utf8' }
455
458
) ;
456
459
return this . loadSketch ( FileUri . create ( sketchDir ) . toString ( ) ) ;
@@ -630,9 +633,21 @@ export class SketchesServiceImpl
630
633
}
631
634
}
632
635
633
- // Returns the default.ino from the default folder
636
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
637
+ private tryParse ( raw : string ) : any | undefined {
638
+ try {
639
+ return JSON . parse ( raw ) ;
640
+ } catch {
641
+ return undefined ;
642
+ }
643
+ }
644
+
645
+ // Returns the default.ino from the settings or from default folder.
634
646
private async loadDefault ( ) : Promise < string > {
635
647
const root = await this . root ( await this . sketchbookUri ( ) ) ;
648
+ const configDirUri = await this . envVariableServer . getConfigDirUri ( ) ;
649
+ const configDirPath = FileUri . fsPath ( configDirUri ) ;
650
+
636
651
let result : string ;
637
652
result = `void setup() {
638
653
// put your setup code here, to run once:
@@ -644,23 +659,30 @@ void loop() {
644
659
645
660
}` ;
646
661
647
- let filename = `${ root } /default/default.ino` ;
648
- let exists = false ;
649
- let raw = "" ;
662
+ try {
663
+ const raw = await fs . readFile ( join ( configDirPath , 'settings.json' ) , {
664
+ encoding : 'utf8' ,
665
+ } ) ;
666
+ const json = this . tryParse ( raw ) ;
667
+ if ( json ) {
668
+ const value = json [ 'arduino.sketch.inoBlueprint' ] ;
650
669
651
- try {
652
- raw = await fs . readFile ( filename , { encoding : 'utf8' , } ) ;
653
- exists = true ;
654
- } catch {
655
- exists = false ;
656
- }
670
+ let filename = ( typeof value === 'string' && ! ! value ) ? value : `${ root } /default/default.ino` ;
657
671
658
- if ( exists ) {
672
+ let raw = '' ;
673
+
674
+ raw = await fs . readFile ( filename , { encoding : 'utf8' , } ) ;
659
675
result = raw ;
660
- this . logger . info ( `-- Default found at ${ filename } ` )
661
- } else {
662
- this . logger . info ( `-- Default sketch not found at ${ filename } ` )
676
+
677
+ this . logger . info ( `-- Default found at ${ filename } ` )
678
+ }
679
+ } catch ( error ) {
680
+ if ( 'code' in error && error . code === 'ENOENT' ) {
681
+ return result ;
682
+ }
683
+ throw error ;
663
684
}
685
+
664
686
return result ;
665
687
}
666
688
}
0 commit comments