Skip to content

Instantly share code, notes, and snippets.

@hediet
Created September 11, 2019 13:59
Show Gist options
  • Save hediet/3666cf23a07bf4bc4fc2e308fa53431d to your computer and use it in GitHub Desktop.
Save hediet/3666cf23a07bf4bc4fc2e308fa53431d to your computer and use it in GitHub Desktop.
createProgram from tsConfigSearchPath
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