@@ -20,6 +20,7 @@ import * as vscode from 'vscode';
20
20
import type { Terminal , Pseudoterminal , ExtensionTerminalOptions } from 'vscode' ;
21
21
import type { ArduinoContext , BoardDetails } from 'vscode-arduino-api' ;
22
22
import * as path from 'path' ;
23
+ import { promises as fs } from 'node:fs' ;
23
24
import { spawn } from 'child_process' ;
24
25
25
26
@@ -39,9 +40,53 @@ export function activate(context: vscode.ExtensionContext) {
39
40
} )
40
41
) ;
41
42
context . subscriptions . push (
42
- vscode . commands . registerCommand ( 'teensysecurity.step1' , ( ) => {
43
+ vscode . commands . registerCommand ( 'teensysecurity.step1' , async ( ) => {
44
+ const { userDirPath } = acontext ;
45
+ if ( ! userDirPath ) {
46
+ return ;
47
+ }
43
48
console . log ( 'teensysecurity.step1 (Fuse Write Sketch) callback' ) ;
44
- // TODO...
49
+ // I used the sketchbook path here, but it can be relative to current sketch, or in the temp folder.
50
+ // It's just an example.
51
+ const newBasename = await vscode . window . showInputBox ( {
52
+ placeHolder : 'New Fuse sketch folder name' ,
53
+ async validateInput ( value ) {
54
+ try {
55
+ await fs . readdir ( path . join ( userDirPath , value ) ) ;
56
+ // when reading the directory is OK, the sketch folder already exists
57
+ return 'The sketch already exists' ;
58
+ } catch ( err ) {
59
+ if ( err instanceof Error && 'code' in err && err . code === 'ENOENT' ) {
60
+ // Ignore all ENOENT errors (https://nodejs.org/api/errors.html)
61
+ } else {
62
+ throw err ;
63
+ }
64
+ // all errors considered
65
+ }
66
+ // TODO: validate the sketch folder name (https://arduino.github.io/arduino-cli/1.0/sketch-specification/#sketch-folders-and-files)
67
+ if ( value === 'alma' ) {
68
+ return 'alma is reserved' ; // return any string to show as an error
69
+ }
70
+ return undefined ; // no error otherwise
71
+ } ,
72
+ } ) ;
73
+ if ( ! newBasename ) {
74
+ return ;
75
+ }
76
+ const newSketchFolderPath = path . join ( userDirPath , newBasename ) ;
77
+ await fs . mkdir ( newSketchFolderPath , { recursive : true } ) ;
78
+ await fs . writeFile ( path . join ( newSketchFolderPath , `${ newBasename } .ino` ) , `
79
+ // Generated with teensysecurity (${ new Date ( ) . toISOString ( ) } )
80
+
81
+ void setup() {}
82
+ void loop() {}
83
+ ` , { encoding : 'utf8' } ) ;
84
+ // VS Code APIs use URI instead of raw file-system path strings
85
+ const newSketchFolderUri = vscode . Uri . file ( newSketchFolderPath ) ;
86
+ // See all built-in VS Code commands: https://code.visualstudio.com/api/references/commands
87
+ // Theia supports a few of them https://github.com/eclipse-theia/theia/blob/363865fd7ecea164eb134f491e95a4005ba6df0d/packages/plugin-ext-vscode/src/browser/plugin-vscode-commands-contribution.ts#L292-L293
88
+ vscode . commands . executeCommand ( 'vscode.openFolder' , newSketchFolderUri , { forceNewWindow : true } ) ;
89
+ return undefined ;
45
90
} )
46
91
) ;
47
92
context . subscriptions . push (
0 commit comments