Commit ec362f68 authored by Auto Backup's avatar Auto Backup

feat: 修改命令执行函数,使用父进程的shell执行命令

parent adf6cd49
#!/usr/bin/env node
const { exec, spawn } = require('child_process');
const { spawn, exec } = require('child_process');
const { promisify } = require('util');
const execAsync = promisify(exec);
const fs = require('fs');
const os = require('os');
const path = require('path');
// Platform detection
const isWindows = os.platform() === 'win32';
......@@ -14,19 +15,100 @@ const isLinux = os.platform() === 'linux';
console.log(`Platform: ${os.platform()} (${isWindows ? 'Windows' : isMacOS ? 'macOS' : isLinux ? 'Linux' : 'Other'})`);
/**
* Execute command and return result
* @param {string} command - Command to execute
* @returns {Promise<{success: boolean, stdout: string, stderr: string}>}
* 获取父进程的 shell 路径
*/
function getParentShell() {
try {
const parentPid = process.ppid;
if (isWindows) {
// Windows: 使用 PowerShell 获取父进程信息
return new Promise((resolve) => {
exec(`wmic process where ProcessId=${parentPid} get ExecutablePath`, (error, stdout) => {
if (error) {
console.log('⚠️ 无法获取父进程路径,使用默认 shell');
resolve(null);
return;
}
const lines = stdout.trim().split('\n');
if (lines.length >= 2) {
const parentPath = lines[1].trim();
// 检查是否是 PowerShell
if (parentPath.toLowerCase().includes('powershell') ||
parentPath.toLowerCase().includes('pwsh')) {
resolve(parentPath);
} else {
resolve(null);
}
} else {
resolve(null);
}
});
});
} else {
// Unix: 通过 /proc/<pid>/exe 获取
return new Promise((resolve) => {
fs.readlink(`/proc/${parentPid}/exe`, (error, link) => {
if (error) {
console.log('⚠️ 无法获取父进程路径,使用默认 shell');
resolve(null);
return;
}
resolve(link);
});
});
}
} catch (error) {
console.log(`⚠️ 获取父进程 shell 失败: ${error.message}`);
return Promise.resolve(null);
}
}
/**
* 使用指定 shell 执行命令
*/
function execWithShell(command, shellPath) {
return new Promise((resolve, reject) => {
const options = {
shell: shellPath || true,
windowsHide: true
};
exec(command, options, (error, stdout, stderr) => {
if (error) {
reject({ stdout: stdout.trim(), stderr: stderr.trim(), error: error.message });
} else {
resolve({ stdout: stdout.trim(), stderr: stderr.trim() });
}
});
});
}
/**
* Execute command using parent's shell if possible
*/
async function executeCommand(command) {
try {
const { stdout, stderr } = await execAsync(command);
return { success: true, stdout: stdout.trim(), stderr: stderr.trim() };
let parentShell = null;
if (isWindows) {
try {
parentShell = await getParentShell();
if (parentShell) {
console.log(`🔍 父进程 shell: ${parentShell}`);
}
} catch (e) {
// 忽略错误,使用默认
}
}
const result = await execWithShell(command, parentShell);
return { success: true, stdout: result.stdout, stderr: result.stderr };
} catch (error) {
return {
success: false,
stdout: error.stdout ? error.stdout.trim() : '',
stderr: error.stderr ? error.stderr.trim() : error.message
stdout: error.stdout || '',
stderr: error.stderr || error.error || ''
};
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment