Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const read = tsc.sys.readFile(file);
// See https://github.com/Microsoft/TypeScript/blob/a757e8428410c2196886776785c16f8f0c2a62d9/src/compiler/sys.ts#L203 :
// `readFile` returns `undefined` in case the file does not exist!
if (!read) {
throw new Error(`ENOENT: no such file or directory, open '${configPath}'`);
}
return read;
});
// In case of an error, we cannot go further - the config is malformed.
if (loaded.error) {
throw new Error(JSON.stringify(loaded.error, null, 4));
}
// Second step: Parse the config, resolving all potential references.
const basePath = path.dirname(configPath); // equal to "getDirectoryPath" from ts, at least in our case.
const parsedConfig = tsc.parseJsonConfigFileContent(loaded.config, tsc.sys, basePath);
// In case the config is present, it already contains possibly merged entries from following the
// 'extends' entry, thus it is not required to follow it manually.
// This procedure does NOT throw, but generates a list of errors that can/should be evaluated.
if (parsedConfig.errors.length > 0) {
const formattedErrors = formatTscParserErrors(parsedConfig.errors);
throw new Error(`Some errors occurred while attempting to read from ${configPath}: ${formattedErrors}`);
}
return parsedConfig.options;
}
const createProgram = (configFile: string, projectDirectory?: string): ts.Program => {
if (projectDirectory === undefined) {
projectDirectory = path.dirname(configFile);
}
const { config } = ts.readConfigFile(configFile, ts.sys.readFile);
const parseConfigHost: ts.ParseConfigHost = {
fileExists: fs.existsSync,
readDirectory: ts.sys.readDirectory,
readFile: (file) => fs.readFileSync(file, 'utf8'),
useCaseSensitiveFileNames: true,
};
const parsed = ts.parseJsonConfigFileContent(config, parseConfigHost, projectDirectory);
const host = ts.createCompilerHost(parsed.options, true);
const program = ts.createProgram(parsed.fileNames, parsed.options, host);
return program;
};
public static createProgram(
configFile: string,
projectDirectory: string = path.dirname(configFile),
): ts.Program {
const config = ts.readConfigFile(configFile, ts.sys.readFile);
if (config.error !== undefined) {
throw new FatalError(
ts.formatDiagnostics([config.error], {
getCanonicalFileName: f => f,
getCurrentDirectory: process.cwd,
getNewLine: () => "\n",
}),
);
}
const parseConfigHost: ts.ParseConfigHost = {
fileExists: fs.existsSync,
readDirectory: ts.sys.readDirectory,
readFile: file => fs.readFileSync(file, "utf8"),
useCaseSensitiveFileNames: true,
};
const parsed = ts.parseJsonConfigFileContent(
export const createProgramFromTsConfig = (configFile: string): ts.Program => {
const projectDirectory = dirname(configFile);
const { config } = ts.readConfigFile(configFile, ts.sys.readFile);
const parseConfigHost: ts.ParseConfigHost = {
fileExists: existsSync,
readDirectory: ts.sys.readDirectory,
readFile: (file) => readFileSync(file, 'utf8'),
useCaseSensitiveFileNames: true,
};
const parsed = ts.parseJsonConfigFileContent(config, parseConfigHost, projectDirectory);
parsed.options.baseUrl = parsed.options.baseUrl || projectDirectory;
normalizeOptions(parsed.options, configFile);
const host = ts.createCompilerHost(parsed.options, true);
const program = ts.createProgram(parsed.fileNames, parsed.options, host);
return program;
};
getScriptFileNames: () => sourceFiles,
getScriptVersion: _ => "0",
getScriptSnapshot: fileName => {
if (!fs.existsSync(fileName)) {
return undefined
}
return ts.ScriptSnapshot.fromString(
fs.readFileSync(fileName).toString()
)
},
getCurrentDirectory: () => process.cwd(),
getCompilationSettings: () => options,
getDefaultLibFileName: opts => ts.getDefaultLibFilePath(opts),
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
readDirectory: ts.sys.readDirectory,
},
ts.createDocumentRegistry()
)
}
function reportDiagnostic(diagnostic) {
var output = "";
if (diagnostic.file) {
var loc = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
output += diagnostic.file.fileName + "(" + (loc.line+1) + "," + (loc.character+1) + "): ";
}
var category = ts.DiagnosticCategory[diagnostic.category].toLowerCase();
output += category + " TS" + diagnostic.code + ": " + ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine) + ts.sys.newLine;
ts.sys.write(output);
}
getCurrentDirectory: function () { return ts.sys.getCurrentDirectory(); },
getNewLine: function () { return ts.sys.newLine; },
getNewLine: function() {
return ts.sys.newLine;
}
};
listener(FileChangeEvent.Change, path);
break;
case 'unlink':
case 'add':
listener(FileChangeEvent.CreateDelete, path);
break;
case 'unlinkDir':
case 'addDir':
listener(FileChangeEvent.CreateDeleteDir, path);
break;
}
});
watcher.on('ready', ready);
return { close: function () { return watcher.close(); }, ready: ready };
},
setTimeout: (ts.sys.clearTimeout && ts.sys.setTimeout) || setTimeout,
clearTimeout: (ts.sys.setTimeout && ts.sys.clearTimeout) || clearTimeout,
};
}
exports.createPerformWatchHost = createPerformWatchHost;
LSHost.prototype.directoryExists = function (path) {
return ts.sys.directoryExists(path);
};
/**