import{a as x}from"./chunk-3ZUFRYJ4.js";import{a as c,b as p}from"./chunk-A5O5YHGN.js";import{a as O}from"./chunk-IPJHKYVM.js";import{a as h,b as A,c as l}from"./chunk-74CEPOFO.js";var y={name:"timeout",summary:"run a command with a time limit",usage:"timeout [OPTION] DURATION COMMAND [ARG]...",description:`Start COMMAND, and kill it if still running after DURATION.

DURATION is a number with optional suffix:
  s - seconds (default)
  m - minutes
  h - hours
  d - days`,options:["-k, --kill-after=DURATION  send KILL signal after DURATION if still running","-s, --signal=SIGNAL        specify signal to send (default: TERM)","    --preserve-status      exit with same status as COMMAND, even on timeout","    --foreground           run command in foreground","    --help                 display this help and exit"]},D={name:"timeout",async execute(s,n){if(A(s))return h(y);let i=0;for(let e=0;e<s.length;e++){let t=s[e];if(t==="--preserve-status")i=e+1;else if(t==="--foreground")i=e+1;else if(t==="-k"||t==="--kill-after")e++,i=e+1;else if(t.startsWith("--kill-after="))i=e+1;else if(t==="-s"||t==="--signal")e++,i=e+1;else if(t.startsWith("--signal="))i=e+1;else{if(t.startsWith("--")&&t!=="--")return l("timeout",t);if(t.startsWith("-")&&t.length>1&&t!=="--")if(t.startsWith("-k"))i=e+1;else if(t.startsWith("-s"))i=e+1;else return l("timeout",t);else{i=e;break}}}let r=s.slice(i);if(r.length===0)return{stdout:"",stderr:`timeout: missing operand
`,exitCode:1};let u=r[0],m=x(u);if(m===null)return{stdout:"",stderr:`timeout: invalid time interval '${u}'
`,exitCode:1};let d=r.slice(1);if(d.length===0)return{stdout:"",stderr:`timeout: missing operand
`,exitCode:1};if(!n.exec)return{stdout:"",stderr:`timeout: exec not available
`,exitCode:1};let v=O(d),f=new AbortController,o;try{let e=new Promise(a=>{o=c(()=>{f.abort(),a({timedOut:!0})},m)}),t=n.exec(v,{cwd:n.cwd,signal:f.signal,stdin:n.stdin}).then(a=>({timedOut:!1,result:a})),g=await Promise.race([e,t]);return g.timedOut?{stdout:"",stderr:"",exitCode:124}:g.result}finally{o!==void 0&&p(o)}}},I={name:"timeout",flags:[{flag:"-k",type:"value",valueHint:"string"},{flag:"-s",type:"value",valueHint:"string"},{flag:"--preserve-status",type:"boolean"},{flag:"--foreground",type:"boolean"}],needsArgs:!0,minArgs:2};export{D as a,I as b};
