Skip to content

Instantly share code, notes, and snippets.

@GongT
Created September 1, 2018 19:14
Show Gist options
  • Save GongT/1d5113a2ffd8ac7cac53123af61039d1 to your computer and use it in GitHub Desktop.
Save GongT/1d5113a2ffd8ac7cac53123af61039d1 to your computer and use it in GitHub Desktop.
the first part of typescript analyze process.
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