forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.ts
More file actions
191 lines (161 loc) · 7.15 KB
/
execute.ts
File metadata and controls
191 lines (161 loc) · 7.15 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
import * as ts from "typescript";
import * as tstl from "..";
import * as cliDiagnostics from "./diagnostics";
import { getHelpString, versionString } from "./information";
import { parseCommandLine } from "./parse";
import { createDiagnosticReporter } from "./report";
import { createConfigFileUpdater, locateConfigFile, parseConfigFileWithSystem } from "./tsconfig";
const shouldBePretty = ({ pretty }: ts.CompilerOptions = {}) =>
pretty !== undefined ? (pretty as boolean) : ts.sys.writeOutputIsTTY?.() ?? false;
let reportDiagnostic = createDiagnosticReporter(false);
function updateReportDiagnostic(options?: ts.CompilerOptions): void {
reportDiagnostic = createDiagnosticReporter(shouldBePretty(options));
}
function createWatchStatusReporter(options?: ts.CompilerOptions): ts.WatchStatusReporter {
return ts.createWatchStatusReporter(ts.sys, shouldBePretty(options));
}
export 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 = parseCommandLine(args);
if (commandLine.options.build) {
reportDiagnostic(cliDiagnostics.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(versionString);
return ts.sys.exit(ts.ExitStatus.Success);
}
if (commandLine.options.help) {
console.log(versionString);
console.log(getHelpString());
return ts.sys.exit(ts.ExitStatus.Success);
}
const configFileName = locateConfigFile(commandLine);
if (typeof configFileName === "object") {
reportDiagnostic(configFileName);
return ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
}
const commandLineOptions = commandLine.options;
if (configFileName) {
const configParseResult = 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 { diagnostics: emitDiagnostics, emitSkipped } = new tstl.Compiler().emit({ program });
const diagnostics = ts.sortAndDeduplicateDiagnostics([...ts.getPreEmitDiagnostics(program), ...emitDiagnostics]);
diagnostics.forEach(reportDiagnostic);
const exitCode =
diagnostics.length === 0
? ts.ExitStatus.Success
: emitSkipped
? 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 hadErrorLastTime = true;
const updateConfigFile = createConfigFileUpdater(optionsToExtend);
const compiler = new tstl.Compiler();
host.afterProgramCreate = builderProgram => {
const program = builderProgram.getProgram();
const options: tstl.CompilerOptions = builderProgram.getCompilerOptions();
const configFileParsingDiagnostics: ts.Diagnostic[] = updateConfigFile(options);
let sourceFiles: ts.SourceFile[] | undefined;
if (!tstl.isBundleEnabled(options) && !hadErrorLastTime) {
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 } = compiler.emit({ program, sourceFiles });
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);
hadErrorLastTime = errors.length > 0;
host.onWatchStatusChange!(cliDiagnostics.watchErrorSummary(errors.length), host.getNewLine(), options);
};
}