refactor: Replace exec with spawn (#8510)

This commit is contained in:
Gal Zahavi
2025-09-16 12:03:17 -07:00
committed by GitHub
parent a015ea203f
commit 986b9fe7e9
11 changed files with 330 additions and 295 deletions

View File

@@ -10,7 +10,7 @@ import path from 'node:path';
import fs from 'node:fs';
import net from 'node:net';
import os from 'node:os';
import { execSync } from 'node:child_process';
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import crypto from 'node:crypto';
@@ -44,10 +44,14 @@ export function getJson(url) {
`gemini-cli-releases-${Date.now()}.json`,
);
try {
execSync(
`curl -sL -H "User-Agent: gemini-cli-dev-script" -o "${tmpFile}" "${url}"`,
{ stdio: 'pipe' },
const result = spawnSync(
'curl',
['-sL', '-H', 'User-Agent: gemini-cli-dev-script', '-o', tmpFile, url],
{ stdio: 'pipe', encoding: 'utf-8' },
);
if (result.status !== 0) {
throw new Error(result.stderr);
}
const content = fs.readFileSync(tmpFile, 'utf-8');
return JSON.parse(content);
} catch (e) {
@@ -62,9 +66,13 @@ export function getJson(url) {
export function downloadFile(url, dest) {
try {
execSync(`curl -fL -sS -o "${dest}" "${url}"`, {
const result = spawnSync('curl', ['-fL', '-sS', '-o', dest, url], {
stdio: 'pipe',
encoding: 'utf-8',
});
if (result.status !== 0) {
throw new Error(result.stderr);
}
return dest;
} catch (e) {
console.error(`Failed to download file from ${url}`);
@@ -254,10 +262,20 @@ export async function ensureBinary(
const actualExt = asset.name.endsWith('.zip') ? 'zip' : 'tar.gz';
let result;
if (actualExt === 'zip') {
execSync(`unzip -o "${archivePath}" -d "${tmpDir}"`, { stdio: 'pipe' });
result = spawnSync('unzip', ['-o', archivePath, '-d', tmpDir], {
stdio: 'pipe',
encoding: 'utf-8',
});
} else {
execSync(`tar -xzf "${archivePath}" -C "${tmpDir}"`, { stdio: 'pipe' });
result = spawnSync('tar', ['-xzf', archivePath, '-C', tmpDir], {
stdio: 'pipe',
encoding: 'utf-8',
});
}
if (result.status !== 0) {
throw new Error(result.stderr);
}
const nameToFind = binaryNameInArchive || executableName;