|
1 | | -import { spawnSync, execSync } from 'child_process' |
2 | | -import { name } from '../../package.json' |
| 1 | +import { transpile } from 'typescript' |
3 | 2 | import Compiler from './Compiler' |
4 | | -import fs from 'fs' |
5 | | -import TypeScriptRunner from '../Runners/TypeScriptRunner' |
6 | | -import os from 'os' |
7 | | -import path from 'path' |
8 | 3 |
|
9 | 4 | export default class TypeScriptCompiler < Compiler |
10 | 5 |
|
11 | 6 | def get |
12 | | - fs.writeFileSync(path.join(self.directory, "{self.sessionId}.ts"), "{self.code.trim!}") |
13 | | - |
14 | | - let failed = false |
15 | | - |
16 | | - # try to compile ts code. |
17 | | - try |
18 | | - const res = spawnSync(TypeScriptRunner.instance!, [path.join(self.directory, "{self.sessionId}.ts"), '-t', 'esnext', '--moduleResolution', 'node', '--noResolve', '--module', 'commonjs', '--skipLibCheck'], { |
19 | | - stdio: 'pipe' |
20 | | - }) |
21 | | - |
22 | | - let line = res.output.toString() |
23 | | - |
24 | | - if !line.includes("error TS2451: Cannot redeclare block-scoped variable") |
25 | | - if line.includes("{self.sessionId}.ts") && line.includes("): error TS") && !line.includes('TS2304: Cannot find name') |
26 | | - failed = true |
27 | | - |
28 | | - line = line.split('): error ')[1] |
29 | | - |
30 | | - if line.trim! !== ',,' && !line.includes('): error TS') && !line.includes("{self.sessionId}.ts") |
31 | | - process.stderr.write(line) |
32 | | - |
33 | | - # read from compiled version. |
34 | | - const results\Buffer = fs.readFileSync(path.join(self.directory, self.sessionId + '.js')) |
35 | | - |
36 | | - # remove compiled ts file. |
37 | | - fs.rmSync(path.join(self.directory, "{self.sessionId}.js")) |
38 | | - |
39 | | - # remove ts file (source of truth). |
40 | | - fs.rmSync(path.join(self.directory, "{self.sessionId}.ts")) |
41 | | - |
42 | | - # return results. |
43 | | - if !failed |
44 | | - results.toString! |
| 7 | + transpile(self.code.trim!, { |
| 8 | + module: 'CommonJS', |
| 9 | + target: 'ESNext', |
| 10 | + moduleResolution: 'node', |
| 11 | + esModuleInterop: true, |
| 12 | + }) |
0 commit comments