Minor refactor, print STDERR in red

This commit is contained in:
Earle F. Philhower, III
2023-09-29 17:29:18 -07:00
parent ac92013a12
commit 5726dab863
3 changed files with 208 additions and 206 deletions

View File

@ -7,7 +7,7 @@ LittleFS uploader compatible with Arduino IDE 2.2.1 or higher. For use with the
## Usage ## Usage
`[Ctrl]+[Shift]+[P]`, then "`Upload LittleFS to Pico/ESP8266`" `[Ctrl]` + `[Shift]` + `[P]`, then "`Upload LittleFS to Pico/ESP8266`"
## Glitches ## Glitches

Binary file not shown.

View File

@ -21,7 +21,7 @@ function makeTerminal(title : string) {
close: () => { writerReady = false; }, close: () => { writerReady = false; },
handleInput: () => {} handleInput: () => {}
}; };
let terminal = (<any>vscode.window).createTerminal({name: title, pty}); const terminal = (<any>vscode.window).createTerminal({name: title, pty});
terminal.show(); terminal.show();
} }
@ -39,6 +39,23 @@ function findTool(ctx: ArduinoContext, match : string) : string | undefined {
return ret; return ret;
} }
// Execute a command and display it's output in the terminal
async function runCommand(exe : string, opts : any[]) {
const cmd = spawn(exe, opts);
for await (const chunk of cmd.stdout) {
writeEmitter.fire(String(chunk).replace(/\n/g, "\r\n"));
}
for await (const chunk of cmd.stderr) {
// Write stderr in red
writeEmitter.fire("\x1b[31m" + String(chunk).replace(/\n/g, "\r\n") + "\x1b[0m");
}
// Wait until the executable finishes
let exitCode = await new Promise( (resolve, reject) => {
cmd.on('close', resolve);
});
return exitCode;
}
export function activate(context: vscode.ExtensionContext) { export function activate(context: vscode.ExtensionContext) {
// Get the Arduino info extension loaded // Get the Arduino info extension loaded
const arduinoContext: ArduinoContext = vscode.extensions.getExtension('dankeboy36.vscode-arduino-api')?.exports; const arduinoContext: ArduinoContext = vscode.extensions.getExtension('dankeboy36.vscode-arduino-api')?.exports;
@ -60,8 +77,14 @@ export function activate(context: vscode.ExtensionContext) {
} }
makeTerminal("LittleFS Upload"); makeTerminal("LittleFS Upload");
// Wait for the terminal to become active. // Wait for the terminal to become active.
let cnt = 0;
while (!writerReady) { while (!writerReady) {
if (cnt++ >= 50) { // Give it 5 seconds and then give up
vscode.window.showErrorMessage("Unable to open upload terminal");
return;
}
await new Promise( resolve => setTimeout(resolve, 100) ); await new Promise( resolve => setTimeout(resolve, 100) );
} }
@ -140,7 +163,7 @@ export function activate(context: vscode.ExtensionContext) {
let tool = undefined; let tool = undefined;
if (pico) { if (pico) {
tool = findTool(arduinoContext, "runtime.tools.pqt-mklittlefs"); tool = findTool(arduinoContext, "runtime.tools.pqt-mklittlefs");
} else { // ESP826 } else { // ESP8266
tool = findTool(arduinoContext, "runtime.tools.mklittlefs"); tool = findTool(arduinoContext, "runtime.tools.mklittlefs");
} }
if (tool) { if (tool) {
@ -182,18 +205,7 @@ export function activate(context: vscode.ExtensionContext) {
writeEmitter.fire("Building LittleFS filesystem\r\n"); writeEmitter.fire("Building LittleFS filesystem\r\n");
writeEmitter.fire(mklittlefs + " " + buildOpts.join(" ") + "\r\n"); writeEmitter.fire(mklittlefs + " " + buildOpts.join(" ") + "\r\n");
const mkfs = spawn(mklittlefs, buildOpts); let exitCode = await runCommand(mklittlefs, buildOpts);
for await (const chunk of mkfs.stdout) {
writeEmitter.fire(String(chunk).replace(/\n/g, "\r\n"));
}
for await (const chunk of mkfs.stderr) {
writeEmitter.fire(String(chunk).replace(/\n/g, "\r\n"));
}
// Wait until the executable finishes
let exitCode = await new Promise( (resolve, reject) => {
mkfs.on('close', resolve);
});
if (exitCode) { if (exitCode) {
writeEmitter.fire("ERROR: Mklittlefs failed, error code: " + String(exitCode) + "\r\n\r\n"); writeEmitter.fire("ERROR: Mklittlefs failed, error code: " + String(exitCode) + "\r\n\r\n");
return; return;
@ -219,18 +231,8 @@ export function activate(context: vscode.ExtensionContext) {
writeEmitter.fire("\r\n\r\nUploading LittleFS filesystem\r\n"); writeEmitter.fire("\r\n\r\nUploading LittleFS filesystem\r\n");
writeEmitter.fire(python3 + " " + uploadOpts.join(" ") + "\r\n"); writeEmitter.fire(python3 + " " + uploadOpts.join(" ") + "\r\n");
const upld = spawn(python3, uploadOpts);
for await (const chunk of upld.stdout) {
writeEmitter.fire(String(chunk).replace(/\n/g, "\r\n"));
}
for await (const chunk of upld.stderr) {
writeEmitter.fire(String(chunk).replace(/\n/g, "\r\n"));
}
// Wait until the executable finishes
exitCode = await new Promise( (resolve, reject) => {
upld.on('close', resolve);
});
exitCode = await runCommand(python3, uploadOpts);
if (exitCode) { if (exitCode) {
writeEmitter.fire("ERROR: Upload failed, error code: " + String(exitCode) + "\r\n\r\n"); writeEmitter.fire("ERROR: Upload failed, error code: " + String(exitCode) + "\r\n\r\n");
return; return;