1- export { transpileFiles , transpileProject , transpileString , TranspileStringResult } from "./API" ;
1+ import * as fs from "fs" ;
2+ import * as path from "path" ;
3+ import * as ts from "typescript" ;
4+ import { parseConfigFileContent } from "./CommandLineParser" ;
5+ import { CompilerOptions } from "./CompilerOptions" ;
6+ import { getTranspileOutput , TranspilationResult , TranspiledFile } from "./Transpile" ;
7+
28export { parseConfigFileContent } from "./CommandLineParser" ;
39export { CompilerOptions , LuaLibImportKind , LuaTarget } from "./CompilerOptions" ;
410export * from "./Emit" ;
@@ -7,3 +13,107 @@ export { LuaLibFeature } from "./LuaLib";
713export { LuaPrinter } from "./LuaPrinter" ;
814export { LuaTransformer } from "./LuaTransformer" ;
915export * from "./Transpile" ;
16+
17+ export function transpileFiles (
18+ rootNames : string [ ] ,
19+ options : CompilerOptions = { }
20+ ) : TranspilationResult {
21+ const program = ts . createProgram ( rootNames , options ) ;
22+ const { diagnostics, transpiledFiles } = getTranspileOutput ( { program, options } ) ;
23+
24+ const allDiagnostics = ts . sortAndDeduplicateDiagnostics ( [
25+ ...ts . getPreEmitDiagnostics ( program ) ,
26+ ...diagnostics ,
27+ ] ) ;
28+
29+ return { transpiledFiles, diagnostics : [ ...allDiagnostics ] } ;
30+ }
31+
32+ export function transpileProject ( fileName : string , options ?: CompilerOptions ) : TranspilationResult {
33+ const parseResult = parseConfigFileContent (
34+ fs . readFileSync ( fileName , "utf8" ) ,
35+ fileName ,
36+ options
37+ ) ;
38+ if ( parseResult . isValid === false ) {
39+ // TODO: Return diagnostics
40+ throw new Error ( parseResult . errorMessage ) ;
41+ }
42+
43+ return transpileFiles ( parseResult . result . fileNames , parseResult . result . options ) ;
44+ }
45+
46+ const libCache : { [ key : string ] : ts . SourceFile } = { } ;
47+
48+ /** @internal */
49+ export function createVirtualProgram (
50+ input : Record < string , string > ,
51+ options ?: CompilerOptions
52+ ) : ts . Program {
53+ const compilerHost : ts . CompilerHost = {
54+ fileExists : ( ) => true ,
55+ getCanonicalFileName : fileName => fileName ,
56+ getCurrentDirectory : ( ) => "" ,
57+ getDefaultLibFileName : ts . getDefaultLibFileName ,
58+ readFile : ( ) => "" ,
59+ getNewLine : ( ) => "\n" ,
60+ useCaseSensitiveFileNames : ( ) => false ,
61+ writeFile : ( ) => { } ,
62+
63+ getSourceFile : filename => {
64+ if ( filename in input ) {
65+ return ts . createSourceFile (
66+ filename ,
67+ input [ filename ] ,
68+ ts . ScriptTarget . Latest ,
69+ false
70+ ) ;
71+ }
72+
73+ if ( filename . startsWith ( "lib." ) ) {
74+ if ( libCache [ filename ] ) return libCache [ filename ] ;
75+ const typeScriptDir = path . dirname ( require . resolve ( "typescript" ) ) ;
76+ const filePath = path . join ( typeScriptDir , filename ) ;
77+ const content = fs . readFileSync ( filePath , "utf8" ) ;
78+
79+ libCache [ filename ] = ts . createSourceFile (
80+ filename ,
81+ content ,
82+ ts . ScriptTarget . Latest ,
83+ false
84+ ) ;
85+
86+ return libCache [ filename ] ;
87+ }
88+ } ,
89+ } ;
90+
91+ return ts . createProgram ( Object . keys ( input ) , options , compilerHost ) ;
92+ }
93+
94+ export interface TranspileStringResult {
95+ file : TranspiledFile ;
96+ diagnostics : ts . Diagnostic [ ] ;
97+ }
98+
99+ export function transpileString (
100+ input : string | Record < string , string > ,
101+ options : CompilerOptions = { }
102+ ) : TranspileStringResult {
103+ const programFiles = typeof input === "object" ? input : { "main.ts" : input } ;
104+ const mainFileName =
105+ typeof input === "string"
106+ ? "main.ts"
107+ : Object . keys ( input ) . find ( x => / \b m a i n \. [ a - z ] + $ / . test ( x ) ) ;
108+ if ( mainFileName === undefined ) throw new Error ( 'Input should have a file named "main"' ) ;
109+
110+ const program = createVirtualProgram ( programFiles , options ) ;
111+ const { diagnostics, transpiledFiles } = getTranspileOutput ( { program, options } ) ;
112+
113+ const allDiagnostics = ts . sortAndDeduplicateDiagnostics ( [
114+ ...ts . getPreEmitDiagnostics ( program ) ,
115+ ...diagnostics ,
116+ ] ) ;
117+
118+ return { file : transpiledFiles . get ( mainFileName ) , diagnostics : [ ...allDiagnostics ] } ;
119+ }
0 commit comments