'use strict' ; import * as fs from 'fs-extra' ; import { inject , injectable } from 'inversify' ; import * as path from 'path' ; import { createDeferred } from '../helpers' ; import { IFileSystem , IPlatformService } from './types' ; @ injectable ( ) export class FileSystem implements IFileSystem { constructor ( @ inject ( IPlatformService ) private platformService ) { } public get directorySeparatorChar ( ) : string { return path . sep ; } public objectExists ( filePath , statCheck ) { return new Promise < boolean > ( resolve => { fs . stat ( filePath , ( error , stats ) => { if ( error ) { return resolve ( false ) ; } return resolve ( statCheck ( stats ) ) ; } ) ; } ) ; } public fileExists ( filePath ) { return this . objectExists ( filePath , ( stats ) => stats . isFile ( ) ) ; } public fileExistsSync ( filePath ) { return fs . existsSync ( filePath ) ; } param { string } filePath returns { Promise < string > } memberof public readFile ( filePath ) { return fs . readFile ( filePath ) . then ( buffer => buffer . toString ( ) ) ; } public directoryExists ( filePath ) { return this . objectExists ( filePath , ( stats ) => stats . isDirectory ( ) ) ; } public createDirectory ( directoryPath ) { return fs . mkdirp ( directoryPath ) ; } public getSubDirectories ( rootDir ) { return new Promise < string [ ] > ( resolve => { fs . readdir ( rootDir , ( error , files ) => { if ( error ) { return resolve ( [ ] ) ; } const subDirs = [ ] ; files . forEach ( name => { const fullPath = path . join ( rootDir , name ) ; try { if ( fs . statSync ( fullPath ) . isDirectory ( ) ) { subDirs . push ( fullPath ) ; } } catch ( ex ) { } } ) ; resolve ( subDirs ) ; } ) ; } ) ; } public arePathsSame ( path1 , path2 ) { path1 = path . normalize ( path1 ) ; path2 = path . normalize ( path2 ) ; if ( this . platformService . isWindows ) { return path1 . toUpperCase ( ) === path2 . toUpperCase ( ) ; } else { return path1 === path2 ; } } public appendFileSync ( filename , data , encoding ) ; public appendFileSync ( filename , data , options ? ) ; public appendFileSync ( filename , data , options ? ) ; public appendFileSync ( filename , data , optionsOrEncoding ) { return fs . appendFileSync ( filename , data , optionsOrEncoding ) ; } public getRealPath ( filePath ) { return new Promise < string > ( resolve => { fs . realpath ( filePath , ( err , realPath ) => { resolve ( err ? filePath : realPath ) ; } ) ; } ) ; } public copyFile ( src , dest ) { const deferred = createDeferred < void > ( ) ; const rs = fs . createReadStream ( src ) . on ( 'error' , ( err ) => { deferred . reject ( err ) ; } ) ; const ws = fs . createWriteStream ( dest ) . on ( 'error' , ( err ) => { deferred . reject ( err ) ; } ) . on ( 'close' , ( ) => { deferred . resolve ( ) ; } ) ; rs . pipe ( ws ) ; return deferred . promise ; } public deleteFile ( filename ) { const deferred = createDeferred < void > ( ) ; fs . unlink ( filename , err => err ? deferred . reject ( err ) : deferred . resolve ( ) ) ; return deferred . promise ; } }