'use strict' ; import * as fs from 'fs' ; import * as fse from 'fs-extra' ; import { inject , injectable } from 'inversify' ; import * as path from 'path' ; import { IFileSystem , IPlatformService } from './types' ; @ injectable ( ) export class FileSystem implements IFileSystem { constructor ( @ inject ( IPlatformService ) private platformService ) { } public get directorySeparatorChar ( ) : string { return path . sep ; } public objectExistsAsync ( filePath , statCheck ) { return new Promise < boolean > ( resolve => { fse . stat ( filePath , ( error , stats ) => { if ( error ) { return resolve ( false ) ; } return resolve ( statCheck ( stats ) ) ; } ) ; } ) ; } public fileExistsAsync ( filePath ) { return this . objectExistsAsync ( filePath , stats => stats . isFile ( ) ) ; } public directoryExistsAsync ( filePath ) { return this . objectExistsAsync ( filePath , stats => stats . isDirectory ( ) ) ; } public createDirectoryAsync ( directoryPath ) { return fse . mkdirp ( directoryPath ) ; } public getSubDirectoriesAsync ( 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 ) { const path1ToCompare = path . normalize ( path1 ) ; const path2ToCompare = path . normalize ( path2 ) ; if ( this . platformService . isWindows ) { return path1ToCompare . toUpperCase ( ) === path2ToCompare . toUpperCase ( ) ; } else { return path1ToCompare === path2ToCompare ; } } }