blob: e9901b1ed96bd8550bc090305239d064ffc116e1 [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { spawn } from 'node:child_process';
const DEFAULT_COMMAND_TIMEOUT_MS = 30_000;
export interface RuntimeHostServiceManagerCommandResult {
readonly exitCode: number;
readonly stdout: string;
readonly stderr: string;
}
export function runRuntimeHostServiceManagerCommand(
command: string,
args: readonly string[],
): Promise<RuntimeHostServiceManagerCommandResult> {
return new Promise((resolveResult, reject) => {
const child = spawn(command, args, {
stdio: ['ignore', 'pipe', 'pipe'],
windowsHide: true,
timeout: DEFAULT_COMMAND_TIMEOUT_MS,
killSignal: 'SIGKILL',
});
let stdout = '';
let stderr = '';
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
child.stdout.on('data', (chunk: string) => {
stdout += chunk;
});
child.stderr.on('data', (chunk: string) => {
stderr += chunk;
});
child.once('error', reject);
child.once('close', (exitCode) => {
resolveResult({ exitCode: exitCode ?? 1, stdout, stderr });
});
});
}