forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineParser.ts
More file actions
354 lines (304 loc) · 12.3 KB
/
CommandLineParser.ts
File metadata and controls
354 lines (304 loc) · 12.3 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import * as fs from "fs";
import * as path from "path";
import * as ts from "typescript";
import {CompilerOptions, LuaTarget, LuaLibImportKind} from "./CompilerOptions";
export type CLIParseResult = ParseResult<ParsedCommandLine>;
type ParseResult<T> =
{ isValid: true; result: T }
| { isValid: false, errorMessage: string};
interface ParsedCommandLine extends ts.ParsedCommandLine {
options: CompilerOptions;
}
interface BaseCLIOption {
aliases: string[];
describe: string;
type: string;
}
interface CLIOption<T> extends BaseCLIOption {
choices: T[];
default: T;
}
const optionDeclarations: {[key: string]: CLIOption<any>} = {
luaLibImport: {
choices: [LuaLibImportKind.Inline, LuaLibImportKind.Require, LuaLibImportKind.Always, LuaLibImportKind.None],
default: LuaLibImportKind.Inline,
describe: "Specifies how js standard features missing in lua are imported.",
type: "enum",
} as CLIOption<string>,
luaTarget: {
aliases: ["lt"],
choices: [LuaTarget.LuaJIT, LuaTarget.Lua53, LuaTarget.Lua52, LuaTarget.Lua51],
default: LuaTarget.LuaJIT,
describe: "Specify Lua target version.",
type: "enum",
} as CLIOption<string>,
noHeader: {
default: false,
describe: "Specify if a header will be added to compiled files.",
type: "boolean",
} as CLIOption<boolean>,
noHoisting: {
default: false,
describe: "Disables hoisting.",
type: "boolean",
} as CLIOption<boolean>,
};
export const { version } = require("../package.json");
const helpString =
`Version ${version}\n` +
"Syntax: tstl [options] [files...]\n\n" +
"Examples: tstl path/to/file.ts [...]\n" +
" tstl -p path/to/tsconfig.json\n\n" +
"In addition to the options listed below you can also pass options\n" +
"for the typescript compiler (For a list of options use tsc -h).\n" +
"Some tsc options might have no effect.";
/**
* Parse the supplied arguments.
* The result will include arguments supplied via CLI and arguments from tsconfig.
*/
export function parseCommandLine(args: string[]): CLIParseResult
{
let commandLine = ts.parseCommandLine(args);
// Run diagnostics to check for invalid tsc options
const diagnosticsResult = runTsDiagnostics(commandLine);
if (diagnosticsResult.isValid === false) {
return diagnosticsResult;
}
// This will add TS and TSTL options from a tsconfig
const configResult = readTsConfig(commandLine);
if (configResult.isValid === true) {
commandLine = configResult.result;
} else {
return { isValid: false, errorMessage: configResult.errorMessage };
}
// Run diagnostics to check for invalid tsconfig
const diagnosticsResult2 = runTsDiagnostics(commandLine);
if (diagnosticsResult2.isValid === false) {
return diagnosticsResult2;
}
// Merge TSTL CLI options in (highest priority) will also set defaults if none specified
const tstlCLIResult = parseTSTLOptions(commandLine, args);
if (tstlCLIResult.isValid === true) {
commandLine = tstlCLIResult.result;
} else {
return { isValid: false, errorMessage: tstlCLIResult.errorMessage };
}
if (commandLine.options.project && !commandLine.options.rootDir) {
commandLine.options.rootDir = path.dirname(commandLine.options.project);
}
if (!commandLine.options.rootDir) {
commandLine.options.rootDir = process.cwd();
}
if (!commandLine.options.outDir) {
commandLine.options.outDir = commandLine.options.rootDir;
}
return { isValid: true, result: commandLine as ParsedCommandLine };
}
export function getHelpString(): string {
let result = helpString + "\n\n";
result += "Options:\n";
for (const optionName in optionDeclarations) {
const option = optionDeclarations[optionName];
const aliasStrings = option.aliases
? option.aliases.map(a => "-" + a)
: [];
const optionString = aliasStrings.concat(["--" + optionName]).join("|");
const parameterDescribe = option.choices
? option.choices.join("|")
: option.type;
const spacing = " ".repeat(Math.max(1, 45 - optionString.length - parameterDescribe.length));
result += `\n ${optionString} <${parameterDescribe}>${spacing}${option.describe}\n`;
}
return result;
}
function readTsConfig(parsedCommandLine: ts.ParsedCommandLine): CLIParseResult
{
const options = parsedCommandLine.options;
// Load config
if (options.project) {
const findProjectPathResult = findConfigFile(options);
if (findProjectPathResult.isValid === true) {
options.project = findProjectPathResult.result;
} else {
return { isValid: false, errorMessage: findProjectPathResult.errorMessage };
}
const configPath = options.project;
const configContents = fs.readFileSync(configPath).toString();
const configJson = ts.parseConfigFileTextToJson(configPath, configContents);
const parsedJsonConfig = ts.parseJsonConfigFileContent(
configJson.config,
ts.sys,
path.dirname(configPath),
options
);
for (const key in parsedJsonConfig.raw) {
const option = optionDeclarations[key];
if (option !== undefined) {
const value = readValue(parsedJsonConfig.raw[key], option.type, key);
if (option.choices) {
if (option.choices.indexOf(value) < 0) {
return {
isValid: false,
errorMessage: `Unknown ${key} value '${value}'.\nAccepted values: ${option.choices}`,
};
}
}
parsedJsonConfig.options[key] = value;
}
}
return { isValid: true, result: parsedJsonConfig };
}
return { isValid: true, result: parsedCommandLine };
}
function parseTSTLOptions(commandLine: ts.ParsedCommandLine, args: string[]): CLIParseResult {
const result = {};
for (let i = 0; i < args.length; i++) {
if (args[i].startsWith("--")) {
const argumentName = args[i].substr(2);
const option = optionDeclarations[argumentName];
if (option) {
const argumentResult = getArgumentValue(argumentName, args[i + 1]);
i++; // Skip the value from being considered as argument name
if (argumentResult.isValid === true) {
result[argumentName] = argumentResult.result;
} else {
return { isValid: false, errorMessage: argumentResult.errorMessage };
}
}
} else if (args[i].startsWith("-")) {
const argument = args[i].substr(1);
let argumentName: string;
for (const key in optionDeclarations) {
if (optionDeclarations[key].aliases && optionDeclarations[key].aliases.indexOf(argument) >= 0) {
argumentName = key;
break;
}
}
if (argumentName) {
const argumentResult = getArgumentValue(argumentName, args[i + 1]);
i++; // Skip the value from being considered as argument name
if (argumentResult.isValid === true) {
result[argumentName] = argumentResult.result;
} else {
return { isValid: false, errorMessage: argumentResult.errorMessage };
}
}
}
}
for (const option in result) {
commandLine.options[option] = result[option];
}
// Add defaults if not set
const defaultOptions = getDefaultOptions();
for (const option in defaultOptions) {
if (!commandLine.options[option]) {
commandLine.options[option] = defaultOptions[option];
}
}
return { isValid: true, result: commandLine };
}
function getArgumentValue(argumentName: string, argument: string): ParseResult<string | boolean>
{
if (argument === undefined) {
return { isValid: false, errorMessage: `Missing value for parameter ${argumentName}`};
}
const option = optionDeclarations[argumentName];
const value = readValue(argument, option.type, argumentName);
if (option.choices) {
if (option.choices.indexOf(value) < 0) {
return {
isValid: false,
errorMessage: `Unknown ${argumentName} value '${value}'. Accepted values are: ${option.choices}`,
};
}
}
return { isValid: true, result: value };
}
function readValue(valueString: string, valueType: string, parameterName: string): string | boolean {
if (valueType === "boolean") {
return valueString === "true" || valueString === "t"
? true
: false;
} else if (valueType === "enum") {
return valueString.toLowerCase();
} else {
return valueString;
}
}
function getDefaultOptions(): CompilerOptions {
const options: CompilerOptions = {};
for (const optionName in optionDeclarations) {
if (optionDeclarations[optionName].default !== undefined) {
options[optionName] = optionDeclarations[optionName].default;
}
}
return options;
}
/** Check the current state of the ParsedCommandLine for errors */
function runTsDiagnostics(commandLine: ts.ParsedCommandLine): ParseResult<boolean> {
// Remove files that dont exist
commandLine.fileNames = commandLine.fileNames.filter(file => fs.existsSync(file) || fs.existsSync(file + ".ts"));
const tsInvalidCompilerOptionErrorCode = 5023;
if (commandLine.errors.length !== 0) {
// Generate a list of valid option names and aliases
const optionNames: string[] = [];
for (const key of Object.keys(optionDeclarations)) {
optionNames.push(key);
const alias = optionDeclarations[key].aliases;
if (alias) {
if (typeof alias === "string") {
optionNames.push(alias);
} else {
optionNames.push(...alias);
}
}
}
for (const err of commandLine.errors) {
// Ignore errors caused by tstl specific compiler options
if (err.code === tsInvalidCompilerOptionErrorCode) {
let ignore = false;
for (const optionName of optionNames) {
if (err.messageText.toString().indexOf(optionName) !== -1) {
ignore = true;
break;
}
}
if (!ignore) {
return { isValid: false, errorMessage: `error TS${err.code}: ${err.messageText}`};
}
}
}
}
return { isValid: true, result: true };
}
/** Find configFile, function from ts api seems to be broken? */
export function findConfigFile(options: ts.CompilerOptions): ParseResult<string> {
if (!options.project) {
return { isValid: false, errorMessage: `error no base path provided, could not find config.`};
}
let configPath = options.project;
// If the project path is wrapped in double quotes, remove them
if (/^".*"$/.test(configPath)) {
configPath = configPath.substring(1, configPath.length - 1);
}
/* istanbul ignore if: Testing else part is not really possible via automated tests */
if (!path.isAbsolute(configPath)) {
// TODO check if options.project can even contain non absolute paths
configPath = path.join(process.cwd(), configPath);
}
if (fs.statSync(configPath).isDirectory()) {
configPath = path.join(configPath, "tsconfig.json");
} else if (fs.statSync(configPath).isFile() && path.extname(configPath) === ".ts") {
// Search for tsconfig upwards in directory hierarchy starting from the file path
const dir = path.dirname(configPath).split(path.sep);
for (let i = dir.length; i > 0; i--) {
const searchPath = dir.slice(0, i).join("/") + path.sep + "tsconfig.json";
// If tsconfig.json was found, stop searching
if (ts.sys.fileExists(searchPath)) {
configPath = searchPath;
break;
}
}
}
return { isValid: true, result: configPath };
}