mirror of
https://github.com/arduino/arduino-ide.git
synced 2025-07-23 19:26:35 +00:00
Reuse spawnCommand
util for more robust command execution
This commit is contained in:
parent
fb50244a29
commit
0f35821d14
@ -24,9 +24,9 @@
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
import { spawnSync } from 'child_process';
|
||||
import { EOL } from 'os';
|
||||
import { dirname, normalize, basename } from 'path';
|
||||
import { normalize, basename } from 'path';
|
||||
import { spawnCommand } from 'arduino-ide-extension/lib/node/exec-util';
|
||||
|
||||
export enum SymbolType {
|
||||
Function,
|
||||
@ -133,28 +133,9 @@ export class SymbolTable {
|
||||
}
|
||||
|
||||
private execute(): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!this.objdump) {
|
||||
return reject(new Error('Missing parameter: objdump'));
|
||||
}
|
||||
try {
|
||||
const { stdout, stderr } = spawnSync(this.objdump, [
|
||||
'--syms',
|
||||
this.program
|
||||
], {
|
||||
cwd: dirname(this.objdump),
|
||||
windowsHide: true
|
||||
});
|
||||
|
||||
const error = stderr.toString('utf8');
|
||||
if (error) {
|
||||
return reject(new Error(error));
|
||||
}
|
||||
|
||||
resolve(stdout.toString('utf8'));
|
||||
} catch (error) {
|
||||
return reject(new Error(error));
|
||||
}
|
||||
});
|
||||
if (!this.objdump) {
|
||||
return Promise.reject(new Error('Missing parameter: objdump'));
|
||||
}
|
||||
return spawnCommand(this.objdump, ['--syms', this.program]);
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ export async function getExecPath(commandName: string, logger: ILogger, versionA
|
||||
return buildCommand;
|
||||
}
|
||||
|
||||
export function spawnCommand(command: string, args: string[], logger: ILogger): Promise<string> {
|
||||
export function spawnCommand(command: string, args: string[], logger?: ILogger): Promise<string> {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
const cp = spawn(command, args, { windowsHide: true, shell: true });
|
||||
const outBuffers: Buffer[] = [];
|
||||
@ -38,7 +38,9 @@ export function spawnCommand(command: string, args: string[], logger: ILogger):
|
||||
cp.stdout.on('data', (b: Buffer) => outBuffers.push(b));
|
||||
cp.stderr.on('data', (b: Buffer) => errBuffers.push(b));
|
||||
cp.on('error', error => {
|
||||
logger.error(`Error executing ${command} ${args.join(' ')}`, error);
|
||||
if (logger) {
|
||||
logger.error(`Error executing ${command} ${args.join(' ')}`, error);
|
||||
}
|
||||
reject(error);
|
||||
});
|
||||
cp.on('exit', (code, signal) => {
|
||||
@ -49,17 +51,23 @@ export function spawnCommand(command: string, args: string[], logger: ILogger):
|
||||
}
|
||||
if (errBuffers.length > 0) {
|
||||
const message = Buffer.concat(errBuffers).toString('utf8').trim();
|
||||
logger.error(`Error executing ${command} ${args.join(' ')}: ${message}`);
|
||||
if (logger) {
|
||||
logger.error(`Error executing ${command} ${args.join(' ')}: ${message}`);
|
||||
}
|
||||
reject(new Error(`Process failed with error: ${message}`));
|
||||
return;
|
||||
}
|
||||
if (signal) {
|
||||
logger.error(`Unexpected signal '${signal}' when executing ${command} ${args.join(' ')}`);
|
||||
if (logger) {
|
||||
logger.error(`Unexpected signal '${signal}' when executing ${command} ${args.join(' ')}`);
|
||||
}
|
||||
reject(new Error(`Process exited with signal: ${signal}`));
|
||||
return;
|
||||
}
|
||||
if (code) {
|
||||
logger.error(`Unexpected exit code '${code}' when executing ${command} ${args.join(' ')}`);
|
||||
if (logger) {
|
||||
logger.error(`Unexpected exit code '${code}' when executing ${command} ${args.join(' ')}`);
|
||||
}
|
||||
reject(new Error(`Process exited with exit code: ${code}`));
|
||||
return;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user