// 외부 프로그램(크롬·claude·codex·ffmpeg) 위치와 부르는 법. 맥·윈도우 둘 다에서 돌게 한 곳에 모은다. import { execFileSync } from 'node:child_process' import { existsSync, readFileSync } from 'node:fs' import { dirname, join } from 'node:path' const WIN = process.platform === 'win32' export const CHROME = process.env.CHROME_PATH ?? (WIN ? [`${process.env.PROGRAMFILES}/Google/Chrome/Application/chrome.exe`, `${process.env['PROGRAMFILES(X86)']}/Google/Chrome/Application/chrome.exe`, `${process.env.LOCALAPPDATA}/Google/Chrome/Application/chrome.exe`].find((p) => existsSync(p)) ?? 'chrome.exe' : process.platform === 'darwin' ? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' : 'google-chrome') /** * execFile 로 부를 [실행 파일, ...앞 인자]. CLAUDE_BIN·CODEX_BIN 환경변수로 바꿀 수 있다. * 윈도우에서 npm 으로 깐 CLI 는 .cmd 라 shell 없이는 못 부르므로(인자 따옴표가 깨짐) .cmd 가 가리키는 .js 를 node 로 직접 부른다. */ export function cli(name) { const override = process.env[`${name.toUpperCase()}_BIN`] if (override) return [override] if (!WIN) return [name] let hits = [] try { hits = execFileSync('where', [name], { encoding: 'utf8' }).split(/\r?\n/).filter(Boolean) } catch { throw new Error(`${name} 을(를) 찾지 못했어요. 설치했는지 보고, ${name.toUpperCase()}_BIN 환경변수로 실행 파일 경로를 알려 주세요.`) } const exe = hits.find((p) => p.toLowerCase().endsWith('.exe')) if (exe) return [exe] const shim = hits.find((p) => p.toLowerCase().endsWith('.cmd')) const js = shim && readFileSync(shim, 'utf8').match(/%dp0%\\([^"]+?\.js)/)?.[1] if (!js) throw new Error(`${name} 실행 파일(${hits.join(', ')})을 부를 수 없어요. ${name.toUpperCase()}_BIN 환경변수로 .exe 경로를 알려 주세요.`) return [process.execPath, join(dirname(shim), js)] } /** png → jpg (품질 높게) */ export function toJpeg(png, jpg) { execFileSync('ffmpeg', ['-y', '-loglevel', 'error', '-i', png, '-q:v', '2', jpg], { stdio: 'ignore' }) } /** png → webp, 가로 width 로 줄인다. cwebp 가 있으면 쓰고(맥 brew), 없으면 ffmpeg 의 libwebp(윈도우 배포판에 들어 있다) */ export function toWebp(png, webp, width = 1024) { try { execFileSync('cwebp', ['-quiet', '-q', '88', '-resize', String(width), '0', png, '-o', webp]) } catch (e) { if (e.code !== 'ENOENT') throw e execFileSync('ffmpeg', ['-y', '-loglevel', 'error', '-i', png, '-vf', `scale=${width}:-1`, '-c:v', 'libwebp', '-quality', '88', webp]) } }