Created
September 1, 2018 19:14
-
-
Save GongT/1d5113a2ffd8ac7cac53123af61039d1 to your computer and use it in GitHub Desktop.
the first part of typescript analyze process.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
createCompilerHost, | |
createProgram, | |
Diagnostic, | |
formatDiagnostic, | |
FormatDiagnosticsHost, | |
getParsedCommandLineOfConfigFile, | |
ParseConfigFileHost, | |
ParsedCommandLine, | |
Program, | |
sys, | |
} from 'typescript'; | |
import { normalize } from 'path'; | |
const program = createProgramConfigByTsConfig('./src/tsconfig.json'); | |
for (const file of program.getSourceFiles()) { | |
console.log(file.fileName); | |
} | |
function createProgramConfigByTsConfig(tsconfigPath: string): Program { | |
const myFormatDiagnosticsHost: FormatDiagnosticsHost = { | |
getCurrentDirectory : sys.getCurrentDirectory, | |
getCanonicalFileName: normalize, | |
getNewLine(): string { | |
return sys.newLine; | |
}, | |
}; | |
const createConfigFileHost: ParseConfigFileHost = { | |
onUnRecoverableConfigFileDiagnostic(diagnostic: Diagnostic) { | |
console.error(formatDiagnostic(diagnostic, myFormatDiagnosticsHost)); | |
}, | |
useCaseSensitiveFileNames: false, | |
readDirectory : sys.readDirectory, | |
fileExists : sys.fileExists, | |
readFile : sys.readFile, | |
getCurrentDirectory : sys.getCurrentDirectory, | |
}; | |
const configParseResult: ParsedCommandLine = getParsedCommandLineOfConfigFile(tsconfigPath, {}, createConfigFileHost); | |
const host = createCompilerHost(configParseResult.options, true); | |
return createProgram(configParseResult.fileNames, configParseResult.options, host); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment