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

Commit 27c65e7

Browse files
Reworked util.spawn to mergable version
1 parent ced9e56 commit 27c65e7

File tree

2 files changed

+45
-54
lines changed

2 files changed

+45
-54
lines changed

src/arduino/arduino.ts

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ Please make sure the folder is not occupied by other procedures .`);
244244
}
245245
try {
246246
await util.spawn(this._settings.commandPath,
247-
showOutput ? arduinoChannel.channel : null,
248-
["--install-boards", `${packageName}${arch && ":" + arch}${version && ":" + version}`]);
249-
247+
["--install-boards", `${packageName}${arch && ":" + arch}${version && ":" + version}`],
248+
undefined,
249+
{ channel: showOutput ? arduinoChannel.channel : null });
250250
if (updatingIndex) {
251251
arduinoChannel.end("Updated package index files.");
252252
} else {
@@ -282,9 +282,9 @@ Please make sure the folder is not occupied by other procedures .`);
282282
}
283283
try {
284284
await util.spawn(this._settings.commandPath,
285-
showOutput ? arduinoChannel.channel : null,
286-
["--install-library", `${libName}${version && ":" + version}`]);
287-
285+
["--install-library", `${libName}${version && ":" + version}`],
286+
undefined,
287+
{ channel: showOutput ? arduinoChannel.channel : undefined });
288288
if (updatingIndex) {
289289
arduinoChannel.end("Updated library index files.");
290290
} else {
@@ -418,9 +418,9 @@ Please make sure the folder is not occupied by other procedures .`);
418418
const cmd = args.shift();
419419
try {
420420
await util.spawn(cmd,
421-
arduinoChannel.channel,
422421
args,
423-
{ shell: true, cwd: ArduinoWorkspace.rootPath });
422+
{ shell: true, cwd: ArduinoWorkspace.rootPath },
423+
{ channel: arduinoChannel.channel });
424424
} catch (ex) {
425425
arduinoChannel.error(`Running pre-build command failed: ${os.EOL}${ex.error}`);
426426
return false;
@@ -548,8 +548,6 @@ Please make sure the folder is not occupied by other procedures .`);
548548
// Push sketch as last argument
549549
args.push(path.join(ArduinoWorkspace.rootPath, dc.sketch));
550550

551-
let success = false;
552-
553551
const cleanup = async () => {
554552
if (cocopa) {
555553
await cocopa.conclude();
@@ -562,25 +560,14 @@ Please make sure the folder is not occupied by other procedures .`);
562560
}
563561
}
564562

565-
// TODO: Get rid of spawn's channel parameter and just support
566-
// stdout and stderr callbacks
567-
const stdoutCallback = (line: string) => {
568-
if (cocopa) {
569-
cocopa.callback(line);
570-
if (verbose) {
571-
arduinoChannel.channel.append(line);
572-
}
573-
} else {
574-
arduinoChannel.channel.append(line);
575-
}
576-
}
577-
578-
await util.spawn(
563+
return await util.spawn(
579564
this._settings.commandPath,
580-
arduinoChannel.channel,
581565
args,
582566
undefined,
583-
stdoutCallback,
567+
{
568+
channel: !cocopa || cocopa && verbose ? arduinoChannel.channel : undefined,
569+
stdout: cocopa ? cocopa.callback : undefined,
570+
}
584571
).then(async () => {
585572
await cleanup();
586573
if (mode !== BuildMode.Analyze) {
@@ -590,17 +577,14 @@ Please make sure the folder is not occupied by other procedures .`);
590577
arduinoChannel.info(`To rebuild your IntelliSense configuration run "${cmd}"`);
591578
}
592579
arduinoChannel.end(`${mode} sketch '${dc.sketch}${os.EOL}`);
593-
success = true;
580+
return true;
594581
}, async (reason) => {
595582
await cleanup();
596583
const msg = reason.code
597584
? `Exit with code=${reason.code}`
598-
: reason.message
599-
? reason.message
600-
: JSON.stringify(reason);
585+
: JSON.stringify(reason);
601586
arduinoChannel.error(`${mode} sketch '${dc.sketch}': ${msg}${os.EOL}`);
587+
return false;
602588
});
603-
604-
return success;
605589
}
606590
}

src/common/util.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,13 @@ 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
export function spawn(command: string,
191-
outputChannel: vscode.OutputChannel,
192189
args: string[] = [],
193190
options: any = {},
194-
stdoutCallback?: (string) => void): Thenable<object> {
191+
output?: {channel?: vscode.OutputChannel,
192+
stdout?: (string) => void,
193+
stderr?: (string) => void}): Thenable<object> {
195194
return new Promise((resolve, reject) => {
196-
const stdout = "";
197-
const stderr = "";
198195
options.cwd = options.cwd || path.resolve(path.join(__dirname, ".."));
199196
const child = childProcess.spawn(command, args, options);
200197

@@ -210,27 +207,37 @@ export function spawn(command: string,
210207
}
211208
}
212209

213-
if (outputChannel) {
214-
child.stdout.on("data", (data: Buffer) => {
215-
const decoded = decodeData(data, codepage);
216-
if (stdoutCallback) {
217-
stdoutCallback(decoded);
218-
} else {
219-
outputChannel.append(decoded);
220-
}
221-
});
222-
child.stderr.on("data", (data: Buffer) => {
223-
outputChannel.append(decodeData(data, codepage));
224-
});
210+
if (output) {
211+
if (output.channel || output.stdout) {
212+
child.stdout.on("data", (data: Buffer) => {
213+
const decoded = decodeData(data, codepage);
214+
if (output.stdout) {
215+
output.stdout(decoded);
216+
}
217+
if (output.channel) {
218+
output.channel.append(decoded);
219+
}
220+
});
221+
}
222+
if (output.channel || output.stderr) {
223+
child.stderr.on("data", (data: Buffer) => {
224+
const decoded = decodeData(data, codepage);
225+
if (output.stderr) {
226+
output.stderr(decoded);
227+
}
228+
if (output.channel) {
229+
output.channel.append(decoded);
230+
}
231+
});
232+
}
225233
}
226234

227-
child.on("error", (error) => reject({ error, stderr, stdout }));
228-
235+
child.on("error", (error) => reject({ error }));
229236
child.on("exit", (code) => {
230237
if (code === 0) {
231-
resolve({ code, stdout, stderr });
238+
resolve({ code });
232239
} else {
233-
reject({ code, stdout, stderr });
240+
reject({ code });
234241
}
235242
});
236243
});

0 commit comments

Comments
 (0)