import { Injectable , NgZone } from '@angular/core' ; import { copy , CopyOptions , ensureDir , move , pathExists , readdir , readFile , readJson , remove , writeFile , writeJson , } from 'fs-extra' ; import { from , Observable } from 'rxjs' ; import { enterZone } from '../../libs/rx' ; @ Injectable ( ) export class FsService { constructor ( private ngZone ) { } isPathExists ( path ) { return from ( pathExists ( path ) ) . pipe ( enterZone ( this . ngZone ) ) ; } readFile ( fileName ) { return from ( readFile ( fileName , 'utf8' ) ) . pipe ( enterZone ( this . ngZone ) ) ; } param { string } fileName readJsonFile < T > ( fileName ) { return from ( readJson ( fileName , { throws : false } ) ) . pipe ( enterZone ( this . ngZone ) ) ; } readDirectory ( dirName ) { return from ( readdir ( dirName ) ) . pipe ( enterZone ( this . ngZone ) ) ; } writeFile ( fileName , value ) { return from ( writeFile ( fileName , value , 'utf8' ) ) . pipe ( enterZone ( this . ngZone ) ) ; } writeJsonFile < T > ( fileName , value ) { return from ( writeJson ( fileName , value ) ) . pipe ( enterZone ( this . ngZone ) ) ; } copyFile ( src , dest , options ? ) { return from ( copy ( src , dest , options ) ) . pipe ( enterZone ( this . ngZone ) ) ; } ensureDirectory ( dirName ) { return from ( ensureDir ( dirName ) ) . pipe ( enterZone ( this . ngZone ) ) ; } renameFile ( oldFileName , newFileName ) { return from ( move ( oldFileName , newFileName , { overwrite : false } ) ) . pipe ( enterZone ( this . ngZone ) ) ; } removeFile ( fileName ) { return from ( remove ( fileName ) ) . pipe ( enterZone ( this . ngZone ) ) ; } }