-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunTscWithNode.ts
35 lines (26 loc) · 1.16 KB
/
runTscWithNode.ts
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
import * as child_process from 'node:child_process';
import { getTscCommandComponents } from './getTscCommandComponents.ts';
import type { TscExecutionResultBare } from './TscExecutionResult.ts';
/**
NOTE: This does NOT work in Node or Bun if you are inside a project that isn't using ESM. Like if you have some crusty old monorepo with some Angular apps in it and a bunch of CommonJS dependencies.
For that scenario, use `runTscWithNodeSubprocessThatRunsDeno()`.
Otherwise, this is the Node flavor of `runTsc`. This should also work when running on Bun.
*/
export const runTscWithNode = (file: string): TscExecutionResultBare =>
{
const start = performance.now();
let tscExitCode = -1;
const args = getTscCommandComponents(file, false);
const { status, stdout: rawStdout, stderr: rawStderr } = child_process.spawnSync('npx', args);
tscExitCode = status ?? -1;
const stdout = new TextDecoder().decode(rawStdout);
const stderr = new TextDecoder().decode(rawStderr);
const result: TscExecutionResultBare = {
tscExitCode,
elapsedMs: performance.now() - start,
stdout,
stderr,
tscCommand: 'npx ' + args.join(' '),
};
return result;
};