mirror of
https://gitcode.com/gh_mirrors/ar/arduino-littlefs-upload.git
synced 2025-08-06 18:24:30 +08:00
Use async processing, displays immediate feedback
Still seems not to be able to recreate the window when closed once.
This commit is contained in:
Binary file not shown.
@ -2,16 +2,10 @@ import * as vscode from 'vscode';
|
|||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import type { ArduinoContext } from 'vscode-arduino-api';
|
import type { ArduinoContext } from 'vscode-arduino-api';
|
||||||
import { platform } from 'node:os';
|
import { platform } from 'node:os';
|
||||||
import { spawn, spawnSync } from 'child_process';
|
import { spawn } from 'child_process';
|
||||||
|
|
||||||
|
|
||||||
import { exec as cpExec } from "child_process";
|
|
||||||
import { promisify } from "util";
|
|
||||||
|
|
||||||
// promisified Node executable (Node 10+)
|
|
||||||
const exec = promisify(cpExec);
|
|
||||||
|
|
||||||
const writeEmitter = new vscode.EventEmitter<string>();
|
const writeEmitter = new vscode.EventEmitter<string>();
|
||||||
|
let writerReady : boolean = false;
|
||||||
|
|
||||||
function makeTerminal(title : string) {
|
function makeTerminal(title : string) {
|
||||||
// If it exists, move it to the front
|
// If it exists, move it to the front
|
||||||
@ -24,8 +18,8 @@ function makeTerminal(title : string) {
|
|||||||
// Not found, make a new terminal
|
// Not found, make a new terminal
|
||||||
const pty = {
|
const pty = {
|
||||||
onDidWrite: writeEmitter.event,
|
onDidWrite: writeEmitter.event,
|
||||||
open: () => {},
|
open: () => { writerReady = true; },
|
||||||
close: () => {},
|
close: () => { writerReady = false; },
|
||||||
handleInput: () => {}
|
handleInput: () => {}
|
||||||
};
|
};
|
||||||
let terminal = (<any>vscode.window).createTerminal({name: title, pty});
|
let terminal = (<any>vscode.window).createTerminal({name: title, pty});
|
||||||
@ -46,6 +40,8 @@ function findTool(ctx: ArduinoContext, match : string) : string | undefined {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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;
|
||||||
@ -58,7 +54,7 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
makeTerminal("LittleFS Upload");
|
makeTerminal("LittleFS Upload");
|
||||||
|
|
||||||
// Register the command
|
// Register the command
|
||||||
let disposable = vscode.commands.registerCommand('arduino-littlefs-upload.uploadLittleFS', () => {
|
const disposable = vscode.commands.registerCommand('arduino-littlefs-upload.uploadLittleFS', async () => {
|
||||||
|
|
||||||
//let str = JSON.stringify(arduinoContext, null, 4);
|
//let str = JSON.stringify(arduinoContext, null, 4);
|
||||||
//console.log(str);
|
//console.log(str);
|
||||||
@ -68,6 +64,11 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wait for the terminal to become active.
|
||||||
|
while (!writerReady) {
|
||||||
|
await new Promise( resolve => setTimeout(resolve, 100) );
|
||||||
|
}
|
||||||
|
|
||||||
// Clear the terminal
|
// Clear the terminal
|
||||||
writeEmitter.fire('\x1b[2J\x1b[3J\x1b[;H');
|
writeEmitter.fire('\x1b[2J\x1b[3J\x1b[;H');
|
||||||
|
|
||||||
@ -185,14 +186,20 @@ 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");
|
||||||
|
|
||||||
let mkfs = spawnSync(mklittlefs, buildOpts);
|
const mkfs = spawn(mklittlefs, buildOpts);
|
||||||
writeEmitter.fire(String(mkfs.stdout).replace(/\n/g, "\r\n"));
|
for await (const chunk of mkfs.stdout) {
|
||||||
if (mkfs.stderr) {
|
writeEmitter.fire(String(chunk).replace(/\n/g, "\r\n"));
|
||||||
writeEmitter.fire(String(mkfs.stderr).replace(/\n/g, "\r\n"));
|
|
||||||
}
|
}
|
||||||
writeEmitter.fire("\r\n\r\n");
|
for await (const chunk of mkfs.stderr) {
|
||||||
if (mkfs.status) {
|
writeEmitter.fire(String(chunk).replace(/\n/g, "\r\n"));
|
||||||
writeEmitter.fire("ERROR: Mklittlefs failed, error code: " + String(mkfs.status) + "\r\n\r\n");
|
}
|
||||||
|
// Wait until the executable finishes
|
||||||
|
let exitCode = await new Promise( (resolve, reject) => {
|
||||||
|
mkfs.on('close', resolve);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (exitCode) {
|
||||||
|
writeEmitter.fire("ERROR: Mklittlefs failed, error code: " + String(exitCode) + "\r\n\r\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,17 +221,22 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
uploadOpts = [upload, "--chip", "esp8266", "--port", serialPort, "--baud", String(uploadSpeed), "write_flash", String(fsStart), imageFile];
|
uploadOpts = [upload, "--chip", "esp8266", "--port", serialPort, "--baud", String(uploadSpeed), "write_flash", String(fsStart), imageFile];
|
||||||
}
|
}
|
||||||
|
|
||||||
writeEmitter.fire("Uploading 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");
|
||||||
let upld = spawnSync(python3, uploadOpts);
|
const upld = spawn(python3, uploadOpts);
|
||||||
|
for await (const chunk of upld.stdout) {
|
||||||
writeEmitter.fire(String(upld.stdout).replace(/\n/g, "\r\n"));
|
writeEmitter.fire(String(chunk).replace(/\n/g, "\r\n"));
|
||||||
if (upld.stderr) {
|
|
||||||
writeEmitter.fire(String(upld.stderr).replace(/\n/g, "\r\n"));
|
|
||||||
}
|
}
|
||||||
writeEmitter.fire("\r\n\r\n");
|
for await (const chunk of upld.stderr) {
|
||||||
if (upld.status) {
|
writeEmitter.fire(String(chunk).replace(/\n/g, "\r\n"));
|
||||||
writeEmitter.fire("ERROR: Upload failed, error code: " + String(upld.status) + "\r\n\r\n");
|
}
|
||||||
|
// Wait until the executable finishes
|
||||||
|
exitCode = await new Promise( (resolve, reject) => {
|
||||||
|
upld.on('close', resolve);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (exitCode) {
|
||||||
|
writeEmitter.fire("ERROR: Upload failed, error code: " + String(exitCode) + "\r\n\r\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user