Skip to content

Commit

Permalink
fix: cleanup temp file if process is terminated beforehand
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavopch committed Jul 4, 2023
1 parent 56756dc commit b4cdeb5
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ const tmpTsconfig = {
}
fs.writeFileSync(tmpTsconfigPath, JSON.stringify(tmpTsconfig, null, 2))

// Attach cleanup handlers
let didCleanup = false
for (const eventName of ['exit', 'SIGHUP', 'SIGINT', 'SIGTERM']) {
process.on(eventName, exitCode => {
if (didCleanup) return
didCleanup = true

fs.unlinkSync(tmpTsconfigPath)

if (eventName !== 'exit') {
process.exit(exitCode)
}
})
}

// Type-check our files
const { status } = spawnSync(
resolveFromModule(
Expand All @@ -67,7 +82,4 @@ const { status } = spawnSync(
{ stdio: 'inherit' },
)

// Delete temp config file
fs.unlinkSync(tmpTsconfigPath)

process.exit(status)

0 comments on commit b4cdeb5

Please sign in to comment.