import * as fs from 'fs' ; /**
 * Created by martin on 17.02.2017.
 * Some (a few) simple utils for file operations.
 * Just for convenience.
 */ export class FileUtil { param filename return public static exists ( filename ) { return fs . existsSync ( filename ) ; } param filename param encoding return public static read ( filename , encoding ) { return fs . readFileSync ( filename , encoding ) ; } param filename param newContent param encoding public static replaceContent ( filename , newContent , encoding ) { fs . writeFileSync ( filename , newContent , { encoding : encoding } ) ; } public static copy ( srcFile , destFile ) { const BUF_LENGTH = 64 * 1024 ; const buff = Buffer . alloc ( BUF_LENGTH ) ; const fdr = fs . openSync ( srcFile , 'r' ) ; const fdw = fs . openSync ( destFile , 'w' ) ; let bytesRead = 1 ; let pos = 0 ; while ( bytesRead > 0 ) { bytesRead = fs . readSync ( fdr , buff , 0 , BUF_LENGTH , pos ) ; fs . writeSync ( fdw , buff , 0 , bytesRead ) ; pos += bytesRead ; } fs . closeSync ( fdr ) ; fs . closeSync ( fdw ) ; } param path public static deleteFolderRecursive ( path ) { let files = [ ] ; if ( fs . existsSync ( path ) ) { files = fs . readdirSync ( path ) ; files . forEach ( function ( file ) { const curPath = path + '/' + file ; if ( fs . lstatSync ( curPath ) . isDirectory ( ) ) { FileUtil . deleteFolderRecursive ( curPath ) ; } else { fs . unlinkSync ( curPath ) ; } } ) ; fs . rmdirSync ( path ) ; } } param path public static deleteFolderContentRecursive ( path ) { let files = [ ] ; if ( fs . existsSync ( path ) ) { files = fs . readdirSync ( path ) ; files . forEach ( function ( file ) { const curPath = path + '/' + file ; if ( fs . lstatSync ( curPath ) . isDirectory ( ) ) { FileUtil . deleteFolderRecursive ( curPath ) ; } else { fs . unlinkSync ( curPath ) ; } } ) ; } } param path public static deleteFile ( path ) { fs . unlinkSync ( path ) ; } }