-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.js
More file actions
42 lines (33 loc) · 1.2 KB
/
loader.js
File metadata and controls
42 lines (33 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { registerHooks } from 'node:module'
import { existsSync } from 'node:fs'
import { spawnSync } from 'node:child_process'
import { resolve } from 'node:path'
const tscPath = resolve(import.meta.dirname, 'node_modules', '@typescript', 'native-preview', 'bin', 'tsgo.js')
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 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']
})
if (tscStatus !== 0) {
const output = nextLoad(url, context)
output.source = Buffer.from('process.exit(1)')
return output
}
firstScript = false
}
return nextLoad(url, context)
},
})