-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.ts
More file actions
executable file
·48 lines (44 loc) · 1.25 KB
/
Copy pathindex.ts
File metadata and controls
executable file
·48 lines (44 loc) · 1.25 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
#!/usr/bin/env node
import ts from "typescript";
import path from "path";
import { program } from "@commander-js/extra-typings";
import { typeScriptToPython } from "./typeScriptToPython";
import { Ts2PyConfig } from "./config";
const compile = (
fileNames: string[],
config: Ts2PyConfig & { strict?: boolean },
) => {
const program = ts.createProgram(fileNames, {
noEmit: true,
allowJs: true,
resolveJsonModule: true,
skipLibCheck: true,
strict: config.strict,
});
const relevantSourceFiles = program
.getSourceFiles()
.filter((f) =>
fileNames
.map((fn) => path.relative(fn, f.fileName) === "")
.reduce((a, b) => a || b),
);
const transpiled = typeScriptToPython(
program.getTypeChecker(),
relevantSourceFiles,
config,
);
console.log(transpiled);
};
program
.name("typescript2python")
.description("A program that converts TypeScript type definitions to Python")
.option(
"--nullable-optionals",
"if set, optional entries in dictionaries will be nullable, e.g. `NotRequired[Optional[T]]`",
)
.option("--strict", "Enable all strict type-checking options.")
.arguments("<input...>")
.action((args, options) => {
compile(args, options);
})
.parse(process.argv);