Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 4202508

Browse files
elektronikworkshopadiazulay
authored andcommitted
Reworked util.spawn to mergable version
1 parent d408686 commit 4202508

File tree

2 files changed

+59
-62
lines changed

2 files changed

+59
-62
lines changed

src/arduino/arduino.ts

+30-40
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,6 @@ export class ArduinoApp {
361361
UsbDetector.getInstance().pauseListening();
362362
}
363363

364-
let success = false;
365-
366364
// Push sketch as last argument
367365
args.push(path.join(ArduinoWorkspace.rootPath, dc.sketch));
368366

@@ -378,25 +376,14 @@ export class ArduinoApp {
378376
}
379377
}
380378

381-
// TODO: Get rid of spawn's channel parameter and just support
382-
// stdout and stderr callbacks
383-
const stdoutCallback = (line: string) => {
384-
if (cocopa) {
385-
cocopa.callback(line);
386-
if (verbose) {
387-
arduinoChannel.channel.append(line);
388-
}
389-
} else {
390-
arduinoChannel.channel.append(line);
391-
}
392-
}
393-
394-
await util.spawn(
379+
return await util.spawn(
395380
this._settings.commandPath,
396-
arduinoChannel.channel,
397381
args,
398382
undefined,
399-
stdoutCallback,
383+
{
384+
channel: !cocopa || cocopa && verbose ? arduinoChannel.channel : undefined,
385+
stdout: cocopa ? cocopa.callback : undefined,
386+
},
400387
).then(async () => {
401388
await cleanup();
402389
if (buildMode !== BuildMode.Analyze) {
@@ -406,18 +393,15 @@ export class ArduinoApp {
406393
arduinoChannel.info(`To rebuild your IntelliSense configuration run "${cmd}"`);
407394
}
408395
arduinoChannel.end(`${buildMode} sketch '${dc.sketch}'${os.EOL}`);
409-
success = true;
396+
return true;
410397
}, async (reason) => {
411398
await cleanup();
412399
const msg = reason.code
413400
? `Exit with code=${reason.code}`
414-
: reason.message
415-
? reason.message
416-
: JSON.stringify(reason);
401+
: JSON.stringify(reason);
417402
arduinoChannel.error(`${buildMode} sketch '${dc.sketch}': ${msg}${os.EOL}`);
403+
return false;
418404
});
419-
420-
return success;
421405
}
422406

423407
// Include the *.h header files from selected library to the arduino sketch.
@@ -481,14 +465,17 @@ export class ArduinoApp {
481465
}
482466
arduinoChannel.info(`${packageName}${arch && ":" + arch}${version && ":" + version}`);
483467
try {
484-
this.useArduinoCli() ?
468+
if (this.useArduinoCli()) {
485469
await util.spawn(this._settings.commandPath,
486-
showOutput ? arduinoChannel.channel : null,
487-
["core", "install", `${packageName}${arch && ":" + arch}${version && "@" + version}`]) :
470+
["core", "install", `${packageName}${arch && ":" + arch}${version && "@" + version}`],
471+
undefined,
472+
{ channel: showOutput ? arduinoChannel.channel : null });
473+
} else {
488474
await util.spawn(this._settings.commandPath,
489-
showOutput ? arduinoChannel.channel : null,
490-
["--install-boards", `${packageName}${arch && ":" + arch}${version && ":" + version}`]);
491-
475+
["--install-boards", `${packageName}${arch && ":" + arch}${version && ":" + version}`],
476+
undefined,
477+
{ channel: showOutput ? arduinoChannel.channel : null });
478+
}
492479
if (updatingIndex) {
493480
arduinoChannel.end("Updated package index files.");
494481
} else {
@@ -530,14 +517,17 @@ export class ArduinoApp {
530517
arduinoChannel.start(`Install library - ${libName}`);
531518
}
532519
try {
533-
this.useArduinoCli() ?
534-
await util.spawn(this._settings.commandPath,
535-
showOutput ? arduinoChannel.channel : null,
536-
["lib", "install", `${libName}${version && "@" + version}`]) :
537-
await util.spawn(this._settings.commandPath,
538-
showOutput ? arduinoChannel.channel : null,
539-
["--install-library", `${libName}${version && ":" + version}`]);
540-
520+
if (this.useArduinoCli()) {
521+
await util.spawn(this._settings.commandPath,
522+
["lib", "install", `${libName}${version && "@" + version}`],
523+
undefined,
524+
{ channel: showOutput ? arduinoChannel.channel : undefined });
525+
} else {
526+
await util.spawn(this._settings.commandPath,
527+
["--install-library", `${libName}${version && ":" + version}`],
528+
undefined,
529+
{ channel: showOutput ? arduinoChannel.channel : undefined });
530+
}
541531
if (updatingIndex) {
542532
arduinoChannel.end("Updated library index files.");
543533
} else {
@@ -671,9 +661,9 @@ export class ArduinoApp {
671661
const cmd = args.shift();
672662
try {
673663
await util.spawn(cmd,
674-
arduinoChannel.channel,
675664
args,
676-
{ shell: true, cwd: ArduinoWorkspace.rootPath });
665+
{ shell: true, cwd: ArduinoWorkspace.rootPath },
666+
{ channel: arduinoChannel.channel });
677667
} catch (ex) {
678668
arduinoChannel.error(`Running pre-build command failed: ${os.EOL}${ex.error}`);
679669
return false;

src/common/util.ts

+29-22
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ export function isArduinoFile(filePath): boolean {
185185
return fileExistsSync(filePath) && (path.extname(filePath) === ".ino" || path.extname(filePath) === ".pde");
186186
}
187187

188-
// TODO: remove output channel and just provide callback hooks for stdout and
189-
// stderr
190188
/**
191189
* Send a command to arduino
192190
* @param {string} command - base command path (either Arduino IDE or CLI)
@@ -197,14 +195,13 @@ export function isArduinoFile(filePath): boolean {
197195
*/
198196
export function spawn(
199197
command: string,
200-
outputChannel: vscode.OutputChannel,
201198
args: string[] = [],
202199
options: any = {},
203-
stdoutCallback?: (s: string) => void,
200+
output?: {channel?: vscode.OutputChannel,
201+
stdout?: (s: string) => void,
202+
stderr?: (s: string) => void},
204203
): Thenable<object> {
205204
return new Promise((resolve, reject) => {
206-
const stdout = "";
207-
const stderr = "";
208205
options.cwd = options.cwd || path.resolve(path.join(__dirname, ".."));
209206
const child = childProcess.spawn(command, args, options);
210207

@@ -220,27 +217,37 @@ export function spawn(
220217
}
221218
}
222219

223-
if (outputChannel) {
224-
child.stdout.on("data", (data: Buffer) => {
225-
const decoded = decodeData(data, codepage);
226-
if (stdoutCallback) {
227-
stdoutCallback(decoded);
228-
} else {
229-
outputChannel.append(decoded);
230-
}
231-
});
232-
child.stderr.on("data", (data: Buffer) => {
233-
outputChannel.append(decodeData(data, codepage));
234-
});
220+
if (output) {
221+
if (output.channel || output.stdout) {
222+
child.stdout.on("data", (data: Buffer) => {
223+
const decoded = decodeData(data, codepage);
224+
if (output.stdout) {
225+
output.stdout(decoded);
226+
}
227+
if (output.channel) {
228+
output.channel.append(decoded);
229+
}
230+
});
231+
}
232+
if (output.channel || output.stderr) {
233+
child.stderr.on("data", (data: Buffer) => {
234+
const decoded = decodeData(data, codepage);
235+
if (output.stderr) {
236+
output.stderr(decoded);
237+
}
238+
if (output.channel) {
239+
output.channel.append(decoded);
240+
}
241+
});
242+
}
235243
}
236244

237-
child.on("error", (error) => reject({ error, stderr, stdout }));
238-
245+
child.on("error", (error) => reject({ error }));
239246
child.on("exit", (code) => {
240247
if (code === 0) {
241-
resolve({ code, stdout, stderr });
248+
resolve({ code });
242249
} else {
243-
reject({ code, stdout, stderr });
250+
reject({ code });
244251
}
245252
});
246253
});

0 commit comments

Comments
 (0)