Skip to content

Commit 43e46aa

Browse files
committed
feat: add sketch generate and open example
Signed-off-by: dankeboy36 <[email protected]>
1 parent 9187b77 commit 43e46aa

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

src/extension.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as vscode from 'vscode';
2020
import type { Terminal, Pseudoterminal, ExtensionTerminalOptions } from 'vscode';
2121
import type { ArduinoContext, BoardDetails } from 'vscode-arduino-api';
2222
import * as path from 'path';
23+
import { promises as fs } from 'node:fs';
2324
import { spawn } from 'child_process';
2425

2526

@@ -39,9 +40,53 @@ export function activate(context: vscode.ExtensionContext) {
3940
})
4041
);
4142
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+
}
4348
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;
4590
})
4691
);
4792
context.subscriptions.push(

0 commit comments

Comments
 (0)