{"version":3,"file":"bin.mjs","names":[],"sources":["../../../../src/utils/runParallel/bin.ts"],"sourcesContent":["import type { ChildProcess } from 'node:child_process';\nimport { type SpawnOptions, spawn } from 'node:child_process';\n\ntype BinCallback = (err: Error | null, stdout?: string, code?: number) => void;\n\nconst stripStderr = (stderr: string | undefined): string | undefined => {\n  if (!stderr) return;\n  let cleaned = stderr.trim();\n  // Strip bogus screen size error.\n  // See https://github.com/microsoft/vscode/issues/98590\n  const regex = /your \\d+x\\d+ screen size is bogus\\. expect trouble/gi;\n  cleaned = cleaned.replace(regex, '');\n\n  return cleaned.trim() || undefined;\n};\n\n/**\n * Spawn a binary and read its stdout.\n * @param cmd The name of the binary to spawn.\n * @param args The arguments for the binary.\n * @param options Optional option for the spawn function.\n * @param done Callback function.\n */\nexport const run = (\n  cmd: string,\n  args: string[],\n  options: SpawnOptions | BinCallback | undefined,\n  done?: BinCallback\n): void => {\n  let normalizedOptions: SpawnOptions | undefined;\n  let normalizedDone: BinCallback;\n\n  if (typeof options === 'function') {\n    normalizedDone = options;\n    normalizedOptions = undefined;\n  } else {\n    normalizedDone = done!;\n    normalizedOptions = options;\n  }\n\n  let executed = false;\n  const ch: ChildProcess = spawn(cmd, args, normalizedOptions ?? {});\n  let stdout = '';\n  let stderr = '';\n\n  if (ch.stdout) {\n    ch.stdout.on('data', (d: Buffer) => {\n      stdout += d.toString();\n    });\n  }\n\n  if (ch.stderr) {\n    ch.stderr.on('data', (d: Buffer) => {\n      stderr += d.toString();\n    });\n  }\n\n  ch.on('error', (err: Error) => {\n    if (executed) return;\n    executed = true;\n    normalizedDone(new Error(String(err)));\n  });\n\n  ch.on('close', (code: number | null) => {\n    if (executed) return;\n    executed = true;\n\n    const cleanedStderr = stripStderr(stderr);\n    if (cleanedStderr) {\n      return normalizedDone(new Error(cleanedStderr));\n    }\n\n    normalizedDone(null, stdout, code ?? undefined);\n  });\n};\n"],"mappings":";;;AAKA,MAAM,eAAe,WAAmD;CACtE,IAAI,CAAC,QAAQ;CACb,IAAI,UAAU,OAAO,KAAK;CAI1B,UAAU,QAAQ,QAAQ,wDAAO,EAAE;CAEnC,OAAO,QAAQ,KAAK,KAAK;AAC3B;;;;;;;;AASA,MAAa,OACX,KACA,MACA,SACA,SACS;CACT,IAAI;CACJ,IAAI;CAEJ,IAAI,OAAO,YAAY,YAAY;EACjC,iBAAiB;EACjB,oBAAoB;CACtB,OAAO;EACL,iBAAiB;EACjB,oBAAoB;CACtB;CAEA,IAAI,WAAW;CACf,MAAM,KAAmB,MAAM,KAAK,MAAM,qBAAqB,CAAC,CAAC;CACjE,IAAI,SAAS;CACb,IAAI,SAAS;CAEb,IAAI,GAAG,QACL,GAAG,OAAO,GAAG,SAAS,MAAc;EAClC,UAAU,EAAE,SAAS;CACvB,CAAC;CAGH,IAAI,GAAG,QACL,GAAG,OAAO,GAAG,SAAS,MAAc;EAClC,UAAU,EAAE,SAAS;CACvB,CAAC;CAGH,GAAG,GAAG,UAAU,QAAe;EAC7B,IAAI,UAAU;EACd,WAAW;EACX,eAAe,IAAI,MAAM,OAAO,GAAG,CAAC,CAAC;CACvC,CAAC;CAED,GAAG,GAAG,UAAU,SAAwB;EACtC,IAAI,UAAU;EACd,WAAW;EAEX,MAAM,gBAAgB,YAAY,MAAM;EACxC,IAAI,eACF,OAAO,eAAe,IAAI,MAAM,aAAa,CAAC;EAGhD,eAAe,MAAM,QAAQ,QAAQ,MAAS;CAChD,CAAC;AACH"}