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