{"version":3,"file":"execAsync.mjs","names":[],"sources":["../../src/utils/execAsync.ts"],"sourcesContent":["/* eslint-disable jsdoc/no-defaults */\nimport { type, type Type } from 'arktype';\nimport { exec, type ExecException } from 'node:child_process';\nimport { constants } from 'node:os';\nimport { promisify } from 'node:util';\nimport { isError } from './isError.ts';\n\n/**\n * A `promisify(exec)` wrapper to optionally assign the child process's STDERR as the {@link Error.prototype.cause}.\n * @see {@link promisify}, {@link exec}\n * @param command The command to run, with space-separated arguments.\n * @param [shouldSetStderrAsCause=false] If true and the child process's stderr is available, the thrown Error's {@link Error.prototype.cause} is assigned the stderr string.\n * @returns A promise of the child process's STDOUT and STDERR streams as strings\n * @throws {Error | ChildProcessSpawnException}\n */\nexport async function execAsync(command: string, shouldSetStderrAsCause = false): Promise<{\n  stdout: string;\n  stderr: string;\n}> {\n  try {\n    return await promisify(exec)(command);\n  }\n  catch (error: unknown) {\n    if (!isError(error))\n      throw new Error(JSON.stringify(error), { cause: error });\n\n    if (shouldSetStderrAsCause && 'stderr' in error && typeof error.stderr === 'string' && error.stderr !== '')\n      error.cause ??= error.stderr;\n\n    if ('stdout' in error && typeof error.stdout === 'string') {\n      error.message\n        += '\\nSTDOUT:\\n'\n          + `  ${error.stdout.replaceAll('\\n', '\\n  ')}`;\n    }\n    if ('stderr' in error && typeof error.stderr === 'string') {\n      error.message\n        += '\\nSTDERR:\\n'\n          + `  ${error.stderr.replaceAll('\\n', '\\n  ')}`;\n    }\n\n    throw new ChildProcessSpawnException(error.message, error);\n  }\n}\n\nconst T_ExecException: Type<\n  Error\n  & Omit<ExecException, 'cmd' | 'code' | 'signal'>\n  & {\n    cmd?: ExecException['cmd'];\n    code?: number | string | undefined;\n    signal?: ExecException['signal'] | null;\n  }\n> = type.instanceOf(Error).and({\n  'cmd?': 'string',\n  'code?': 'number',\n  'killed?': 'boolean',\n  'signal?': type((Object.keys(constants.signals) as NodeJS.Signals[])\n    .map(v => type(`'${v}'`))\n    // eslint-disable-next-line unicorn/no-array-reduce\n    .reduce((previous, current) => previous.or(current)))\n    .or('null'),\n  'stdout?': 'string',\n  'stderr?': 'string',\n});\n\n// A class can only implement an identifier/qualified-name with optional type arguments. ts(2500)\ntype _ExecException = typeof T_ExecException.inferOut;\n\nexport class ChildProcessSpawnException extends Error implements _ExecException {\n  cmd: typeof T_ExecException.inferOut.cmd;\n  code: typeof T_ExecException.inferOut.code;\n  killed: typeof T_ExecException.inferOut.killed;\n  signal: typeof T_ExecException.inferOut.signal;\n  stderr: typeof T_ExecException.inferOut.stderr;\n  stdout: typeof T_ExecException.inferOut.stdout;\n\n  constructor(\n    message: Parameters<typeof Error>[0],\n    options: typeof T_ExecException.inferIn,\n  ) {\n    options = T_ExecException.from(options);\n    super(message, options);\n    this.cmd = options.cmd;\n    this.code = options.code;\n    this.killed = options.killed;\n    this.signal = options.signal;\n    this.stderr = options.stderr;\n    this.stdout = options.stdout;\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;AAeA,eAAsB,UAAU,SAAiB,yBAAyB,OAGvE;CACD,IAAI;EACF,OAAO,MAAM,UAAU,IAAI,CAAC,CAAC,OAAO;CACtC,SACO,OAAgB;EACrB,IAAI,CAAC,QAAQ,KAAK,GAChB,MAAM,IAAI,MAAM,KAAK,UAAU,KAAK,GAAG,EAAE,OAAO,MAAM,CAAC;EAEzD,IAAI,0BAA0B,YAAY,SAAS,OAAO,MAAM,WAAW,YAAY,MAAM,WAAW,IACtG,MAAM,UAAU,MAAM;EAExB,IAAI,YAAY,SAAS,OAAO,MAAM,WAAW,UAC/C,MAAM,WACD;;IACM,MAAM,OAAO,WAAW,MAAM,MAAM;EAEjD,IAAI,YAAY,SAAS,OAAO,MAAM,WAAW,UAC/C,MAAM,WACD;;IACM,MAAM,OAAO,WAAW,MAAM,MAAM;EAGjD,MAAM,IAAI,2BAA2B,MAAM,SAAS,KAAK;CAC3D;AACF;AAEA,MAAM,kBAQF,KAAK,WAAW,KAAK,CAAC,CAAC,IAAI;CAC7B,QAAQ;C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