build-ts.mjs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. const locations = []
  14. for await (const line of readLines(stdin)) {
  15. const { location } = JSON.parse(line)
  16. if (existsSync(path.join(cwd, location, 'tsconfig.json'))) {
  17. locations.unshift(location)
  18. }
  19. const tsConfigBuildPath = path.join(cwd, location, 'tsconfig.build.json')
  20. if (existsSync(tsConfigBuildPath)) {
  21. locations.push(tsConfigBuildPath)
  22. }
  23. }
  24. const cp = spawn(exe, [...argv0, 'tsc', '--build', ...locations], {
  25. stdio: 'inherit',
  26. cwd,
  27. })
  28. await Promise.race([
  29. once(cp, 'error').then(err => Promise.reject(err)),
  30. await once(cp, 'exit')
  31. .then(([code]) => (code && Promise.reject(new Error(`Non-zero exit code when building TS projects: ${code}`)))),
  32. ])