@@ -472,6 +472,33 @@ namespace ts {
472472 }
473473 }
474474
475+ function recursiveCreateDirectory ( directoryPath : string , sys : System ) {
476+ const basePath = getDirectoryPath ( directoryPath ) ;
477+ const shouldCreateParent = basePath !== "" && directoryPath !== basePath && ! sys . directoryExists ( basePath ) ;
478+ if ( shouldCreateParent ) {
479+ recursiveCreateDirectory ( basePath , sys ) ;
480+ }
481+ if ( shouldCreateParent || ! sys . directoryExists ( directoryPath ) ) {
482+ sys . createDirectory ( directoryPath ) ;
483+ }
484+ }
485+
486+ /**
487+ * patch writefile to create folder before writing the file
488+ */
489+ /*@internal */
490+ export function patchWriteFileEnsuringDirectory ( sys : System ) {
491+ // patch writefile to create folder before writing the file
492+ const originalWriteFile = sys . writeFile ;
493+ sys . writeFile = ( path , data , writeBom ) => {
494+ const directoryPath = getDirectoryPath ( normalizeSlashes ( path ) ) ;
495+ if ( directoryPath && ! sys . directoryExists ( directoryPath ) ) {
496+ recursiveCreateDirectory ( directoryPath , sys ) ;
497+ }
498+ originalWriteFile . call ( sys , path , data , writeBom ) ;
499+ } ;
500+ }
501+
475502 /*@internal */
476503 interface NodeBuffer extends Uint8Array {
477504 write ( str : string , offset ?: number , length ?: number , encoding ?: string ) : number ;
@@ -1259,17 +1286,6 @@ namespace ts {
12591286 } ;
12601287 }
12611288
1262- function recursiveCreateDirectory ( directoryPath : string , sys : System ) {
1263- const basePath = getDirectoryPath ( directoryPath ) ;
1264- const shouldCreateParent = basePath !== "" && directoryPath !== basePath && ! sys . directoryExists ( basePath ) ;
1265- if ( shouldCreateParent ) {
1266- recursiveCreateDirectory ( basePath , sys ) ;
1267- }
1268- if ( shouldCreateParent || ! sys . directoryExists ( directoryPath ) ) {
1269- sys . createDirectory ( directoryPath ) ;
1270- }
1271- }
1272-
12731289 let sys : System | undefined ;
12741290 if ( typeof ChakraHost !== "undefined" ) {
12751291 sys = getChakraSystem ( ) ;
@@ -1281,14 +1297,7 @@ namespace ts {
12811297 }
12821298 if ( sys ) {
12831299 // patch writefile to create folder before writing the file
1284- const originalWriteFile = sys . writeFile ;
1285- sys . writeFile = ( path , data , writeBom ) => {
1286- const directoryPath = getDirectoryPath ( normalizeSlashes ( path ) ) ;
1287- if ( directoryPath && ! sys ! . directoryExists ( directoryPath ) ) {
1288- recursiveCreateDirectory ( directoryPath , sys ! ) ;
1289- }
1290- originalWriteFile . call ( sys , path , data , writeBom ) ;
1291- } ;
1300+ patchWriteFileEnsuringDirectory ( sys ) ;
12921301 }
12931302 return sys ! ;
12941303 } ) ( ) ;
0 commit comments