forked from sagifogel/typescript-closure-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtscc
188 lines (163 loc) · 6.82 KB
/
tscc
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
#!/usr/bin/env node
var path = require("path");
var ts = require(__dirname + "/typescript.js");
var pkg = require(__dirname + "/package.json");
function reportDiagnostic(diagnostic) {
var output = "";
if (diagnostic.file) {
var loc = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
output += diagnostic.file.fileName + "(" + (loc.line + 1) + "," + (loc.character + 1) + "): ";
}
var category = ts.DiagnosticCategory[diagnostic.category].toLowerCase();
output += category.toLowerCase() + " TS" + diagnostic.code + ": " + flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine) + ts.sys.newLine;
ts.sys.write(output);
}
function flattenDiagnosticMessageText(messageText, newLine) {
if (typeof messageText === "string") {
return messageText;
}
else {
var diagnosticChain = messageText;
var result = "";
var indent = 0;
while (diagnosticChain) {
if (indent) {
result += newLine;
for (var i = 0; i < indent; i++) {
result += " ";
}
}
result += diagnosticChain.messageText;
indent++;
diagnosticChain = diagnosticChain.next;
}
return result;
}
}
function reportDiagnostics(diagnostics) {
for (var i = 0; i < diagnostics.length; i++) {
reportDiagnostic(diagnostics[i]);
}
}
function isJSONSupported() {
return typeof JSON === "object" && typeof JSON.parse === "function";
}
function printVersion() {
ts.sys.write("Version " + pkg.version + ts.sys.newLine);
}
function sortFiles(fileNames) {
return fileNames.filter(function (file) { return path.extname(file) === '.ts'; })
.sort(function (a, b) {
if (a === b) { return 0; }
if (a.indexOf("d.ts") > -1) { return -1; }
return 1;
});
}
function executeCommandLine(args) {
var configFileName;
var commandLine = ts.parseCommandLine(args);
if (commandLine.errors.length > 0) {
reportDiagnostics(commandLine.errors);
return ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
}
if (commandLine.options.version) {
printVersion();
return ts.sys.exit(ts.ExitStatus.Success);
}
if (commandLine.options.project) {
if (!isJSONSupported()) {
reportDiagnostic(ts.createCompilerDiagnostic(ts.Diagnostics.The_current_host_does_not_support_the_0_option, "--project"));
return ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
}
configFileName = ts.normalizePath(ts.combinePaths(commandLine.options.project, "tsconfig.json"));
if (commandLine.fileNames.length !== 0) {
reportDiagnostic(ts.createCompilerDiagnostic(ts.Diagnostics.Option_project_cannot_be_mixed_with_source_files_on_a_command_line));
return ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
}
}
else if (commandLine.fileNames.length === 0 && isJSONSupported()) {
var searchPath = ts.normalizePath(ts.sys.getCurrentDirectory());
configFileName = ts.findConfigFile(searchPath);
}
if (commandLine.fileNames.length === 0 && !configFileName) {
printVersion();
return ts.sys.exit(ts.ExitStatus.Success);
}
performCompilation();
function parseConfigFile() {
try {
var cachedConfigFileText = ts.sys.readFile(configFileName);
}
catch (e) {
var error = ts.createCompilerDiagnostic(ts.Diagnostics.Cannot_read_file_0_Colon_1, configFileName, e.message);
ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
return;
}
var result = ts.parseConfigFileTextToJson(configFileName, cachedConfigFileText);
if (result.error) {
reportDiagnostics([result.error]);
return ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
}
var configObject = result.config;
var configParseResult = ts.parseJsonConfigFileContent(configObject, ts.sys, ts.getDirectoryPath(configFileName));
if (configParseResult.errors.length > 0) {
reportDiagnostics(configParseResult.errors);
return ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
}
return configParseResult;
}
function emitErrorsAndExit(errors) {
reportDiagnostics(configParseResult.errors);
return ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
}
function normalizeFilePath(file) {
return ts.normalizePath(ts.combinePaths(commandLine.options.rootDir, file));
}
function performCompilation() {
var now,
program,
exitCode,
emitResult,
rootFileNames,
fileNames = [],
configParseResult,
externFileNames = [];
if (configFileName) {
configParseResult = parseConfigFile();
rootFileNames = configParseResult.fileNames;
externFileNames = configParseResult.externFileNames;
compilerOptions = ts.extend(commandLine.options, configParseResult.options);
}
else {
rootFileNames = commandLine.fileNames;
compilerOptions = commandLine.options;
externFileNames = commandLine.externFileNames;
}
compilerOptions = ts.extend({
emitInterfaces : true,
emitAnnotations: true,
target: ts.ScriptTarget.ES5,
externs: externFileNames || []
}, compilerOptions);
if (!configFileName && commandLine.options.rootDir) {
rootFileNames = rootFileNames.map(normalizeFilePath);
compilerOptions.externs = externFileNames = commandLine.externFileNames.map(normalizeFilePath);
if (compilerOptions.entry) {
compilerOptions.entry = ts.normalizePath(ts.combinePaths(commandLine.options.rootDir, compilerOptions.entry))
}
if (compilerOptions.externsOutFile) {
compilerOptions.externsOutFile = ts.normalizePath(ts.combinePaths(commandLine.options.rootDir, compilerOptions.externsOutFile))
}
}
rootFileNames = rootFileNames.concat(externFileNames)
fileNames = sortFiles(rootFileNames);
program = ts.createProgram(fileNames, compilerOptions);
now = Date.now();
emitResult = program.emit();
ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics).forEach(reportDiagnostic);
exitCode = emitResult.emitSkipped ? 1 : 0;
console.log("Process exiting with code '" + exitCode + "' and lasted " + (Date.now() - now) + " milliseconds.");
process.exit(exitCode);
}
}
executeCommandLine(ts.sys.args);