Move handeling to a diffrent function, and remove specialized serialization

This commit is contained in:
Mohamed Hegazy 2015-08-25 17:42:39 -07:00
parent 16a6de281c
commit 509232f477
2 changed files with 61 additions and 80 deletions

View File

@ -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;

View File

@ -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<string|number|boolean> {
let result: Map<string|number|boolean> = {};
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 = <Map<number>>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);