Extract shared helper

This commit is contained in:
Andrew Casey
2019-10-17 16:26:43 -07:00
parent f39b49d756
commit 205b3dae3b
4 changed files with 48 additions and 65 deletions

View File

@@ -103,31 +103,20 @@ namespace ts {
return false;
}
function ensureDirectoriesExist(directoryPath: string) {
if (directoryPath.length > getRootLength(directoryPath) && !directoryExists(directoryPath)) {
const parentDirectory = getDirectoryPath(directoryPath);
ensureDirectoriesExist(parentDirectory);
(compilerHost.createDirectory || system.createDirectory)(directoryPath);
}
}
function writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) {
try {
performance.mark("beforeIOWrite");
// PERF: Checking for directory existence is expensive.
// Instead, assume the directory exists and fall back
// to creating it if the file write fails.
// NOTE: If patchWriteFileEnsuringDirectory has been called,
// the file write will do its own directory creation and
// the system.writeFile will do its own directory creation and
// the ensureDirectoriesExist call will always be redundant.
try {
writeFileWorker(fileName, data, writeByteOrderMark);
}
catch {
ensureDirectoriesExist(getDirectoryPath(normalizePath(fileName)));
writeFileWorker(fileName, data, writeByteOrderMark);
}
writeFileEnsuringDirectories(
fileName,
data,
writeByteOrderMark,
writeFileWorker,
compilerHost.createDirectory || system.createDirectory,
directoryExists);
performance.mark("afterIOWrite");
performance.measure("I/O Write", "beforeIOWrite", "afterIOWrite");