-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcli.ts
More file actions
88 lines (84 loc) · 2.5 KB
/
cli.ts
File metadata and controls
88 lines (84 loc) · 2.5 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env node
import cac from 'cac'
import { fs } from './fs'
import pathLib, { extname } from 'path'
import os from 'os'
import { logger } from './logger'
import { Is } from './utils'
import { getGlobalTaskManager } from './task-manager'
import chalk from 'chalk'
import { initDefaultCli } from './default-cli'
import { spawn, ChildProcess } from 'child_process'
const { foyFiles, registers, defaultCli } = initDefaultCli()
async function main() {
const pkg = await fs.readJson('./package.json')
const isESM = pkg.type === 'module'
const deps = { ...pkg.dependencies, ...pkg.devDependencies }
const results: ChildProcess[] = [];
for (const foyFile of foyFiles) {
let executor = defaultCli.options.executor
if (!executor) {
executor = 'node'
if (['.ts', '.cts', '.tsx', '.ctsx'].includes(extname(foyFile))) {
if ('tsx' in deps) {
executor = 'tsx'
} else if ('ts-node' in deps) {
executor = isESM ? 'ts-node-esm' : 'ts-node'
} else if ('@swc-node/register' in deps) {
if (isESM) {
registers.push('@swc-node/register/esm-register')
} else {
registers.push('@swc-node/register')
}
} else if ('swc-node' in deps) {
executor = 'swc-node'
} else {
logger.error('no tsx/ts-node/swc-node or @swc-node/register found')
process.exit(1)
}
}
}
const args = [
...registers.map((r) => `--${isESM ? 'import' : 'require'} ${r}`),
foyFile,
...process.argv.slice(2),
]
let NODE_OPTIONS = process.env.NODE_OPTIONS ?? ''
;[
['--inspect',defaultCli.options.inspect],
['--inspectBrk',defaultCli.options.inspectBrk]
].map(([inspect, inspectVal]) => {
if (inspectVal) {
if (typeof inspectVal === 'string') {
inspect += '=' + inspectVal
}
NODE_OPTIONS += ' ' + inspect
}
})
results.push(spawn(executor, args, {
stdio: 'inherit',
shell: process.platform === 'win32',
cwd: process.cwd(),
env:{
...process.env,
NODE_OPTIONS,
}
}));
}
for (const p of results) {
await new Promise<void>((resolve) => p.on('exit', () => resolve()))
if (p.exitCode !== 0 && p.exitCode !== null) {
process.exitCode = p.exitCode;
}
}
// fix zombie process sometimes
process.on('SIGINT', () => {
results.forEach((p) => {
p.kill(9)
})
})
}
main().catch((err) => {
console.error(err)
process.exitCode = 1;
})