UNPKG

@serenity-js/core

Version:

The core Serenity/JS framework, providing the Screenplay Pattern interfaces, as well as the test reporting and integration infrastructure

88 lines 3.83 kB
import * as nodeOS from 'node:os'; import gracefulFS from 'graceful-fs'; import { Path } from './Path.js'; export class FileSystem { root; fs; os; directoryMode; constructor(root, fs = gracefulFS, os = nodeOS, directoryMode = Number.parseInt('0777', 8) & (~process.umask())) { this.root = root; this.fs = fs; this.os = os; this.directoryMode = directoryMode; } resolve(relativeOrAbsolutePath) { return this.root.resolve(relativeOrAbsolutePath); } async store(relativeOrAbsolutePathToFile, data, encoding) { await this.ensureDirectoryExistsAt(relativeOrAbsolutePathToFile.directory()); return this.writeFile(relativeOrAbsolutePathToFile, data, encoding); } readFile(relativeOrAbsolutePathToFile, options) { return this.fs.promises.readFile(this.resolve(relativeOrAbsolutePathToFile).value, options); } readFileSync(relativeOrAbsolutePathToFile, options) { return this.fs.readFileSync(this.resolve(relativeOrAbsolutePathToFile).value, options); } async writeFile(relativeOrAbsolutePathToFile, data, options) { const resolvedPath = this.resolve(relativeOrAbsolutePathToFile); await this.fs.promises.writeFile(resolvedPath.value, data, options); return resolvedPath; } writeFileSync(relativeOrAbsolutePathToFile, data, options) { const resolvedPath = this.resolve(relativeOrAbsolutePathToFile); this.fs.writeFileSync(resolvedPath.value, data, options); return resolvedPath; } createReadStream(relativeOrAbsolutePathToFile) { return this.fs.createReadStream(this.resolve(relativeOrAbsolutePathToFile).value); } createWriteStreamTo(relativeOrAbsolutePathToFile) { return this.fs.createWriteStream(this.resolve(relativeOrAbsolutePathToFile).value); } stat(relativeOrAbsolutePathToFile) { return this.fs.promises.stat(this.resolve(relativeOrAbsolutePathToFile).value); } exists(relativeOrAbsolutePathToFile) { return this.fs.existsSync(this.resolve(relativeOrAbsolutePathToFile).value); } async remove(relativeOrAbsolutePathToFileOrDirectory) { try { const absolutePath = this.resolve(relativeOrAbsolutePathToFileOrDirectory); const stat = await this.stat(relativeOrAbsolutePathToFileOrDirectory); if (stat.isFile()) { await this.fs.promises.unlink(absolutePath.value); } else { const entries = await this.fs.promises.readdir(absolutePath.value); for (const entry of entries) { await this.remove(absolutePath.join(new Path(entry))); } await this.fs.promises.rmdir(absolutePath.value); } } catch (error) { if (error?.code === 'ENOENT') { return void 0; } throw error; } } async ensureDirectoryExistsAt(relativeOrAbsolutePathToDirectory) { const absolutePath = this.resolve(relativeOrAbsolutePathToDirectory); await this.fs.promises.mkdir(absolutePath.value, { recursive: true, mode: this.directoryMode }); return absolutePath; } rename(source, destination) { return this.fs.promises.rename(source.value, destination.value); } temporaryDirectory(prefix) { const osTemporaryDirectory = Path.from(this.os.tmpdir()); const subDirectory = Path.from(prefix || '/'); const desiredPath = osTemporaryDirectory.join(subDirectory); const customTemporaryDirectory = this.fs.realpathSync(this.fs.mkdtempSync(desiredPath.value, { encoding: 'utf8' })); return Path.from(customTemporaryDirectory); } } //# sourceMappingURL=FileSystem.js.map