-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathproxy.ts
More file actions
68 lines (56 loc) · 1.62 KB
/
Copy pathproxy.ts
File metadata and controls
68 lines (56 loc) · 1.62 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
// tsproxy.ts
import { writeFile, unlink } from 'fs/promises';
import { randomUUID } from 'crypto';
import { pathToFileURL } from 'url';
import path from 'path';
(async () => {
const chunks: Buffer[] = [];
for await (const chunk of process.stdin) {
chunks.push(chunk);
}
const code = Buffer.concat(chunks).toString();
const tmpFileName = `.tmp-tsproxy-${randomUUID()}.ts`;
const tmpFile = path.join(process.cwd(), tmpFileName);
const origLog = console.log;
let capturedLogs: any[] = [];
console.log = (...args: any[]) => {
capturedLogs.push(args);
}
process.env.HEAVY_DEBUG && console.log(`🪲 TMP proxy file: ${tmpFile}`);
process.env.HEAVY_DEBUG && console.log(`🪲 Current working directory: ${process.cwd()}`);
try {
// Save code to a temp file
await writeFile(tmpFile, code);
// Dynamically import the file
const module = await import(pathToFileURL(tmpFile).href);
if (typeof module.exec !== 'function') {
throw new Error("Module does not export an 'exec' function");
}
const result = await Promise.resolve(module.exec());
// Restore original console.log
console.log = origLog;
console.log(
">>>>>>>"+
JSON.stringify({
result,
capturedLogs,
error: null
})
+"<<<<<<<"
);
} catch (error: any) {
// Restore original console.log
console.log = origLog;
console.log(
">>>>>>>"+
JSON.stringify({
error: error.message,
stack: error.stack,
capturedLogs
})
+"<<<<<<<"
);
} finally {
await unlink(tmpFile).catch(() => {});
}
})();