Skip to content

Commit 205b3da

Browse files
committed
Extract shared helper
1 parent f39b49d commit 205b3da

File tree

4 files changed

+48
-65
lines changed

4 files changed

+48
-65
lines changed

src/compiler/program.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -103,31 +103,20 @@ namespace ts {
103103
return false;
104104
}
105105

106-
function ensureDirectoriesExist(directoryPath: string) {
107-
if (directoryPath.length > getRootLength(directoryPath) && !directoryExists(directoryPath)) {
108-
const parentDirectory = getDirectoryPath(directoryPath);
109-
ensureDirectoriesExist(parentDirectory);
110-
(compilerHost.createDirectory || system.createDirectory)(directoryPath);
111-
}
112-
}
113-
114106
function writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) {
115107
try {
116108
performance.mark("beforeIOWrite");
117109

118-
// PERF: Checking for directory existence is expensive.
119-
// Instead, assume the directory exists and fall back
120-
// to creating it if the file write fails.
121110
// NOTE: If patchWriteFileEnsuringDirectory has been called,
122-
// the file write will do its own directory creation and
111+
// the system.writeFile will do its own directory creation and
123112
// the ensureDirectoriesExist call will always be redundant.
124-
try {
125-
writeFileWorker(fileName, data, writeByteOrderMark);
126-
}
127-
catch {
128-
ensureDirectoriesExist(getDirectoryPath(normalizePath(fileName)));
129-
writeFileWorker(fileName, data, writeByteOrderMark);
130-
}
113+
writeFileEnsuringDirectories(
114+
fileName,
115+
data,
116+
writeByteOrderMark,
117+
writeFileWorker,
118+
compilerHost.createDirectory || system.createDirectory,
119+
directoryExists);
131120

132121
performance.mark("afterIOWrite");
133122
performance.measure("I/O Write", "beforeIOWrite", "afterIOWrite");

src/compiler/sys.ts

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -522,40 +522,21 @@ namespace ts {
522522
}
523523
}
524524

525-
function recursiveCreateDirectory(directoryPath: string, sys: System) {
526-
const basePath = getDirectoryPath(directoryPath);
527-
const shouldCreateParent = basePath !== "" && directoryPath !== basePath && !sys.directoryExists(basePath);
528-
if (shouldCreateParent) {
529-
recursiveCreateDirectory(basePath, sys);
530-
}
531-
if (shouldCreateParent || !sys.directoryExists(directoryPath)) {
532-
sys.createDirectory(directoryPath);
533-
}
534-
}
535-
536525
/**
537526
* patch writefile to create folder before writing the file
538527
*/
539528
/*@internal*/
540529
export function patchWriteFileEnsuringDirectory(sys: System) {
541530
// patch writefile to create folder before writing the file
542531
const originalWriteFile = sys.writeFile;
543-
sys.writeFile = (path, data, writeBom) => {
544-
// PERF: Checking for directory existence is expensive.
545-
// Instead, assume the directory exists and fall back
546-
// to creating it if the file write fails.
547-
try {
548-
originalWriteFile.call(sys, path, data, writeBom);
549-
}
550-
catch {
551-
const directoryPath = getDirectoryPath(normalizeSlashes(path));
552-
if (directoryPath && !sys.directoryExists(directoryPath)) {
553-
recursiveCreateDirectory(directoryPath, sys);
554-
}
555-
556-
originalWriteFile.call(sys, path, data, writeBom);
557-
}
558-
};
532+
sys.writeFile = (path, data, writeBom) =>
533+
writeFileEnsuringDirectories(
534+
path,
535+
data,
536+
writeBom,
537+
(p, d, w) => originalWriteFile.call(sys, p, d, w),
538+
sys.createDirectory,
539+
sys.directoryExists);
559540
}
560541

561542
/*@internal*/

src/compiler/utilities.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3700,6 +3700,36 @@ namespace ts {
37003700
}, sourceFiles);
37013701
}
37023702

3703+
function ensureDirectoriesExist(
3704+
directoryPath: string,
3705+
createDirectory: (path: string) => void,
3706+
directoryExists: (path: string) => boolean): void {
3707+
if (directoryPath.length > getRootLength(directoryPath) && !directoryExists(directoryPath)) {
3708+
const parentDirectory = getDirectoryPath(directoryPath);
3709+
ensureDirectoriesExist(parentDirectory, createDirectory, directoryExists);
3710+
createDirectory(directoryPath);
3711+
}
3712+
}
3713+
3714+
export function writeFileEnsuringDirectories(
3715+
path: string,
3716+
data: string,
3717+
writeByteOrderMark: boolean | undefined,
3718+
writeFile: (path: string, data: string, writeByteOrderMark?: boolean) => void,
3719+
createDirectory: (path: string) => void,
3720+
directoryExists: (path: string) => boolean): void {
3721+
3722+
// PERF: Checking for directory existence is expensive. Instead, assume the directory exists
3723+
// and fall back to creating it if the file write fails.
3724+
try {
3725+
writeFile(path, data, writeByteOrderMark);
3726+
}
3727+
catch {
3728+
ensureDirectoriesExist(getDirectoryPath(normalizePath(path)), createDirectory, directoryExists);
3729+
writeFile(path, data, writeByteOrderMark);
3730+
}
3731+
}
3732+
37033733
export function getLineOfLocalPosition(currentSourceFile: SourceFile, pos: number) {
37043734
return getLineAndCharacterOfPosition(currentSourceFile, pos).line;
37053735
}

src/compiler/watch.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -296,31 +296,14 @@ namespace ts {
296296
readDirectory: maybeBind(host, host.readDirectory),
297297
};
298298

299-
function ensureDirectoriesExist(directoryPath: string) {
300-
if (directoryPath.length > getRootLength(directoryPath) && !host.directoryExists!(directoryPath)) {
301-
const parentDirectory = getDirectoryPath(directoryPath);
302-
ensureDirectoriesExist(parentDirectory);
303-
if (host.createDirectory) host.createDirectory(directoryPath);
304-
}
305-
}
306-
307299
function writeFile(fileName: string, text: string, writeByteOrderMark: boolean, onError: (message: string) => void) {
308300
try {
309301
performance.mark("beforeIOWrite");
310302

311-
// PERF: Checking for directory existence is expensive.
312-
// Instead, assume the directory exists and fall back
313-
// to creating it if the file write fails.
314303
// NOTE: If patchWriteFileEnsuringDirectory has been called,
315-
// the file write will do its own directory creation and
304+
// the host.writeFile will do its own directory creation and
316305
// the ensureDirectoriesExist call will always be redundant.
317-
try {
318-
host.writeFile!(fileName, text, writeByteOrderMark);
319-
}
320-
catch {
321-
ensureDirectoriesExist(getDirectoryPath(normalizePath(fileName)));
322-
host.writeFile!(fileName, text, writeByteOrderMark);
323-
}
306+
writeFileEnsuringDirectories(fileName, text, writeByteOrderMark, host.writeFile!, host.createDirectory!, host.directoryExists!);
324307

325308
performance.mark("afterIOWrite");
326309
performance.measure("I/O Write", "beforeIOWrite", "afterIOWrite");

0 commit comments

Comments
 (0)