Download src/utils/process-helper.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 9.55 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/utils/process-helper.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/utils/process-helper.js
-
curl -L -o process-helper.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/utils/process-helper.js
9.55 kB
| /** | |
| * ๊ฒฝ๋ก: /Users/jungkibong/Projects/langfuse/claude-desktop-cli/src/utils/process-helper.js | |
| * ํ์ผ: process-helper.js | |
| * ๋ฒ์ : v1.1.0 | |
| * Rationale: | |
| * - Electron PTY ๋ฐ child_process์ ์๋ช ์ฃผ๊ธฐ๋ฅผ ์์ ํ๊ฒ ๊ด๋ฆฌํ๊ณ ์ข๋น ํ๋ก์ธ์ค ๋์ถ์ ๋ฐฉ์งํ๊ธฐ ์ํ ํ๋ก์ธ์ค ํธ๋ฆฌ ์ข ๋ฃ ๋ฐ ์ ํจ์ฑ ๊ฒ์ฌ ์ ํธ๋ฆฌํฐ. | |
| * - Unix/macOS์์ ์์ PID(-pid)๋ฅผ ์ด์ฉํ SIGKILL ์ ๋ฉ์ธ ์ผ๋ ํธ๋ก ํ๋ก์ธ์ค ๊ทธ๋ฃน ์ ์ฒด๊ฐ ์ฆ์ฌํ๋ ๊ฒฐํจ์ ํด๊ฒฐํฉ๋๋ค. | |
| * - ๋๊ธฐ์ execSync('lsof') ํธ์ถ๋ก ์ธํ UI ํ๋ฆฌ์ง์ ์ฐจ๋จํ๊ธฐ ์ํด ๋น๋๊ธฐ getProcessCwdAsync์ 1์ด ์บ์ฑ TTL ๋ ์ด์ด๋ฅผ ์ถ๊ฐํฉ๋๋ค. | |
| */ | |
| const { exec, execSync } = require('child_process'); | |
| // CWD ์บ์ ๋งต ๋ฐ TTL ์ค์ | |
| const cwdCache = new Map(); | |
| const CACHE_TTL_MS = 1000; // 1์ด ์บ์ ๋ง๋ฃ ์๊ฐ | |
| /** | |
| * ํน์ PID๋ฅผ ๊ฐ์ง ํ๋ก์ธ์ค๊ฐ ํ์ฑํ ์ํ์ธ์ง ํ์ธํฉ๋๋ค. | |
| * @param {number} pid - ํ๋ก์ธ์ค ID | |
| * @returns {boolean} | |
| */ | |
| function isProcessAlive(pid) { | |
| if (!pid) return false; | |
| const numericPid = parseInt(pid, 10); | |
| if (isNaN(numericPid) || !Number.isInteger(numericPid) || numericPid <= 0) return false; | |
| try { | |
| process.kill(numericPid, 0); | |
| return true; | |
| } catch (e) { | |
| return false; | |
| } | |
| } | |
| /** | |
| * ์ง์ ๋ PID๋ฅผ ๊ฐ์ง ํ๋ก์ธ์ค์ ๊ทธ ํ์์ ๋ชจ๋ ์์ ํ๋ก์ธ์ค ํธ๋ฆฌ๋ฅผ ๊ฐ์ ์ข ๋ฃํฉ๋๋ค. | |
| * @param {number} pid - ํ๋ก์ธ์ค ID | |
| * @param {boolean} isSync - ๋๊ธฐ ์คํ ์ฌ๋ถ | |
| */ | |
| function killProcessTree(pid, isSync = false) { | |
| if (!pid) return; | |
| const numericPid = parseInt(pid, 10); | |
| if (isNaN(numericPid) || !Number.isInteger(numericPid) || numericPid <= 0) return; | |
| const isWin = process.platform === 'win32'; | |
| if (isWin) { | |
| const cmd = `taskkill /F /T /PID ${numericPid}`; | |
| if (isSync) { | |
| try { | |
| execSync(cmd, { stdio: 'ignore', windowsHide: true }); | |
| } catch (err) { | |
| // ์ด๋ฏธ ํ๋ก์ธ์ค๊ฐ ์ข ๋ฃ๋์๊ฑฐ๋ ์ ๊ทผ์ด ๊ฑฐ๋ถ๋ ๊ฒฝ์ฐ ๋ฌด์ | |
| } | |
| } else { | |
| exec(cmd, { windowsHide: true }, (err) => { | |
| // ๋ฐฑ๊ทธ๋ผ์ด๋ ํ์คํฌํฌ ์ค๋ฅ ๋ฌด์ | |
| }); | |
| } | |
| } else { | |
| // Unix/macOS: ์์ PID(-pid)๋ก SIGKILL์ ๋ ๋ฆฌ๋ฉด ๋ฉ์ธ Electron ํ๋ก์ธ์ค ๊ทธ๋ฃน์ด ๋๋ฐ ํญ์ฌํ ์ํ์ด ์์ต๋๋ค. | |
| // ๋์ pgrep -P๋ฅผ ํ์ฉํด ์ง์ ํ์ ์์ PIDs๋ฅผ ์กฐํํ๊ณ ๋ฃจํ๋ฅผ ๋๋ ค ๊ฐ๋ณ ์ข ๋ฃํฉ๋๋ค. | |
| try { | |
| let childrenRaw = ''; | |
| try { | |
| // pgrep -P๋ฅผ ๋๊ธฐ/๋์ ์คํํ์ฌ ํ์ ์์ ๋ชฉ๋ก์ ์์ง | |
| childrenRaw = execSync(`pgrep -P ${numericPid}`, { | |
| encoding: 'utf8', | |
| stdio: ['pipe', 'pipe', 'ignore'], | |
| timeout: 1000 | |
| }); | |
| } catch (e) { | |
| // ์์ ํ๋ก์ธ์ค๊ฐ ์๊ฑฐ๋ pgrep ์คํจ ์ ๋ฌด์ | |
| } | |
| if (childrenRaw) { | |
| const childPids = childrenRaw.split('\n') | |
| .map(p => parseInt(p.trim(), 10)) | |
| .filter(p => !isNaN(p) && p > 0); | |
| for (const childPid of childPids) { | |
| killProcessTree(childPid, isSync); // ์ฌ๊ท์ ์์ ์ ๋ฆฌ | |
| } | |
| } | |
| // ์ต์ข ์ ์ผ๋ก ์๊ธฐ ์์ ์์ ํ๊ฒ SIGKILL | |
| process.kill(numericPid, 'SIGKILL'); | |
| } catch (err) { | |
| // ํ๋ก์ธ์ค๊ฐ ์ด๋ฏธ ์ข ๋ฃ๋ ๊ฒฝ์ฐ ๋ฌด์ | |
| } | |
| } | |
| } | |
| /** | |
| * ํน์ PID๋ฅผ ๊ฐ์ง ํ๋ก์ธ์ค์ ํ์ฌ ์์ ๋๋ ํ ๋ฆฌ(CWD)๋ฅผ ๊ฐ์ ธ์ต๋๋ค (๋๊ธฐ์ - ํ์ ํธํ์ฑ ์ ์ฉ). | |
| * @param {number} pid - ํ๋ก์ธ์ค ID | |
| * @returns {string|null} | |
| */ | |
| function getProcessCwd(pid) { | |
| if (!pid) return null; | |
| const numericPid = parseInt(pid, 10); | |
| if (isNaN(numericPid) || !Number.isInteger(numericPid) || numericPid <= 0) return null; | |
| const isWin = process.platform === 'win32'; | |
| try { | |
| if (isWin) { | |
| const cmd = `powershell.exe -NoProfile -Command "(Get-CimInstance Win32_Process -Filter \\"ProcessId = ${numericPid}\\").CurrentWorkingDirectory"`; | |
| const output = execSync(cmd, { encoding: 'utf8', timeout: 2000, windowsHide: true }).trim(); | |
| return output || null; | |
| } else { | |
| // Linux ์ ์ฉ ์ต์ ํ: ์ธ๋ถ ํ๋ก์ธ์ค ์คํฐ ์์ด /proc/<pid>/cwd ๋งํฌ ์ง์ ํด์ | |
| if (process.platform === 'linux') { | |
| const fs = require('fs'); | |
| const procPath = `/proc/${numericPid}/cwd`; | |
| if (fs.existsSync(procPath)) { | |
| return fs.readlinkSync(procPath); | |
| } | |
| } | |
| const cmd = `lsof -a -d cwd -p ${numericPid} -Fn`; | |
| const output = execSync(cmd, { encoding: 'utf8', timeout: 2000 }); | |
| const lines = output.split('\n'); | |
| for (const line of lines) { | |
| if (line.startsWith('n')) { | |
| return line.slice(1).trim(); | |
| } | |
| } | |
| } | |
| } catch (e) { | |
| // ์๋ฌ ๋ฐ์ ์ ๋ฌด์ | |
| } | |
| return null; | |
| } | |
| /** | |
| * ํน์ PID ํ๋ก์ธ์ค์ CWD๋ฅผ ๋น๋๊ธฐ์์ผ๋ก ๊ฐ์ ธ์ต๋๋ค (๋น๋ธ๋กํน - ๊ถ์ฅ). | |
| * @param {number} pid - ํ๋ก์ธ์ค ID | |
| * @returns {Promise<string|null>} | |
| */ | |
| function getProcessCwdAsync(pid) { | |
| if (!pid) return Promise.resolve(null); | |
| const numericPid = parseInt(pid, 10); | |
| if (isNaN(numericPid) || !Number.isInteger(numericPid) || numericPid <= 0) return Promise.resolve(null); | |
| const now = Date.now(); | |
| const cached = cwdCache.get(numericPid); | |
| if (cached && (now - cached.timestamp < CACHE_TTL_MS)) { | |
| return Promise.resolve(cached.path); | |
| } | |
| const isWin = process.platform === 'win32'; | |
| return new Promise((resolve) => { | |
| if (isWin) { | |
| const cmd = `powershell.exe -NoProfile -Command "(Get-CimInstance Win32_Process -Filter \\"ProcessId = ${numericPid}\\").CurrentWorkingDirectory"`; | |
| exec(cmd, { timeout: 1500, windowsHide: true }, (err, stdout) => { | |
| if (err || !stdout) { | |
| resolve(null); | |
| } else { | |
| const resolvedPath = stdout.trim(); | |
| cwdCache.set(numericPid, { path: resolvedPath, timestamp: Date.now() }); | |
| resolve(resolvedPath); | |
| } | |
| }); | |
| } else { | |
| // Linux ์ ์ฉ ์ต์ ํ: /proc/<pid>/cwd ๋งํฌ ๋น๋๊ธฐ/๋๊ธฐ ๊ฒฐํฉ ํด์ | |
| if (process.platform === 'linux') { | |
| const fs = require('fs'); | |
| const procPath = `/proc/${pid}/cwd`; | |
| try { | |
| if (fs.existsSync(procPath)) { | |
| const resolvedPath = fs.readlinkSync(procPath); | |
| cwdCache.set(pid, { path: resolvedPath, timestamp: Date.now() }); | |
| resolve(resolvedPath); | |
| return; | |
| } | |
| } catch (procErr) { | |
| // proc ์คํจ ์ lsof ํด๋ฐฑ ๊ฐ๋ | |
| } | |
| } | |
| const cmd = `lsof -a -d cwd -p ${pid} -Fn`; | |
| exec(cmd, { timeout: 1500 }, (err, stdout) => { | |
| if (err || !stdout) { | |
| resolve(null); | |
| } else { | |
| const lines = stdout.split('\n'); | |
| for (const line of lines) { | |
| if (line.startsWith('n')) { | |
| const resolvedPath = line.slice(1).trim(); | |
| cwdCache.set(pid, { path: resolvedPath, timestamp: Date.now() }); | |
| resolve(resolvedPath); | |
| return; | |
| } | |
| } | |
| resolve(null); | |
| } | |
| }); | |
| } | |
| }); | |
| } | |
| /** | |
| * ํ์ฌ ํ๋ก์ ํธ ๋๋ ํ ๋ฆฌ๋ ํ๊ฒฝ ๋ณ์, ์์ ๋๋ ํ ๋ฆฌ ๊ธฐ์ค์์ ํ์ด์ฌ ๊ฐ์ํ๊ฒฝ(venv)์ ๊ฒ์ํ์ฌ | |
| * ๊ฐ์ํ๊ฒฝ ๋ด์ Python ์คํ ํ์ผ ๊ฒฝ๋ก์ ํ์ ํ๊ฒฝ๋ณ์(PATH, VIRTUAL_ENV)๋ฅผ ์ค์ ํ env ๋ณต์ฌ๋ณธ์ ๋ฐํํฉ๋๋ค. | |
| * ๊ฐ์ํ๊ฒฝ์ด ๋ฐ๊ฒฌ๋์ง ์์ผ๋ฉด ์์คํ ๊ธฐ๋ณธ python3 ๋๋ python์ ๋ฐํํฉ๋๋ค. | |
| * @param {string} projectDir - ํ๋ก์ ํธ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก (์ต์ ) | |
| * @returns {{pythonPath: string, env: Object}} | |
| */ | |
| function getPythonEnvConfig(projectDir = null) { | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const isWin = process.platform === 'win32'; | |
| const binDir = isWin ? 'Scripts' : 'bin'; | |
| const pythonExe = isWin ? 'python.exe' : 'python3'; | |
| let detectedVenv = null; | |
| // 1. process.env.VIRTUAL_ENV ํ๊ฒฝ ๋ณ์ ๊ฒ์ฌ | |
| if (process.env.VIRTUAL_ENV && fs.existsSync(path.join(process.env.VIRTUAL_ENV, binDir, pythonExe))) { | |
| detectedVenv = process.env.VIRTUAL_ENV; | |
| } | |
| // 2. ์ง์ ๋ projectDir ํ์ .venv ๋๋ venv ๋๋ ํ ๋ฆฌ ๊ฒ์ฌ | |
| if (!detectedVenv && projectDir) { | |
| const candidates = [path.join(projectDir, '.venv'), path.join(projectDir, 'venv')]; | |
| for (const cand of candidates) { | |
| if (fs.existsSync(path.join(cand, binDir, pythonExe))) { | |
| detectedVenv = cand; | |
| break; | |
| } | |
| } | |
| } | |
| // 3. ํ์ฌ ์์ ๋๋ ํ ๋ฆฌ(process.cwd()) ํ์ .venv ๋๋ venv ๋๋ ํ ๋ฆฌ ๊ฒ์ฌ | |
| if (!detectedVenv) { | |
| const candidates = [path.join(process.cwd(), '.venv'), path.join(process.cwd(), 'venv')]; | |
| for (const cand of candidates) { | |
| if (fs.existsSync(path.join(cand, binDir, pythonExe))) { | |
| detectedVenv = cand; | |
| break; | |
| } | |
| } | |
| } | |
| const env = Object.assign({}, process.env); | |
| let pythonPath = isWin ? 'python' : 'python3'; | |
| // ๊ฐ์ํ๊ฒฝ์ด ๊ฐ์ง๋ ๊ฒฝ์ฐ ํ๊ฒฝ๋ณ์๋ฅผ ๊ฒฉ๋ฆฌ ๋ฐ ๋ณด์ ์ฒ๋ฆฌ | |
| if (detectedVenv) { | |
| const venvBin = path.join(detectedVenv, binDir); | |
| pythonPath = path.join(venvBin, pythonExe); | |
| env.VIRTUAL_ENV = detectedVenv; | |
| // PATH ํ๊ฒฝ๋ณ์์ ๊ฐ์ฅ ๋งจ ์์ ๊ฐ์ํ๊ฒฝ bin ๋๋ ํ ๋ฆฌ๋ฅผ ์ฃผ์ ํ์ฌ ์คํ ํ์ผ ์ฐ์ ์์๋ฅผ ํ๋ณด | |
| env.PATH = venvBin + (isWin ? ';' : ':') + (process.env.PATH || ''); | |
| } | |
| return { pythonPath, env }; | |
| } | |
| module.exports = { | |
| isProcessAlive, | |
| killProcessTree, | |
| getProcessCwd, | |
| getProcessCwdAsync, | |
| getPythonEnvConfig | |
| }; | |