-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
tsx.ts
112 lines (106 loc) · 1.82 KB
/
tsx.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import path from 'path';
import { fileURLToPath } from 'url';
import { execaNode } from 'execa';
import getNode from 'get-node';
type Options = {
args: string[];
nodePath?: string;
cwd?: string;
};
const __dirname = fileURLToPath(import.meta.url);
export const tsxPath = path.join(__dirname, '../../../dist/cli.js');
export const tsx = (
options: Options,
) => execaNode(
tsxPath,
options.args,
{
env: {
ESBK_DISABLE_CACHE: '1',
},
nodePath: options.nodePath,
nodeOptions: [],
cwd: options.cwd,
reject: false,
},
);
export async function createNode(
nodeVersion: string,
fixturePath: string,
) {
const node = await getNode(nodeVersion);
return {
version: node.version,
packageType: '',
get isCJS() {
return this.packageType === 'commonjs';
},
tsx(
options: Options,
) {
return tsx({
...options,
nodePath: node.path,
});
},
load(
filePath: string,
options?: {
cwd?: string;
args?: string[];
},
) {
return this.tsx(
{
args: [
...(options?.args ?? []),
filePath,
],
cwd: path.join(fixturePath, options?.cwd ?? ''),
},
);
},
import(
filePath: string,
options?: {
typescript?: boolean;
},
) {
return this.tsx({
args: [
`./import-file${options?.typescript ? '.ts' : '.js'}`,
filePath,
],
cwd: fixturePath,
});
},
require(
filePath: string,
options?: {
typescript?: boolean;
},
) {
return this.tsx({
args: [
`./require-file${options?.typescript ? '.cts' : '.cjs'}`,
filePath,
],
cwd: fixturePath,
});
},
requireFlag(
filePath: string,
) {
return this.tsx({
args: [
'--eval',
'null',
'--require',
filePath,
],
cwd: fixturePath,
});
},
};
}
export type NodeApis = Awaited<ReturnType<typeof createNode>>;