@alexsch01/tsnode
Version:
run ts files in node with type-checking
47 lines (37 loc) • 1.49 kB
JavaScript
import { registerHooks } from 'node:module'
import { existsSync } from 'node:fs'
import { spawnSync } from 'node:child_process'
import { resolve } from 'node:path'
let tscPath = resolve(import.meta.dirname, 'node_modules', '@typescript', 'native-preview', 'bin', 'tsgo.js') // global install
if (!existsSync(tscPath)) {
tscPath = resolve(process.cwd(), 'node_modules', '@typescript', 'native-preview', 'bin', 'tsgo.js') // local install
}
const myArgs = process.argv
const packageJSON = resolve(process.cwd(), 'package.json')
const tsconfigJSON = resolve(process.cwd(), 'tsconfig.json')
if ( myArgs.find(a => a.endsWith('.ts')) === undefined ) {
console.error("Error: must provide a ts file")
process.exit(1)
}
if (!existsSync(packageJSON) || !existsSync(tsconfigJSON)) {
console.error("Error: run npx tsnode --init")
process.exit(1)
}
let firstScript = true
registerHooks({
load(url, context, nextLoad) {
if (firstScript) {
const { status: tscStatus } = spawnSync('node', [tscPath], {
stdio: ['inherit', process.stderr, 'inherit'],
env: { NODE_OPTIONS: '' },
})
if (tscStatus !== 0) {
const output = nextLoad(url, context)
output.source = Buffer.from('process.exit(1)')
return output
}
firstScript = false
}
return nextLoad(url, context)
},
})