TypeScript 7's CommonJS export no longer populates ts.sys, which ts-node's config loader depends on, breaking `npm run worker` with a TypeError. tsx (esbuild-based) doesn't rely on that API and natively resolves tsconfig.json path aliases, so tsconfig-paths is also no longer needed. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
37 lines
841 B
TypeScript
37 lines
841 B
TypeScript
import { existsSync, readFileSync, writeFileSync } from 'fs'
|
|
import { spawn } from 'child_process'
|
|
import path from 'path'
|
|
|
|
const PID_FILE = path.join(__dirname, 'worker.pid')
|
|
|
|
function isRunning(pid: number): boolean {
|
|
try {
|
|
process.kill(pid, 0)
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
if (existsSync(PID_FILE)) {
|
|
const pid = parseInt(readFileSync(PID_FILE, 'utf-8').trim(), 10)
|
|
if (!isNaN(pid) && isRunning(pid)) {
|
|
console.log(`[cron-check] worker already running (PID ${pid})`)
|
|
process.exit(0)
|
|
}
|
|
}
|
|
|
|
const child = spawn(
|
|
process.execPath,
|
|
[require.resolve('tsx/cli'), path.join(__dirname, 'index.ts')],
|
|
{ detached: true, stdio: 'ignore' }
|
|
)
|
|
child.unref()
|
|
|
|
if (child.pid) {
|
|
writeFileSync(PID_FILE, String(child.pid))
|
|
console.log(`[cron-check] worker started (PID ${child.pid})`)
|
|
}
|
|
|
|
process.exit(0)
|