feat: display the tsconfig generated when running tsc --init (#45930)

* feat: display the tsconfig generated when running tsc --init

* fix: fix lint issues

* refactor: minor changes
This commit is contained in:
Alex Munoz
2021-10-01 15:08:06 +02:00
committed by GitHub
parent 46a12fdb8a
commit 95ef2a503d
2 changed files with 75 additions and 30 deletions

View File

@@ -2351,6 +2351,47 @@ namespace ts {
return result;
}
/**
* Generate a list of the compiler options whose value is not the default.
* @param options compilerOptions to be evaluated.
/** @internal */
export function getCompilerOptionsDiffValue(options: CompilerOptions, newLine: string): string {
const compilerOptionsMap = getSerializedCompilerOption(options);
return getOverwrittenDefaultOptions();
function makePadding(paddingLength: number): string {
return Array(paddingLength + 1).join(" ");
}
function getOverwrittenDefaultOptions() {
const result: string[] = [];
const tab = makePadding(2);
commandOptionsWithoutBuild.forEach(cmd => {
if (!compilerOptionsMap.has(cmd.name)) {
return;
}
const newValue = compilerOptionsMap.get(cmd.name);
const defaultValue = getDefaultValueForOption(cmd);
if (newValue !== defaultValue) {
result.push(`${tab}${cmd.name}: ${newValue}`);
}
else if (hasProperty(defaultInitCompilerOptions, cmd.name)) {
result.push(`${tab}${cmd.name}: ${defaultValue}`);
}
});
return result.join(newLine) + newLine;
}
}
/**
* Get the compiler options to be written into the tsconfig.json.
* @param options commandlineOptions to be included in the compileOptions.
*/
function getSerializedCompilerOption(options: CompilerOptions): ESMap<string, CompilerOptionsValue> {
const compilerOptions = extend(options, defaultInitCompilerOptions);
return serializeCompilerOptions(compilerOptions);
}
/**
* Generate tsconfig configuration when running command line "--init"
* @param options commandlineOptions to be generated into tsconfig.json
@@ -2358,29 +2399,9 @@ namespace ts {
*/
/* @internal */
export function generateTSConfig(options: CompilerOptions, fileNames: readonly string[], newLine: string): string {
const compilerOptions = extend(options, defaultInitCompilerOptions);
const compilerOptionsMap = serializeCompilerOptions(compilerOptions);
const compilerOptionsMap = getSerializedCompilerOption(options);
return writeConfigurations();
function getDefaultValueForOption(option: CommandLineOption) {
switch (option.type) {
case "number":
return 1;
case "boolean":
return true;
case "string":
return option.isFilePath ? "./" : "";
case "list":
return [];
case "object":
return {};
default:
const iterResult = option.type.keys().next();
if (!iterResult.done) return iterResult.value;
return Debug.fail("Expected 'option.type' to have entries.");
}
}
function makePadding(paddingLength: number): string {
return Array(paddingLength + 1).join(" ");
}
@@ -3542,4 +3563,24 @@ namespace ts {
})!; // TODO: GH#18217
}
}
function getDefaultValueForOption(option: CommandLineOption) {
switch (option.type) {
case "number":
return 1;
case "boolean":
return true;
case "string":
return option.isFilePath ? "./" : "";
case "list":
return [];
case "object":
return {};
default:
const iterResult = option.type.keys().next();
if (!iterResult.done) return iterResult.value;
return Debug.fail("Expected 'option.type' to have entries.");
}
}
}