import { spawnSync } from 'node:child_process'; import { accessSync, constants as fsConstants, statSync } from 'node:fs'; import { constants as osConstants } from 'node:os'; import { delimiter, dirname, join, resolve } from 'node:path'; export type VerifyPassthroughOptions = Readonly<{ cwd?: string; env?: NodeJS.ProcessEnv; platform?: NodeJS.Platform; stderr?: Pick; }>; const EXECUTABLE_NAME = 'plate-verify'; const candidateNames = (platform: NodeJS.Platform): readonly string[] => platform === 'win32' ? [`${EXECUTABLE_NAME}.exe`, `${EXECUTABLE_NAME}.cmd`, EXECUTABLE_NAME] : [EXECUTABLE_NAME]; const isExecutableFile = ( filePath: string, platform: NodeJS.Platform ): boolean => { try { if (!statSync(filePath).isFile()) return false; accessSync( filePath, platform === 'win32' ? fsConstants.F_OK : fsConstants.X_OK ); return true; } catch { return false; } }; const ancestorBinDirectories = (cwd: string): readonly string[] => { const directories: string[] = []; let current = resolve(cwd); while (true) { directories.push(join(current, 'node_modules', '.bin')); const parent = dirname(current); if (parent === current) break; current = parent; } return directories; }; /** * Locate a `plate-verify` executable in ancestor `node_modules/.bin` folders * before scanning `PATH`. */ export const findPlateVerifyExecutable = ( options: Pick = {} ): string | null => { const cwd = options.cwd ?? process.cwd(); const env = options.env ?? process.env; const platform = options.platform ?? process.platform; const names = candidateNames(platform); const pathValue = env.PATH ?? env.Path ?? ''; const searchDirs = [ ...ancestorBinDirectories(cwd), ...pathValue.split(delimiter).filter((entry) => entry.length > 0), ]; for (const directory of searchDirs) { for (const name of names) { const candidate = join(directory, name); if (isExecutableFile(candidate, platform)) { return candidate; } } } return null; }; const signalExitCode = (signal: NodeJS.Signals | null): number => { if (!signal) return 1; const signalNumber = osConstants.signals[signal]; return typeof signalNumber === 'number' ? 128 + signalNumber : 1; }; /** * Forward `plate verify [args...]` verbatim to `plate-verify` when installed * in `node_modules/.bin` or `PATH`. */ export const runVerifyPassthrough = ( args: readonly string[], options: VerifyPassthroughOptions = {} ): number => { const cwd = options.cwd ?? process.cwd(); const env = options.env ?? process.env; const platform = options.platform ?? process.platform; const stderr = options.stderr ?? process.stderr; const executable = findPlateVerifyExecutable({ cwd, env, platform }); if (!executable) { stderr.write( 'plate-verify executable was not found in node_modules/.bin or PATH. Install plate-verify before running "plate verify".\n' ); return 1; } const result = spawnSync(executable, [...args], { cwd, env, ...(platform === 'win32' && executable.toLowerCase().endsWith('.cmd') ? { shell: true } : {}), stdio: 'inherit', }); if (result.error) { stderr.write(`Failed to run "${executable}": ${result.error.message}\n`); return 1; } return result.status ?? signalExitCode(result.signal); };