@@ -164,6 +164,13 @@ namespace ts {
164
164
category : Diagnostics . Command_line_Options ,
165
165
description : Diagnostics . Stylize_errors_and_messages_using_color_and_context_experimental
166
166
} ,
167
+ {
168
+ name : "showConfig" ,
169
+ type : "boolean" ,
170
+ category : Diagnostics . Command_line_Options ,
171
+ isCommandLineOnly : true ,
172
+ description : Diagnostics . Print_the_final_configuration_instead_of_building
173
+ } ,
167
174
168
175
// Basic
169
176
{
@@ -1654,72 +1661,146 @@ namespace ts {
1654
1661
}
1655
1662
1656
1663
/**
1657
- * Generate tsconfig configuration when running command line "--init"
1658
- * @param options commandlineOptions to be generated into tsconfig.json
1659
- * @param fileNames array of filenames to be generated into tsconfig.json
1664
+ * Generate an uncommented, complete tsconfig for use with "--showConfig"
1665
+ * @param configParseResult options to be generated into tsconfig.json
1666
+ * @param configFileName name of the parsed config file - output paths will be generated relative to this
1667
+ * @param host provides current directory and case sensitivity services
1660
1668
*/
1661
- /* @internal */
1662
- export function generateTSConfig ( options : CompilerOptions , fileNames : ReadonlyArray < string > , newLine : string ) : string {
1663
- const compilerOptions = extend ( options , defaultInitCompilerOptions ) ;
1664
- const compilerOptionsMap = serializeCompilerOptions ( compilerOptions ) ;
1665
- return writeConfigurations ( ) ;
1669
+ /** @internal */
1670
+ export function convertToTSConfig ( configParseResult : ParsedCommandLine , configFileName : string , host : { getCurrentDirectory ( ) : string , useCaseSensitiveFileNames : boolean } ) : object {
1671
+ const getCanonicalFileName = createGetCanonicalFileName ( host . useCaseSensitiveFileNames ) ;
1672
+ const files = map (
1673
+ filter (
1674
+ configParseResult . fileNames ,
1675
+ ! configParseResult . configFileSpecs ? _ => false : matchesSpecs (
1676
+ configFileName ,
1677
+ configParseResult . configFileSpecs . validatedIncludeSpecs ,
1678
+ configParseResult . configFileSpecs . validatedExcludeSpecs
1679
+ )
1680
+ ) ,
1681
+ f => getRelativePathFromFile ( getNormalizedAbsolutePath ( configFileName , host . getCurrentDirectory ( ) ) , f , getCanonicalFileName )
1682
+ ) ;
1683
+ const optionMap = serializeCompilerOptions ( configParseResult . options , { configFilePath : getNormalizedAbsolutePath ( configFileName , host . getCurrentDirectory ( ) ) , useCaseSensitiveFileNames : host . useCaseSensitiveFileNames } ) ;
1684
+ const config = {
1685
+ compilerOptions : {
1686
+ ...arrayFrom ( optionMap . entries ( ) ) . reduce ( ( prev , cur ) => ( { ...prev , [ cur [ 0 ] ] : cur [ 1 ] } ) , { } ) ,
1687
+ showConfig : undefined ,
1688
+ configFile : undefined ,
1689
+ configFilePath : undefined ,
1690
+ help : undefined ,
1691
+ init : undefined ,
1692
+ listFiles : undefined ,
1693
+ listEmittedFiles : undefined ,
1694
+ project : undefined ,
1695
+ } ,
1696
+ references : map ( configParseResult . projectReferences , r => ( { ...r , path : r . originalPath , originalPath : undefined } ) ) ,
1697
+ files : length ( files ) ? files : undefined ,
1698
+ ...( configParseResult . configFileSpecs ? {
1699
+ include : filterSameAsDefaultInclude ( configParseResult . configFileSpecs . validatedIncludeSpecs ) ,
1700
+ exclude : configParseResult . configFileSpecs . validatedExcludeSpecs
1701
+ } : { } ) ,
1702
+ compilerOnSave : ! ! configParseResult . compileOnSave ? true : undefined
1703
+ } ;
1704
+ return config ;
1705
+ }
1666
1706
1667
- function getCustomTypeMapOfCommandLineOption ( optionDefinition : CommandLineOption ) : Map < string | number > | undefined {
1668
- if ( optionDefinition . type === "string" || optionDefinition . type === "number" || optionDefinition . type === "boolean" ) {
1669
- // this is of a type CommandLineOptionOfPrimitiveType
1670
- return undefined ;
1671
- }
1672
- else if ( optionDefinition . type === "list" ) {
1673
- return getCustomTypeMapOfCommandLineOption ( optionDefinition . element ) ;
1674
- }
1675
- else {
1676
- return ( < CommandLineOptionOfCustomType > optionDefinition ) . type ;
1707
+ function filterSameAsDefaultInclude ( specs : ReadonlyArray < string > | undefined ) {
1708
+ if ( ! length ( specs ) ) return undefined ;
1709
+ if ( length ( specs ) !== 1 ) return specs ;
1710
+ if ( specs ! [ 0 ] === "**/*" ) return undefined ;
1711
+ return specs ;
1712
+ }
1713
+
1714
+ function matchesSpecs ( path : string , includeSpecs : ReadonlyArray < string > | undefined , excludeSpecs : ReadonlyArray < string > | undefined ) : ( path : string ) => boolean {
1715
+ if ( ! includeSpecs ) return _ => false ;
1716
+ const patterns = getFileMatcherPatterns ( path , excludeSpecs , includeSpecs , sys . useCaseSensitiveFileNames , sys . getCurrentDirectory ( ) ) ;
1717
+ const excludeRe = patterns . excludePattern && getRegexFromPattern ( patterns . excludePattern , sys . useCaseSensitiveFileNames ) ;
1718
+ const includeRe = patterns . includeFilePattern && getRegexFromPattern ( patterns . includeFilePattern , sys . useCaseSensitiveFileNames ) ;
1719
+ if ( includeRe ) {
1720
+ if ( excludeRe ) {
1721
+ return path => includeRe . test ( path ) && ! excludeRe . test ( path ) ;
1677
1722
}
1723
+ return path => includeRe . test ( path ) ;
1724
+ }
1725
+ if ( excludeRe ) {
1726
+ return path => ! excludeRe . test ( path ) ;
1678
1727
}
1728
+ return _ => false ;
1729
+ }
1679
1730
1680
- function getNameOfCompilerOptionValue ( value : CompilerOptionsValue , customTypeMap : Map < string | number > ) : string | undefined {
1681
- // There is a typeMap associated with this command-line option so use it to map value back to its name
1682
- return forEachEntry ( customTypeMap , ( mapValue , key ) => {
1683
- if ( mapValue === value ) {
1684
- return key ;
1685
- }
1686
- } ) ;
1731
+ function getCustomTypeMapOfCommandLineOption ( optionDefinition : CommandLineOption ) : Map < string | number > | undefined {
1732
+ if ( optionDefinition . type === "string" || optionDefinition . type === "number" || optionDefinition . type === "boolean" ) {
1733
+ // this is of a type CommandLineOptionOfPrimitiveType
1734
+ return undefined ;
1735
+ }
1736
+ else if ( optionDefinition . type === "list" ) {
1737
+ return getCustomTypeMapOfCommandLineOption ( optionDefinition . element ) ;
1738
+ }
1739
+ else {
1740
+ return ( < CommandLineOptionOfCustomType > optionDefinition ) . type ;
1687
1741
}
1742
+ }
1688
1743
1689
- function serializeCompilerOptions ( options : CompilerOptions ) : Map < CompilerOptionsValue > {
1690
- const result = createMap < CompilerOptionsValue > ( ) ;
1691
- const optionsNameMap = getOptionNameMap ( ) . optionNameMap ;
1744
+ function getNameOfCompilerOptionValue ( value : CompilerOptionsValue , customTypeMap : Map < string | number > ) : string | undefined {
1745
+ // There is a typeMap associated with this command-line option so use it to map value back to its name
1746
+ return forEachEntry ( customTypeMap , ( mapValue , key ) => {
1747
+ if ( mapValue === value ) {
1748
+ return key ;
1749
+ }
1750
+ } ) ;
1751
+ }
1692
1752
1693
- for ( const name in options ) {
1694
- if ( hasProperty ( options , name ) ) {
1695
- // tsconfig only options cannot be specified via command line,
1696
- // so we can assume that only types that can appear here string | number | boolean
1697
- if ( optionsNameMap . has ( name ) && optionsNameMap . get ( name ) ! . category === Diagnostics . Command_line_Options ) {
1698
- continue ;
1699
- }
1700
- const value = < CompilerOptionsValue > options [ name ] ;
1701
- const optionDefinition = optionsNameMap . get ( name . toLowerCase ( ) ) ;
1702
- if ( optionDefinition ) {
1703
- const customTypeMap = getCustomTypeMapOfCommandLineOption ( optionDefinition ) ;
1704
- if ( ! customTypeMap ) {
1705
- // There is no map associated with this compiler option then use the value as-is
1706
- // This is the case if the value is expect to be string, number, boolean or list of string
1753
+ function serializeCompilerOptions ( options : CompilerOptions , pathOptions ?: { configFilePath : string , useCaseSensitiveFileNames : boolean } ) : Map < CompilerOptionsValue > {
1754
+ const result = createMap < CompilerOptionsValue > ( ) ;
1755
+ const optionsNameMap = getOptionNameMap ( ) . optionNameMap ;
1756
+ const getCanonicalFileName = pathOptions && createGetCanonicalFileName ( pathOptions . useCaseSensitiveFileNames ) ;
1757
+
1758
+ for ( const name in options ) {
1759
+ if ( hasProperty ( options , name ) ) {
1760
+ // tsconfig only options cannot be specified via command line,
1761
+ // so we can assume that only types that can appear here string | number | boolean
1762
+ if ( optionsNameMap . has ( name ) && optionsNameMap . get ( name ) ! . category === Diagnostics . Command_line_Options ) {
1763
+ continue ;
1764
+ }
1765
+ const value = < CompilerOptionsValue > options [ name ] ;
1766
+ const optionDefinition = optionsNameMap . get ( name . toLowerCase ( ) ) ;
1767
+ if ( optionDefinition ) {
1768
+ const customTypeMap = getCustomTypeMapOfCommandLineOption ( optionDefinition ) ;
1769
+ if ( ! customTypeMap ) {
1770
+ // There is no map associated with this compiler option then use the value as-is
1771
+ // This is the case if the value is expect to be string, number, boolean or list of string
1772
+ if ( pathOptions && optionDefinition . isFilePath ) {
1773
+ result . set ( name , getRelativePathFromFile ( pathOptions . configFilePath , getNormalizedAbsolutePath ( value as string , getDirectoryPath ( pathOptions . configFilePath ) ) , getCanonicalFileName ! ) ) ;
1774
+ }
1775
+ else {
1707
1776
result . set ( name , value ) ;
1708
1777
}
1778
+ }
1779
+ else {
1780
+ if ( optionDefinition . type === "list" ) {
1781
+ result . set ( name , ( value as ReadonlyArray < string | number > ) . map ( element => getNameOfCompilerOptionValue ( element , customTypeMap ) ! ) ) ; // TODO: GH#18217
1782
+ }
1709
1783
else {
1710
- if ( optionDefinition . type === "list" ) {
1711
- result . set ( name , ( value as ReadonlyArray < string | number > ) . map ( element => getNameOfCompilerOptionValue ( element , customTypeMap ) ! ) ) ; // TODO: GH#18217
1712
- }
1713
- else {
1714
- // There is a typeMap associated with this command-line option so use it to map value back to its name
1715
- result . set ( name , getNameOfCompilerOptionValue ( value , customTypeMap ) ) ;
1716
- }
1784
+ // There is a typeMap associated with this command-line option so use it to map value back to its name
1785
+ result . set ( name , getNameOfCompilerOptionValue ( value , customTypeMap ) ) ;
1717
1786
}
1718
1787
}
1719
1788
}
1720
1789
}
1721
- return result ;
1722
1790
}
1791
+ return result ;
1792
+ }
1793
+
1794
+ /**
1795
+ * Generate tsconfig configuration when running command line "--init"
1796
+ * @param options commandlineOptions to be generated into tsconfig.json
1797
+ * @param fileNames array of filenames to be generated into tsconfig.json
1798
+ */
1799
+ /* @internal */
1800
+ export function generateTSConfig ( options : CompilerOptions , fileNames : ReadonlyArray < string > , newLine : string ) : string {
1801
+ const compilerOptions = extend ( options , defaultInitCompilerOptions ) ;
1802
+ const compilerOptionsMap = serializeCompilerOptions ( compilerOptions ) ;
1803
+ return writeConfigurations ( ) ;
1723
1804
1724
1805
function getDefaultValueForOption ( option : CommandLineOption ) {
1725
1806
switch ( option . type ) {
0 commit comments