Created
September 11, 2019 13:59
-
-
Save hediet/3666cf23a07bf4bc4fc2e308fa53431d to your computer and use it in GitHub Desktop.
createProgram from tsConfigSearchPath
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 * as ts from "typescript"; | |
import { dirname, resolve } from "path"; | |
export function createProgram(tsConfigSearchPath: string): ts.Program { | |
const configPath = ts.findConfigFile( | |
tsConfigSearchPath, | |
ts.sys.fileExists, | |
"tsconfig.json" | |
); | |
const parseConfigHost: ts.ParseConfigHost = { | |
fileExists: ts.sys.fileExists, | |
readFile: ts.sys.readFile, | |
readDirectory: ts.sys.readDirectory, | |
useCaseSensitiveFileNames: true, | |
}; | |
if (!configPath) { | |
throw new Error(); | |
} | |
const configFile = ts.readConfigFile(configPath, ts.sys.readFile); | |
const compilerOptions = ts.parseJsonConfigFileContent( | |
configFile.config, | |
parseConfigHost, | |
resolve(dirname(configPath)) | |
); | |
const host = ts.createCompilerHost(compilerOptions.options, true); | |
const prog = ts.createProgram( | |
compilerOptions.fileNames, | |
compilerOptions.options, | |
host | |
); | |
return prog; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment