import { errors } from "../errors" ; import { runtime , RuntimeDirEntry } from "../runtimes" ; import { FileSystemHost } from "./FileSystemHost" ; import { FileUtils } from "./FileUtils" ; const fs = runtime . fs ; /** An implementation of a file host that interacts with the actual file system. */ export class RealFileSystemHost implements FileSystemHost { inheritdoc async delete ( path ) { try { await fs . delete ( path ) ; } catch ( err ) { throw this . getFileNotFoundErrorIfNecessary ( err , path ) ; } } inheritdoc deleteSync ( path ) { try { fs . deleteSync ( path ) ; } catch ( err ) { throw this . getFileNotFoundErrorIfNecessary ( err , path ) ; } } inheritdoc readDirSync ( dirPath ) { try { const entries = fs . readDirSync ( dirPath ) ; for ( const entry of entries ) entry . name = FileUtils . pathJoin ( dirPath , entry . name ) ; return entries ; } catch ( err ) { throw this . getDirectoryNotFoundErrorIfNecessary ( err , dirPath ) ; } } inheritdoc async readFile ( filePath , encoding = "utf-8" ) { try { return await fs . readFile ( filePath , encoding ) ; } catch ( err ) { throw this . getFileNotFoundErrorIfNecessary ( err , filePath ) ; } } inheritdoc readFileSync ( filePath , encoding = "utf-8" ) { try { return fs . readFileSync ( filePath , encoding ) ; } catch ( err ) { throw this . getFileNotFoundErrorIfNecessary ( err , filePath ) ; } } inheritdoc async writeFile ( filePath , fileText ) { return fs . writeFile ( filePath , fileText ) ; } inheritdoc writeFileSync ( filePath , fileText ) { fs . writeFileSync ( filePath , fileText ) ; } inheritdoc mkdir ( dirPath ) { return fs . mkdir ( dirPath ) ; } inheritdoc mkdirSync ( dirPath ) { fs . mkdirSync ( dirPath ) ; } inheritdoc move ( srcPath , destPath ) { return fs . move ( srcPath , destPath ) ; } inheritdoc moveSync ( srcPath , destPath ) { fs . moveSync ( srcPath , destPath ) ; } inheritdoc copy ( srcPath , destPath ) { return fs . copy ( srcPath , destPath ) ; } inheritdoc copySync ( srcPath , destPath ) { fs . copySync ( srcPath , destPath ) ; } inheritdoc fileExists ( filePath ) { return fs . fileExists ( filePath ) ; } inheritdoc fileExistsSync ( filePath ) { return fs . fileExistsSync ( filePath ) ; } inheritdoc directoryExists ( dirPath ) { return fs . directoryExists ( dirPath ) ; } inheritdoc directoryExistsSync ( dirPath ) { return fs . directoryExistsSync ( dirPath ) ; } inheritdoc realpathSync ( path ) { return fs . realpathSync ( path ) ; } inheritdoc getCurrentDirectory ( ) { return FileUtils . standardizeSlashes ( fs . getCurrentDirectory ( ) ) ; } inheritdoc glob ( patterns ) { return fs . glob ( backSlashesToForward ( patterns ) ) ; } inheritdoc globSync ( patterns ) { return fs . globSync ( backSlashesToForward ( patterns ) ) ; } inheritdoc isCaseSensitive ( ) { return fs . isCaseSensitive ( ) ; } private getDirectoryNotFoundErrorIfNecessary ( err , path ) { return FileUtils . isNotExistsError ( err ) ? new errors . DirectoryNotFoundError ( FileUtils . getStandardizedAbsolutePath ( this , path ) ) : err ; } private getFileNotFoundErrorIfNecessary ( err , path ) { return FileUtils . isNotExistsError ( err ) ? new errors . FileNotFoundError ( FileUtils . getStandardizedAbsolutePath ( this , path ) ) : err ; } } function backSlashesToForward ( patterns ) { return patterns . map ( p => p . replace ( /\\/g , "/" ) ) ; }