From 509232f47738f33bacc4b197034eca03af856506 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Tue, 25 Aug 2015 17:42:39 -0700 Subject: [PATCH] Move handeling to a diffrent function, and remove specialized serialization --- src/compiler/program.ts | 40 +--------------- src/compiler/tsc.ts | 101 ++++++++++++++++++++++++---------------- 2 files changed, 61 insertions(+), 80 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 36700e0fbf5..a1861f6b462 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -233,45 +233,7 @@ namespace ts { outDir: "built", rootDir: ".", sourceMap: false, - } - - /* @internal */ - export function scriptTargetToString(target: ScriptTarget): string { - switch (target) { - case ScriptTarget.ES5: - return "es5"; - case ScriptTarget.ES6: - return "es6"; - default: - return "es3"; - } - } - - /* @internal */ - export function moduleKindToString(kind: ModuleKind): string { - switch (kind) { - case ModuleKind.None: - return undefined; - case ModuleKind.CommonJS: - return "commonjs"; - case ModuleKind.System: - return "system"; - case ModuleKind.UMD: - return "umd"; - default: - return "amd"; - } - } - - /* @internal */ - export function newLineKindToString(kind: NewLineKind): string { - switch (kind) { - case NewLineKind.CarriageReturnLineFeed: - return "CRLF"; - default: - return "LF"; - } - } + }; export function createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost { let currentDirectory: string; diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index df193f4129b..6c0b80595dc 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -160,47 +160,7 @@ namespace ts { } if (commandLine.options.init) { - let file = combinePaths(sys.getCurrentDirectory(), 'tsconfig.json'); - if (sys.fileExists(file)) { - reportDiagnostic(createCompilerDiagnostic(Diagnostics.A_tsconfig_json_file_is_already_defined_at_Colon_0, file)); - } - else { - let compilerOptions = extend(commandLine.options, defaultInitCompilerOptions); - let configs = { - compilerOptions, - files: commandLine.fileNames, - exclude: ["node_modules"] - }; - sys.writeFile(file, JSON.stringify(configs, (k, v) => { - if (k === "compilerOptions") { - let options: CompilerOptions = v; - for (let o in options) { - switch (o) { - case "target": - options[o] = scriptTargetToString(options[o] as ScriptTarget); - continue; - case "module": - options[o] = moduleKindToString(options[o] as ModuleKind); - continue; - case "newLine": - options[o] = newLineKindToString(options[o] as NewLineKind); - continue; - case "init": - case "watch": - case "version": - case "help": - delete options[o]; - continue; - } - } - - return options; - } - - return v; - }, 4)); - reportDiagnostic(createCompilerDiagnostic(Diagnostics.Successfully_created_a_tsconfig_json_file)); - } + writeConfigFile(commandLine.options, commandLine.fileNames); return sys.exit(ExitStatus.Success); } @@ -534,6 +494,65 @@ namespace ts { return Array(paddingLength + 1).join(" "); } } + + function writeConfigFile(options: CompilerOptions, fileNames: string[]) { + let currentDirectory = sys.getCurrentDirectory(); + let file = combinePaths(currentDirectory, 'tsconfig.json'); + if (sys.fileExists(file)) { + reportDiagnostic(createCompilerDiagnostic(Diagnostics.A_tsconfig_json_file_is_already_defined_at_Colon_0, file)); + } + else { + let compilerOptions = extend(options, defaultInitCompilerOptions); + let configs = { + compilerOptions: serializeCompilerOptions(compilerOptions, currentDirectory), + files: fileNames, + exclude: ["node_modules"] + }; + sys.writeFile(file, JSON.stringify(configs, undefined, 4)); + reportDiagnostic(createCompilerDiagnostic(Diagnostics.Successfully_created_a_tsconfig_json_file)); + } + + return; + + function serializeCompilerOptions(options: CompilerOptions, currentDirectory: string): Map { + let result: Map = {}; + let optionsNameMap = getOptionNameMap().optionNameMap; + + for (let name in options) { + if (hasProperty(options, name)) { + let value = options[name]; + switch (name) { + case "init": + case "watch": + case "version": + case "help": + case "project": + break; + default: + let optionDefinition = optionsNameMap[name]; + if (optionDefinition) { + if (typeof optionDefinition.type === "string") { + // string, number or boolean + result[name] = value; + } + else { + // Enum + let typeMap = >optionDefinition.type; + for (let key in typeMap) { + if (hasProperty(typeMap, key)) { + if (typeMap[key] === value) + result[name] = key; + } + } + } + } + break; + } + } + } + return result; + } + } } ts.executeCommandLine(ts.sys.args);