import * as path from 'path' ; import * as fs from 'fs' ; export class FileSystem { existsFolderAsync ( folder ) { return new Promise ( ( resolve ) => { fs . exists ( folder , exists => { resolve ( exists ) ; } ) ; } ) ; } createFolderAsync ( folder ) { return new Promise ( ( resolve , reject ) => { fs . exists ( folder , exists => { if ( exists ) { resolve ( ) ; } else { fs . mkdir ( folder , async ( error ) => { if ( ! error ) { resolve ( ) ; } else if ( error . code === "ENOENT" ) { const parent = path . dirname ( folder ) ; await this . createFolderAsync ( parent ) ; await this . createFolderAsync ( folder ) ; resolve ( ) ; } else { reject ( error ) ; } } ) ; } } ) ; } ) ; } combinePath ( ... pathSegments ) { return path . resolve ( ... pathSegments ) ; } deleteFileAsync ( filename ) { return new Promise ( ( resolve , reject ) => { fs . unlink ( filename , error => { error ? reject ( error ) : resolve ( ) ; } ) ; } ) ; } async readJsonFileAsync < T > ( filename ) { const json = await this . readFileAsync ( filename ) ; return JSON . parse ( json ) as T ; } readFileAsync ( filename ) { return new Promise ( ( resolve , reject ) => { fs . readFile ( filename , ( error , data ) => { error ? reject ( error ) : resolve ( data . toString ( ) ) ; } ) ; } ) ; } writeJsonFileAsync ( filename , data ) { const json = JSON . stringify ( data , null , 4 ) ; return this . writeFileAsync ( filename , json ) ; } writeFileAsync ( filename , data ) { return new Promise ( ( resolve , reject ) => { fs . writeFile ( filename , data , error => { error ? reject ( error ) : resolve ( ) ; } ) ; } ) ; } }