wrap.ts (1019B)
1 import type { OpenpodContext } from "../context"; 2 import * as path from "node:path"; 3 4 export function wrap( 5 deps: OpenpodContext, 6 cwd: string, 7 command: string, 8 ): string { 9 function getLimaName(dir: string): string | null { 10 const { logger, openpodRoot } = deps; 11 try { 12 logger.log(`get lima name for ${openpodRoot} ${dir}`); 13 const rel = path.relative(openpodRoot, dir); 14 if (rel.startsWith("..") || path.isAbsolute(rel)) { 15 return null; 16 } 17 const first = rel.split(path.sep).find((p) => p.length > 0); 18 return first || null; 19 } catch { 20 return null; 21 } 22 } 23 24 const limaName = getLimaName(cwd); 25 if (!limaName) { 26 return command; 27 } 28 29 const escapedCwd = cwd.replace(/'/g, "'\\''"); 30 const escapedCmd = command.replace(/'/g, "'\\''"); 31 const wrapped = `limactl shell --preserve-env --workdir='${escapedCwd}' ${limaName} bash -c '${escapedCmd}'`; 32 33 deps.logger.log(`redirected shell command for lima ${limaName} (cwd=${cwd})`); 34 35 return wrapped; 36 }