mirror of
https://github.com/arduino/arduino-ide.git
synced 2025-11-15 05:09:29 +00:00
Support of the CLI config.
Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
import * as os from 'os';
|
||||
import * as which from 'which';
|
||||
import * as semver from 'semver';
|
||||
import { spawn } from 'child_process';
|
||||
import { join } from 'path';
|
||||
import { ILogger } from '@theia/core';
|
||||
import { spawn } from 'child_process';
|
||||
|
||||
export async function getExecPath(commandName: string, logger: ILogger, versionArg?: string, inBinDir?: boolean): Promise<string> {
|
||||
export async function getExecPath(commandName: string, onError: (error: Error) => void = (error) => console.log(error), versionArg?: string, inBinDir?: boolean): Promise<string> {
|
||||
const execName = `${commandName}${os.platform() === 'win32' ? '.exe' : ''}`;
|
||||
const relativePath = ['..', '..', 'build'];
|
||||
if (inBinDir) {
|
||||
@@ -16,13 +15,13 @@ export async function getExecPath(commandName: string, logger: ILogger, versionA
|
||||
return buildCommand;
|
||||
}
|
||||
const versionRegexp = /\d+\.\d+\.\d+/;
|
||||
const buildVersion = await spawnCommand(`"${buildCommand}"`, [versionArg], logger);
|
||||
const buildVersion = await spawnCommand(`"${buildCommand}"`, [versionArg], onError);
|
||||
const buildShortVersion = (buildVersion.match(versionRegexp) || [])[0];
|
||||
const pathCommand = await new Promise<string | undefined>(resolve => which(execName, (error, path) => resolve(error ? undefined : path)));
|
||||
if (!pathCommand) {
|
||||
return buildCommand;
|
||||
}
|
||||
const pathVersion = await spawnCommand(`"${pathCommand}"`, [versionArg], logger);
|
||||
const pathVersion = await spawnCommand(`"${pathCommand}"`, [versionArg], onError);
|
||||
const pathShortVersion = (pathVersion.match(versionRegexp) || [])[0];
|
||||
if (semver.gt(pathShortVersion, buildShortVersion)) {
|
||||
return pathCommand;
|
||||
@@ -30,7 +29,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[], onError: (error: Error) => void = (error) => console.log(error)): Promise<string> {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
const cp = spawn(command, args, { windowsHide: true, shell: true });
|
||||
const outBuffers: Buffer[] = [];
|
||||
@@ -38,9 +37,7 @@ 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 => {
|
||||
if (logger) {
|
||||
logger.error(`Error executing ${command} ${args.join(' ')}`, error);
|
||||
}
|
||||
onError(error);
|
||||
reject(error);
|
||||
});
|
||||
cp.on('exit', (code, signal) => {
|
||||
@@ -51,24 +48,21 @@ export function spawnCommand(command: string, args: string[], logger?: ILogger):
|
||||
}
|
||||
if (errBuffers.length > 0) {
|
||||
const message = Buffer.concat(errBuffers).toString('utf8').trim();
|
||||
if (logger) {
|
||||
logger.error(`Error executing ${command} ${args.join(' ')}: ${message}`);
|
||||
}
|
||||
reject(new Error(`Process failed with error: ${message}`));
|
||||
const error = new Error(`Error executing ${command} ${args.join(' ')}: ${message}`);
|
||||
onError(error)
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
if (signal) {
|
||||
if (logger) {
|
||||
logger.error(`Unexpected signal '${signal}' when executing ${command} ${args.join(' ')}`);
|
||||
}
|
||||
reject(new Error(`Process exited with signal: ${signal}`));
|
||||
const error = new Error(`Process exited with signal: ${signal}`);
|
||||
onError(error);
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
if (code) {
|
||||
if (logger) {
|
||||
logger.error(`Unexpected exit code '${code}' when executing ${command} ${args.join(' ')}`);
|
||||
}
|
||||
reject(new Error(`Process exited with exit code: ${code}`));
|
||||
const error = new Error(`Process exited with exit code: ${code}`);
|
||||
onError(error);
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user