TypeScript/src/compiler/commandLineParser.ts

348 lines
13 KiB
TypeScript
Raw Normal View History

2014-07-13 01:04:16 +02:00
/// <reference path="sys.ts"/>
/// <reference path="types.ts"/>
/// <reference path="core.ts"/>
/// <reference path="scanner.ts"/>
module ts {
2014-08-02 02:12:29 +02:00
export var optionDeclarations: CommandLineOption[] = [
{
name: "charset",
type: "string",
},
{
name: "codepage",
type: "number",
},
{
name: "declaration",
shortName: "d",
type: "boolean",
description: Diagnostics.Generates_corresponding_d_ts_file,
},
{
name: "diagnostics",
type: "boolean",
},
{
name: "emitBOM",
type: "boolean"
},
2014-08-02 02:12:29 +02:00
{
name: "help",
shortName: "h",
type: "boolean",
description: Diagnostics.Print_this_message,
},
{
name: "listFiles",
type: "boolean",
},
2014-08-02 02:12:29 +02:00
{
name: "locale",
type: "string",
},
{
name: "mapRoot",
type: "string",
isFilePath: true,
2014-08-02 02:12:29 +02:00
description: Diagnostics.Specifies_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations,
paramType: Diagnostics.LOCATION,
},
{
name: "module",
shortName: "m",
type: {
"commonjs": ModuleKind.CommonJS,
"amd": ModuleKind.AMD
},
description: Diagnostics.Specify_module_code_generation_Colon_commonjs_or_amd,
paramType: Diagnostics.KIND,
error: Diagnostics.Argument_for_module_option_must_be_commonjs_or_amd
},
2014-12-17 05:25:19 +01:00
{
name: "noEmit",
type: "boolean",
description: Diagnostics.Do_not_emit_outputs,
},
2014-10-27 20:48:46 +01:00
{
name: "noEmitOnError",
type: "boolean",
2014-10-28 19:45:18 +01:00
description: Diagnostics.Do_not_emit_outputs_if_any_type_checking_errors_were_reported,
2014-10-27 20:48:46 +01:00
},
2014-08-02 02:12:29 +02:00
{
name: "noImplicitAny",
type: "boolean",
description: Diagnostics.Raise_error_on_expressions_and_declarations_with_an_implied_any_type,
2014-08-02 02:12:29 +02:00
},
{
name: "noLib",
type: "boolean",
},
{
name: "noLibCheck",
type: "boolean",
},
{
name: "noResolve",
type: "boolean",
},
{
name: "out",
type: "string",
description: Diagnostics.Concatenate_and_emit_output_to_single_file,
paramType: Diagnostics.FILE,
},
{
name: "outDir",
type: "string",
isFilePath: true,
2014-08-02 02:12:29 +02:00
description: Diagnostics.Redirect_output_structure_to_the_directory,
paramType: Diagnostics.DIRECTORY,
},
2014-12-10 21:37:09 +01:00
{
name: "preserveConstEnums",
type: "boolean",
description: Diagnostics.Do_not_erase_const_enum_declarations_in_generated_code
},
{
name: "project",
shortName: "p",
type: "string",
isFilePath: true,
description: Diagnostics.Compile_the_project_in_the_given_directory,
paramType: Diagnostics.DIRECTORY
},
2014-08-02 02:12:29 +02:00
{
name: "removeComments",
type: "boolean",
description: Diagnostics.Do_not_emit_comments_to_output,
},
{
2014-08-10 14:02:49 +02:00
name: "sourceMap",
2014-08-02 02:12:29 +02:00
type: "boolean",
description: Diagnostics.Generates_corresponding_map_file,
},
{
name: "sourceRoot",
type: "string",
isFilePath: true,
2014-08-02 02:12:29 +02:00
description: Diagnostics.Specifies_the_location_where_debugger_should_locate_TypeScript_files_instead_of_source_locations,
paramType: Diagnostics.LOCATION,
},
2014-12-10 21:37:09 +01:00
{
name: "suppressImplicitAnyIndexErrors",
type: "boolean",
2014-12-11 02:51:14 +01:00
description: Diagnostics.Suppress_noImplicitAny_errors_for_indexing_objects_lacking_index_signatures,
2014-12-10 21:37:09 +01:00
},
2014-08-02 02:12:29 +02:00
{
name: "target",
shortName: "t",
type: { "es3": ScriptTarget.ES3, "es5": ScriptTarget.ES5, "es6": ScriptTarget.ES6 },
2014-10-11 21:52:42 +02:00
description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES6_experimental,
2014-08-02 02:12:29 +02:00
paramType: Diagnostics.VERSION,
2014-10-11 21:52:42 +02:00
error: Diagnostics.Argument_for_target_option_must_be_es3_es5_or_es6
2014-08-02 02:12:29 +02:00
},
{
name: "version",
shortName: "v",
type: "boolean",
description: Diagnostics.Print_the_compiler_s_version,
},
{
name: "watch",
shortName: "w",
type: "boolean",
description: Diagnostics.Watch_input_files,
2014-12-12 02:47:29 +01:00
}
2014-07-13 01:04:16 +02:00
];
2014-07-13 01:04:16 +02:00
export function parseCommandLine(commandLine: string[]): ParsedCommandLine {
var options: CompilerOptions = {};
2014-07-13 01:04:16 +02:00
var filenames: string[] = [];
var errors: Diagnostic[] = [];
var shortOptionNames: Map<string> = {};
var optionNameMap: Map<CommandLineOption> = {};
2014-07-13 01:04:16 +02:00
forEach(optionDeclarations, option => {
optionNameMap[option.name.toLowerCase()] = option;
if (option.shortName) {
shortOptionNames[option.shortName] = option.name;
}
});
2014-07-13 01:04:16 +02:00
parseStrings(commandLine);
return {
options,
filenames,
errors
2014-07-13 01:04:16 +02:00
};
function parseStrings(args: string[]) {
var i = 0;
while (i < args.length) {
var s = args[i++];
if (s.charCodeAt(0) === CharacterCodes.at) {
parseResponseFile(s.slice(1));
}
else if (s.charCodeAt(0) === CharacterCodes.minus) {
s = s.slice(s.charCodeAt(1) === CharacterCodes.minus ? 2 : 1).toLowerCase();
// Try to translate short option names to their full equivalents.
if (hasProperty(shortOptionNames, s)) {
s = shortOptionNames[s];
}
2014-08-02 02:12:29 +02:00
if (hasProperty(optionNameMap, s)) {
var opt = optionNameMap[s];
2014-07-13 01:04:16 +02:00
// Check to see if no argument was provided (e.g. "--locale" is the last command-line argument).
if (!args[i] && opt.type !== "boolean") {
errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_expects_an_argument, opt.name));
}
switch (opt.type) {
case "number":
options[opt.name] = parseInt(args[i++]);
break;
case "boolean":
options[opt.name] = true;
break;
case "string":
options[opt.name] = args[i++] || "";
break;
// If not a primitive, the possible types are specified in what is effectively a map of options.
default:
var map = <Map<number>>opt.type;
var key = (args[i++] || "").toLowerCase();
if (hasProperty(map, key)) {
options[opt.name] = map[key];
2014-07-13 01:04:16 +02:00
}
else {
errors.push(createCompilerDiagnostic(opt.error));
}
}
}
else {
errors.push(createCompilerDiagnostic(Diagnostics.Unknown_compiler_option_0, s));
}
}
else {
filenames.push(s);
}
}
}
function parseResponseFile(filename: string) {
var text = sys.readFile(filename);
if (!text) {
errors.push(createCompilerDiagnostic(Diagnostics.File_0_not_found, filename));
return;
}
var args: string[] = [];
var pos = 0;
while (true) {
while (pos < text.length && text.charCodeAt(pos) <= CharacterCodes.space) pos++;
if (pos >= text.length) break;
var start = pos;
if (text.charCodeAt(start) === CharacterCodes.doubleQuote) {
pos++;
while (pos < text.length && text.charCodeAt(pos) !== CharacterCodes.doubleQuote) pos++;
if (pos < text.length) {
args.push(text.substring(start + 1, pos));
pos++;
}
else {
errors.push(createCompilerDiagnostic(Diagnostics.Unterminated_quoted_string_in_response_file_0, filename));
}
}
else {
while (text.charCodeAt(pos) > CharacterCodes.space) pos++;
args.push(text.substring(start, pos));
}
}
parseStrings(args);
}
}
export function readConfigFile(filename: string): any {
try {
var text = sys.readFile(filename);
return /\S/.test(text) ? JSON.parse(text) : {};
}
catch (e) {
}
}
export function parseConfigFile(json: any, basePath?: string): ParsedCommandLine {
var errors: Diagnostic[] = [];
return {
options: getCompilerOptions(),
filenames: getFiles(),
errors
};
function getCompilerOptions(): CompilerOptions {
var options: CompilerOptions = {};
var optionNameMap: Map<CommandLineOption> = {};
forEach(optionDeclarations, option => {
optionNameMap[option.name] = option;
});
var jsonOptions = json["compilerOptions"];
if (jsonOptions) {
for (var id in jsonOptions) {
if (hasProperty(optionNameMap, id)) {
var opt = optionNameMap[id];
var optType = opt.type;
var value = jsonOptions[id];
var expectedType = typeof optType === "string" ? optType : "string";
if (typeof value === expectedType) {
if (typeof optType !== "string") {
var key = value.toLowerCase();
if (hasProperty(optType, key)) {
value = optType[key];
}
else {
errors.push(createCompilerDiagnostic(opt.error));
value = 0;
}
}
if (opt.isFilePath) {
value = normalizePath(combinePaths(basePath, value));
}
options[opt.name] = value;
}
else {
errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, id, expectedType));
}
}
else {
errors.push(createCompilerDiagnostic(Diagnostics.Unknown_compiler_option_0, id));
}
}
}
return options;
}
function getFiles(): string[] {
var files: string[] = [];
if (hasProperty(json, "files")) {
if (json["files"] instanceof Array) {
var files = map(<string[]>json["files"], s => combinePaths(basePath, s));
}
}
else {
var sysFiles = sys.readDirectory(basePath, ".ts");
for (var i = 0; i < sysFiles.length; i++) {
var name = sysFiles[i];
if (!fileExtensionIs(name, ".d.ts") || !contains(sysFiles, name.substr(0, name.length - 5) + ".ts")) {
files.push(name);
}
}
}
return files;
}
}
2014-07-13 01:04:16 +02:00
}