UNPKG

allure-js-commons

Version:
109 lines 2.99 kB
import { randomUUID } from "node:crypto"; import { closeSync, fsyncSync, mkdirSync, openSync, readSync, renameSync, rmSync, writeFileSync, writeSync } from "node:fs"; import { dirname, join } from "node:path"; import { stringifyEnvInfo } from "../utils/envInfo.js"; var writeJson = (path, data) => { writeFile(path, JSON.stringify(data), "utf-8"); }; var buildTempPath = path => { return join(dirname(path), ".allure-write-".concat(randomUUID(), ".tmp")); }; var removeTempFile = path => { rmSync(path, { force: true }); }; var tryRemoveTempFile = path => { try { removeTempFile(path); } catch (ignored) { void ignored; } }; var writeAndFlushFile = (path, write) => { var fd = openSync(path, "w"); try { write(fd); fsyncSync(fd); } finally { closeSync(fd); } }; var publishFile = (path, writeTempFile) => { var tempPath = buildTempPath(path); try { writeTempFile(tempPath); renameSync(tempPath, path); } catch (error) { tryRemoveTempFile(tempPath); throw error; } removeTempFile(tempPath); }; var writeFile = (path, data, encoding) => { publishFile(path, tempPath => { writeAndFlushFile(tempPath, fd => { writeFileSync(fd, data, encoding); }); }); }; var copyFile = (from, to) => { publishFile(to, tempPath => { var fromFd = openSync(from, "r"); try { writeAndFlushFile(tempPath, toFd => { var buffer = Buffer.allocUnsafe(64 * 1024); var bytesRead = 0; while ((bytesRead = readSync(fromFd, buffer, 0, buffer.length, null)) > 0) { var bytesWritten = 0; while (bytesWritten < bytesRead) { bytesWritten += writeSync(toFd, buffer, bytesWritten, bytesRead - bytesWritten); } } }); } finally { closeSync(fromFd); } }); }; export class FileSystemWriter { constructor(config) { this.config = config; } writeAttachment(distFileName, content) { var path = this.buildPath(distFileName); writeFile(path, content); } writeAttachmentFromPath(distFileName, from) { var to = this.buildPath(distFileName); copyFile(from, to); } writeEnvironmentInfo(info) { var text = stringifyEnvInfo(info); var path = this.buildPath("environment.properties"); writeFile(path, text); } writeCategoriesDefinitions(categories) { var path = this.buildPath("categories.json"); writeJson(path, categories); } writeGroup(result) { var path = this.buildPath("".concat(result.uuid, "-container.json")); writeJson(path, result); } writeResult(result) { var path = this.buildPath("".concat(result.uuid, "-result.json")); writeJson(path, result); } writeGlobals(distFileName, info) { var path = this.buildPath(distFileName); writeJson(path, info); } buildPath(name) { mkdirSync(this.config.resultsDir, { recursive: true }); return join(this.config.resultsDir, name); } } //# sourceMappingURL=FileSystemWriter.js.map