process.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. readonly cwd?: string
  11. readonly env?: NodeJS.ProcessEnv
  12. }
  13. /** What a command produced, for a caller that decides what a failure means. */
  14. export interface CommandResult {
  15. /** Exit status, or null when a signal ended the process. */
  16. readonly status: number | null
  17. readonly stdout: string
  18. readonly stderr: string
  19. }
  20. /**
  21. * Run a command and capture its output without judging the exit status.
  22. * @param command - executable name.
  23. * @param args - command arguments.
  24. * @param options - working directory and environment.
  25. * @returns The exit status and captured streams.
  26. */
  27. export function attempt(command: string, args: readonly string[], options: RunOptions = {}): CommandResult {
  28. const result = spawnSync(command, [...args], { cwd: options.cwd, env: options.env, encoding: 'utf8' })
  29. if (result.error !== undefined) throw result.error
  30. return { status: result.status, stdout: result.stdout, stderr: result.stderr }
  31. }
  32. /**
  33. * Run a command, then echo and return its captured output. Output is buffered
  34. * until exit and stdout precedes stderr.
  35. * @param command - executable name.
  36. * @param args - command arguments.
  37. * @param options - working directory and environment.
  38. * @returns The exit status and captured streams.
  39. */
  40. export function attemptEchoed(command: string, args: readonly string[], options: RunOptions = {}): CommandResult {
  41. const result = spawnSync(command, [...args], {
  42. cwd: options.cwd,
  43. env: options.env,
  44. encoding: 'utf8',
  45. stdio: ['inherit', 'pipe', 'pipe'],
  46. })
  47. if (result.error !== undefined) throw result.error
  48. if (result.stdout !== '') process.stdout.write(result.stdout)
  49. if (result.stderr !== '') process.stderr.write(result.stderr)
  50. return { status: result.status, stdout: result.stdout, stderr: result.stderr }
  51. }
  52. /**
  53. * Run a command, capture its standard output, and fail on a non-zero exit.
  54. * @param command - executable name.
  55. * @param args - command arguments.
  56. * @param options - working directory and environment.
  57. * @returns The trimmed standard output.
  58. */
  59. export function capture(command: string, args: readonly string[], options: RunOptions = {}): string {
  60. const result = attempt(command, args, options)
  61. if (result.status !== 0) {
  62. throw new Error(`${command} ${args.join(' ')} exited with ${String(result.status)}:\n${result.stdout}\n${result.stderr}`)
  63. }
  64. return result.stdout.trim()
  65. }
  66. /**
  67. * Run a command with inherited streams, so its progress reaches the log, and
  68. * fail on a non-zero exit.
  69. * @param command - executable name.
  70. * @param args - command arguments.
  71. * @param options - working directory and environment.
  72. */
  73. export function run(command: string, args: readonly string[], options: RunOptions = {}): void {
  74. const result = spawnSync(command, [...args], { cwd: options.cwd, env: options.env, stdio: 'inherit' })
  75. if (result.error !== undefined) throw result.error
  76. if (result.status !== 0) throw new Error(`${command} ${args.join(' ')} exited with ${String(result.status)}`)
  77. }
  78. /**
  79. * Return whether Node started the given module as the process entry point.
  80. * @param moduleUrl - the caller's `import.meta.url`.
  81. * @returns True when Node started this module.
  82. */
  83. export function isEntry(moduleUrl: string): boolean {
  84. const invoked = process.argv[1]
  85. if (invoked === undefined) return false
  86. return realpathSync(invoked) === realpathSync(fileURLToPath(moduleUrl))
  87. }