forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtstl.ts
More file actions
264 lines (222 loc) · 9.87 KB
/
tstl.ts
File metadata and controls
264 lines (222 loc) · 9.87 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env node
import * as path from "path";
import * as ts from "typescript";
import * as tstl from ".";
import * as CommandLineParser from "./CommandLineParser";
import * as diagnosticFactories from "./diagnostics";
function createWatchStatusReporter(options?: ts.CompilerOptions): ts.WatchStatusReporter {
return (ts as any).createWatchStatusReporter(ts.sys, shouldBePretty(options));
}
function shouldBePretty(options?: ts.CompilerOptions): boolean {
return !options || options.pretty === undefined
? ts.sys.writeOutputIsTTY !== undefined && ts.sys.writeOutputIsTTY()
: Boolean(options.pretty);
}
let reportDiagnostic = tstl.createDiagnosticReporter(false);
function updateReportDiagnostic(options?: ts.CompilerOptions): void {
reportDiagnostic = tstl.createDiagnosticReporter(shouldBePretty(options));
}
function locateConfigFile(commandLine: tstl.ParsedCommandLine): string | undefined {
const { project } = commandLine.options;
if (!project) {
if (commandLine.fileNames.length === 0) {
const searchPath = path.posix.normalize(ts.sys.getCurrentDirectory());
return ts.findConfigFile(searchPath, ts.sys.fileExists);
}
return;
}
if (commandLine.fileNames.length !== 0) {
reportDiagnostic(diagnosticFactories.optionProjectCannotBeMixedWithSourceFilesOnACommandLine());
ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
return;
}
let fileOrDirectory = path.posix.normalize(project);
if (!path.isAbsolute(fileOrDirectory)) {
fileOrDirectory = path.posix.join(ts.sys.getCurrentDirectory(), fileOrDirectory);
}
if (!fileOrDirectory || ts.sys.directoryExists(fileOrDirectory)) {
const configFileName = path.posix.join(fileOrDirectory, "tsconfig.json");
if (ts.sys.fileExists(configFileName)) {
return configFileName;
} else {
reportDiagnostic(diagnosticFactories.cannotFindATsconfigJsonAtTheSpecifiedDirectory(project));
ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
}
} else {
if (ts.sys.fileExists(fileOrDirectory)) {
return fileOrDirectory;
} else {
reportDiagnostic(diagnosticFactories.theSpecifiedPathDoesNotExist(project));
ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
}
}
}
function executeCommandLine(args: string[]): void {
if (args.length > 0 && args[0].startsWith("-")) {
const firstOption = args[0].slice(args[0].startsWith("--") ? 2 : 1).toLowerCase();
if (firstOption === "build" || firstOption === "b") {
return performBuild(args.slice(1));
}
}
const commandLine = CommandLineParser.parseCommandLine(args);
if (commandLine.options.build) {
reportDiagnostic(diagnosticFactories.optionBuildMustBeFirstCommandLineArgument());
return ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
}
// TODO: ParsedCommandLine.errors isn't meant to contain warnings. Once root-level options
// support would be dropped it should be changed to `commandLine.errors.length > 0`.
if (commandLine.errors.some(e => e.category === ts.DiagnosticCategory.Error)) {
commandLine.errors.forEach(reportDiagnostic);
return ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
}
if (commandLine.options.version) {
console.log(CommandLineParser.version);
return ts.sys.exit(ts.ExitStatus.Success);
}
if (commandLine.options.help) {
console.log(CommandLineParser.version);
console.log(CommandLineParser.getHelpString());
return ts.sys.exit(ts.ExitStatus.Success);
}
const configFileName = locateConfigFile(commandLine);
const commandLineOptions = commandLine.options;
if (configFileName) {
const configParseResult = CommandLineParser.parseConfigFileWithSystem(configFileName, commandLineOptions);
updateReportDiagnostic(configParseResult.options);
if (configParseResult.options.watch) {
createWatchOfConfigFile(configFileName, commandLineOptions);
} else {
performCompilation(
configParseResult.fileNames,
configParseResult.projectReferences,
configParseResult.options,
ts.getConfigFileParsingDiagnostics(configParseResult)
);
}
} else {
updateReportDiagnostic(commandLineOptions);
if (commandLineOptions.watch) {
createWatchOfFilesAndCompilerOptions(commandLine.fileNames, commandLineOptions);
} else {
performCompilation(commandLine.fileNames, commandLine.projectReferences, commandLineOptions);
}
}
}
function performBuild(_args: string[]): void {
console.log("Option '--build' is not supported.");
return ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
}
function performCompilation(
rootNames: string[],
projectReferences: readonly ts.ProjectReference[] | undefined,
options: tstl.CompilerOptions,
configFileParsingDiagnostics?: readonly ts.Diagnostic[]
): void {
const program = ts.createProgram({
rootNames,
options,
projectReferences,
configFileParsingDiagnostics,
});
const { transpiledFiles, diagnostics: transpileDiagnostics } = tstl.transpile({ program });
const diagnostics = ts.sortAndDeduplicateDiagnostics([
...ts.getPreEmitDiagnostics(program),
...transpileDiagnostics,
]);
const emitResult = tstl.emitTranspiledFiles(options, transpiledFiles);
emitResult.forEach(({ name, text }) => ts.sys.writeFile(name, text));
diagnostics.forEach(reportDiagnostic);
const exitCode =
diagnostics.length === 0
? ts.ExitStatus.Success
: transpiledFiles.length === 0
? ts.ExitStatus.DiagnosticsPresent_OutputsSkipped
: ts.ExitStatus.DiagnosticsPresent_OutputsGenerated;
return ts.sys.exit(exitCode);
}
function createWatchOfConfigFile(configFileName: string, optionsToExtend: tstl.CompilerOptions): void {
const watchCompilerHost = ts.createWatchCompilerHost(
configFileName,
optionsToExtend,
ts.sys,
ts.createSemanticDiagnosticsBuilderProgram,
undefined,
createWatchStatusReporter(optionsToExtend)
);
updateWatchCompilationHost(watchCompilerHost, optionsToExtend);
ts.createWatchProgram(watchCompilerHost);
}
function createWatchOfFilesAndCompilerOptions(rootFiles: string[], options: tstl.CompilerOptions): void {
const watchCompilerHost = ts.createWatchCompilerHost(
rootFiles,
options,
ts.sys,
ts.createSemanticDiagnosticsBuilderProgram,
undefined,
createWatchStatusReporter(options)
);
updateWatchCompilationHost(watchCompilerHost, options);
ts.createWatchProgram(watchCompilerHost);
}
function updateWatchCompilationHost(
host: ts.WatchCompilerHost<ts.SemanticDiagnosticsBuilderProgram>,
optionsToExtend: tstl.CompilerOptions
): void {
let fullRecompile = true;
const configFileMap = new WeakMap<ts.TsConfigSourceFile, ts.ParsedCommandLine>();
host.afterProgramCreate = builderProgram => {
const program = builderProgram.getProgram();
const options = builderProgram.getCompilerOptions() as tstl.CompilerOptions;
let configFileParsingDiagnostics: ts.Diagnostic[] = [];
const configFile = options.configFile as ts.TsConfigSourceFile | undefined;
const configFilePath = options.configFilePath as string | undefined;
if (configFile && configFilePath) {
if (!configFileMap.has(configFile)) {
const parsedConfigFile = CommandLineParser.updateParsedConfigFile(
ts.parseJsonSourceFileConfigFileContent(
configFile,
ts.sys,
path.dirname(configFilePath),
optionsToExtend,
configFilePath
)
);
configFileMap.set(configFile, parsedConfigFile);
}
const parsedConfigFile = configFileMap.get(configFile)!;
Object.assign(options, parsedConfigFile.options);
configFileParsingDiagnostics = parsedConfigFile.errors;
}
let sourceFiles: ts.SourceFile[] | undefined;
if (!fullRecompile) {
sourceFiles = [];
while (true) {
const currentFile = builderProgram.getSemanticDiagnosticsOfNextAffectedFile();
if (!currentFile) break;
if ("fileName" in currentFile.affected) {
sourceFiles.push(currentFile.affected);
} else {
sourceFiles.push(...currentFile.affected.getSourceFiles());
}
}
}
const { diagnostics: emitDiagnostics, transpiledFiles } = tstl.transpile({ program, sourceFiles });
const emitResult = tstl.emitTranspiledFiles(options, transpiledFiles);
emitResult.forEach(({ name, text }) => ts.sys.writeFile(name, text));
const diagnostics = ts.sortAndDeduplicateDiagnostics([
...configFileParsingDiagnostics,
...program.getOptionsDiagnostics(),
...program.getSyntacticDiagnostics(),
...program.getGlobalDiagnostics(),
...program.getSemanticDiagnostics(),
...emitDiagnostics,
]);
diagnostics.forEach(reportDiagnostic);
const errors = diagnostics.filter(d => d.category === ts.DiagnosticCategory.Error);
// do a full recompile after an error
fullRecompile = errors.length > 0;
host.onWatchStatusChange!(diagnosticFactories.watchErrorSummary(errors.length), host.getNewLine(), options);
};
}
if ((ts.sys as any).setBlocking) (ts.sys as any).setBlocking();
executeCommandLine(ts.sys.args);