build-ts.mjs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env node
  2. import { spawn } from 'node:child_process'
  3. import { once } from 'node:events'
  4. import { existsSync } from 'node:fs'
  5. import path from 'node:path'
  6. import { stdin, env } from 'node:process'
  7. import { createInterface as readLines } from 'node:readline'
  8. import { fileURLToPath } from 'node:url'
  9. const fromYarn = 'npm_execpath' in env
  10. const exe = fromYarn ? env.npm_execpath : 'corepack'
  11. const argv0 = fromYarn ? [] : ['yarn']
  12. const cwd = fileURLToPath(new URL('../', import.meta.url))
  13. for await (const line of readLines(stdin)) {
  14. const { location, name } = JSON.parse(line)
  15. if (existsSync(path.join(cwd, location, 'tsconfig.json'))) {
  16. const cp = spawn(exe, [...argv0, 'tsc', '-p', location], {
  17. stdio: 'inherit',
  18. cwd,
  19. })
  20. await Promise.race([
  21. once(cp, 'error').then(err => Promise.reject(err)),
  22. await once(cp, 'exit')
  23. .then(([code]) => (code && Promise.reject(new Error(`Non-zero exit code when building "${name}": ${code}`)))),
  24. ])
  25. }
  26. if (existsSync(path.join(cwd, location, 'tsconfig.build.json'))) {
  27. const cp = spawn(exe, [...argv0, 'tsc', '--build', path.join(cwd, location, 'tsconfig.build.json')], {
  28. stdio: 'inherit',
  29. cwd,
  30. })
  31. await Promise.race([
  32. once(cp, 'error').then(err => Promise.reject(err)),
  33. await once(cp, 'exit')
  34. .then(([code]) => (code && Promise.reject(new Error(`Non-zero exit code when building "${name}": ${code}`)))),
  35. ])
  36. }
  37. }