@@ -466,6 +466,14 @@ namespace ts {
466466 shortOptionNames : Map < string > ;
467467 }
468468
469+ /* @internal */
470+ export const defaultInitCompilerOptions : CompilerOptions = {
471+ module : ModuleKind . CommonJS ,
472+ target : ScriptTarget . ES5 ,
473+ noImplicitAny : false ,
474+ sourceMap : false ,
475+ } ;
476+
469477 let optionNameMapCache : OptionNameMap ;
470478
471479 /* @internal */
@@ -671,6 +679,94 @@ namespace ts {
671679 }
672680 }
673681
682+ /**
683+ * Generate tsconfig configuration when running command line "--init"
684+ * @param options commandlineOptions to be generated into tsconfig.json
685+ * @param fileNames array of filenames to be generated into tsconfig.json
686+ */
687+ /* @internal */
688+ export function generateTSConfig ( options : CompilerOptions , fileNames : string [ ] ) : { compilerOptions : Map < CompilerOptionsValue > } {
689+ const compilerOptions = extend ( options , defaultInitCompilerOptions ) ;
690+ const configurations : any = {
691+ compilerOptions : serializeCompilerOptions ( compilerOptions )
692+ } ;
693+ if ( fileNames && fileNames . length ) {
694+ // only set the files property if we have at least one file
695+ configurations . files = fileNames ;
696+ }
697+
698+ return configurations ;
699+
700+ function getCustomTypeMapOfCommandLineOption ( optionDefinition : CommandLineOption ) : Map < string | number > | undefined {
701+ if ( optionDefinition . type === "string" || optionDefinition . type === "number" || optionDefinition . type === "boolean" ) {
702+ // this is of a type CommandLineOptionOfPrimitiveType
703+ return undefined ;
704+ }
705+ else if ( optionDefinition . type === "list" ) {
706+ return getCustomTypeMapOfCommandLineOption ( ( < CommandLineOptionOfListType > optionDefinition ) . element ) ;
707+ }
708+ else {
709+ return ( < CommandLineOptionOfCustomType > optionDefinition ) . type ;
710+ }
711+ }
712+
713+ function getNameOfCompilerOptionValue ( value : CompilerOptionsValue , customTypeMap : MapLike < string | number > ) : string | undefined {
714+ // There is a typeMap associated with this command-line option so use it to map value back to its name
715+ for ( const key in customTypeMap ) {
716+ if ( customTypeMap [ key ] === value ) {
717+ return key ;
718+ }
719+ }
720+ return undefined ;
721+ }
722+
723+ function serializeCompilerOptions ( options : CompilerOptions ) : Map < CompilerOptionsValue > {
724+ const result = createMap < CompilerOptionsValue > ( ) ;
725+ const optionsNameMap = getOptionNameMap ( ) . optionNameMap ;
726+
727+ for ( const name in options ) {
728+ if ( hasProperty ( options , name ) ) {
729+ // tsconfig only options cannot be specified via command line,
730+ // so we can assume that only types that can appear here string | number | boolean
731+ switch ( name ) {
732+ case "init" :
733+ case "watch" :
734+ case "version" :
735+ case "help" :
736+ case "project" :
737+ break ;
738+ default :
739+ const value = options [ name ] ;
740+ let optionDefinition = optionsNameMap [ name . toLowerCase ( ) ] ;
741+ if ( optionDefinition ) {
742+ const customTypeMap = getCustomTypeMapOfCommandLineOption ( optionDefinition ) ;
743+ if ( ! customTypeMap ) {
744+ // There is no map associated with this compiler option then use the value as-is
745+ // This is the case if the value is expect to be string, number, boolean or list of string
746+ result [ name ] = value ;
747+ }
748+ else {
749+ if ( optionDefinition . type === "list" ) {
750+ const convertedValue : string [ ] = [ ] ;
751+ for ( const element of value as ( string | number ) [ ] ) {
752+ convertedValue . push ( getNameOfCompilerOptionValue ( element , customTypeMap ) ) ;
753+ }
754+ result [ name ] = convertedValue ;
755+ }
756+ else {
757+ // There is a typeMap associated with this command-line option so use it to map value back to its name
758+ result [ name ] = getNameOfCompilerOptionValue ( value , customTypeMap ) ;
759+ }
760+ }
761+ }
762+ break ;
763+ }
764+ }
765+ }
766+ return result ;
767+ }
768+ }
769+
674770 /**
675771 * Remove the comments from a json like text.
676772 * Comments can be single line comments (starting with # or //) or multiline comments using / * * /
0 commit comments