process.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Process helpers shared by the release scripts: the release steps drive `git`,
  3. * `pnpm`, `npm`, and `tar`, and each needs one of three failure behaviours.
  4. */
  5. import { spawnSync } from 'node:child_process'
  6. import { realpathSync } from 'node:fs'
  7. import { fileURLToPath } from 'node:url'
  8. /** Where and with what environment a release step runs a command. */
  9. export interface RunOptions {
  10. /** Working directory; defaults to the current one. */
  11. readonly cwd?: string
  12. /** Child environment; defaults to this process's. */
  13. readonly env?: NodeJS.ProcessEnv
  14. }
  15. /** What a command produced, for a caller that decides what a failure means. */
  16. export interface CommandResult {
  17. /** Exit status, or null when a signal ended the process. */
  18. readonly status: number | null
  19. /** Captured standard output. */
  20. readonly stdout: string
  21. /** Captured standard error. */
  22. readonly stderr: string
  23. }
  24. /**
  25. * Run a command and capture its output without judging the exit status.
  26. * @param command - executable name.
  27. * @param args - command arguments.
  28. * @param options - working directory and environment.
  29. * @returns The exit status and captured streams.
  30. */
  31. export function attempt(command: string, args: readonly string[], options: RunOptions = {}): CommandResult {
  32. const result = spawnSync(command, [...args], { cwd: options.cwd, env: options.env, encoding: 'utf8' })
  33. if (result.error !== undefined) throw result.error
  34. return { status: result.status, stdout: result.stdout, stderr: result.stderr }
  35. }
  36. /**
  37. * Run a command, letting its output reach the log while also returning it.
  38. *
  39. * A step that both shows progress and classifies its own failure needs both: the
  40. * output has to appear in the workflow log as the command produces it, and the
  41. * caller has to read it to decide whether a failure is worth retrying.
  42. * @param command - executable name.
  43. * @param args - command arguments.
  44. * @param options - working directory and environment.
  45. * @returns The exit status and captured streams.
  46. */
  47. export function attemptStreaming(command: string, args: readonly string[], options: RunOptions = {}): CommandResult {
  48. const result = spawnSync(command, [...args], {
  49. cwd: options.cwd,
  50. env: options.env,
  51. encoding: 'utf8',
  52. // 'inherit' would leave nothing to capture, so the streams are piped and
  53. // echoed instead.
  54. stdio: ['inherit', 'pipe', 'pipe'],
  55. })
  56. if (result.error !== undefined) throw result.error
  57. if (result.stdout !== '') process.stdout.write(result.stdout)
  58. if (result.stderr !== '') process.stderr.write(result.stderr)
  59. return { status: result.status, stdout: result.stdout, stderr: result.stderr }
  60. }
  61. /**
  62. * Run a command, capture its standard output, and fail on a non-zero exit.
  63. * @param command - executable name.
  64. * @param args - command arguments.
  65. * @param options - working directory and environment.
  66. * @returns The trimmed standard output.
  67. */
  68. export function capture(command: string, args: readonly string[], options: RunOptions = {}): string {
  69. const result = attempt(command, args, options)
  70. if (result.status !== 0) {
  71. throw new Error(`${command} ${args.join(' ')} exited with ${String(result.status)}:\n${result.stdout}\n${result.stderr}`)
  72. }
  73. return result.stdout.trim()
  74. }
  75. /**
  76. * Run a command with inherited streams, so its progress reaches the log, and
  77. * fail on a non-zero exit.
  78. * @param command - executable name.
  79. * @param args - command arguments.
  80. * @param options - working directory and environment.
  81. */
  82. export function run(command: string, args: readonly string[], options: RunOptions = {}): void {
  83. const result = spawnSync(command, [...args], { cwd: options.cwd, env: options.env, stdio: 'inherit' })
  84. if (result.error !== undefined) throw result.error
  85. if (result.status !== 0) throw new Error(`${command} ${args.join(' ')} exited with ${String(result.status)}`)
  86. }
  87. /**
  88. * Whether this module is the process entry point.
  89. *
  90. * The release scripts are both commands and modules: a test imports their pure
  91. * logic, and importing a module runs its body, so an unguarded `main()` would
  92. * run the wrong command with the wrong arguments.
  93. * @param moduleUrl - the caller's `import.meta.url`.
  94. * @returns True when Node started this module.
  95. */
  96. export function isEntry(moduleUrl: string): boolean {
  97. const invoked = process.argv[1]
  98. if (invoked === undefined) return false
  99. return realpathSync(invoked) === realpathSync(fileURLToPath(moduleUrl))
  100. }