run-oxlint.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { spawnSync } from 'node:child_process'
  2. import { resolve } from 'node:path'
  3. import { fileURLToPath } from 'node:url'
  4. const oxlintCli = fileURLToPath(new URL('../node_modules/oxlint/bin/oxlint', import.meta.url))
  5. /** Complete Oxlint child-process arguments and environment. */
  6. export interface OxlintInvocation {
  7. readonly args: readonly string[]
  8. readonly env: NodeJS.ProcessEnv
  9. }
  10. /**
  11. * Apply the repository worker bound to both Oxlint backends.
  12. * @param args - Oxlint CLI arguments requested by the caller.
  13. * @param env - Environment inherited by the Oxlint process.
  14. * @returns the complete CLI arguments and child environment.
  15. */
  16. export function resolveOxlintInvocation(args: readonly string[], env: NodeJS.ProcessEnv): OxlintInvocation {
  17. const raw = env.DSH_OXLINT_THREADS
  18. if (raw === undefined || raw === '') return { args: [...args], env: { ...env } }
  19. const parsed = Number.parseInt(raw, 10)
  20. if (!Number.isSafeInteger(parsed) || parsed < 1 || String(parsed) !== raw) {
  21. throw new Error(`run-oxlint: DSH_OXLINT_THREADS must be a positive integer, got ${JSON.stringify(raw)}.`)
  22. }
  23. if (args.some(arg => arg === '--threads' || arg.startsWith('--threads='))) {
  24. throw new Error('run-oxlint: use DSH_OXLINT_THREADS instead of passing --threads directly.')
  25. }
  26. return {
  27. args: [...args, `--threads=${raw}`],
  28. env: { ...env, GOMAXPROCS: raw },
  29. }
  30. }
  31. function main(): void {
  32. const invocation = resolveOxlintInvocation(process.argv.slice(2), process.env)
  33. const result = spawnSync(process.execPath, [oxlintCli, ...invocation.args], {
  34. env: invocation.env,
  35. stdio: 'inherit',
  36. })
  37. if (result.error !== undefined) throw result.error
  38. process.exitCode = result.status ?? 1
  39. }
  40. const entrypoint = process.argv[1]
  41. if (entrypoint !== undefined && resolve(entrypoint) === fileURLToPath(import.meta.url)) main()