UNPKG

@argos-ci/util

Version:
104 lines (103 loc) 3.2 kB
import { n as getScreenshotName, r as getMetadataPath, t as validateThreshold } from "./threshold-BxFlev7-.mjs"; import { randomBytes } from "node:crypto"; import { access, mkdir, readFile, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { exec } from "node:child_process"; //#region src/fs.ts const createDirectoryPromises = /* @__PURE__ */ new Map(); /** * Create a directory if it doesn't exist. */ async function createDirectory(pathname) { let promise = createDirectoryPromises.get(pathname); if (promise) return promise; promise = mkdir(pathname, { recursive: true }).then(() => {}); createDirectoryPromises.set(pathname, promise); return promise; } /** * Create temporary directory. */ async function createTemporaryDirectory() { const path = join(tmpdir(), "argos." + randomBytes(16).toString("hex")); await createDirectory(path); return path; } //#endregion //#region src/git.ts let cached; /** * Get the top level of the git repository. */ function getGitRepositoryPath() { if (!cached) cached = new Promise((resolve) => { exec("git rev-parse --show-toplevel", (error, stdout, stderr) => { if (error) resolve(null); if (stderr) resolve(null); resolve(stdout.trim()); }); }); return cached; } //#endregion //#region src/introspection.ts const versionCache = /* @__PURE__ */ new Map(); /** * Read the version from a package.json file. */ function readVersionFromPackage(pkgPath) { const readVersion = async () => { const { version } = JSON.parse(await readFile(pkgPath, "utf-8")); if (typeof version !== "string") throw new Error("Invalid version"); return version; }; if (!versionCache.has(pkgPath)) versionCache.set(pkgPath, readVersion()); const fromCache = versionCache.get(pkgPath); if (!fromCache) throw new Error("Invariant violation: version not in cache"); return fromCache; } //#endregion //#region src/metadata-io.ts /** * Write screenshot metadata to disk. */ async function writeMetadata(screenshotPath, metadata) { await writeFile(getMetadataPath(screenshotPath), JSON.stringify(metadata)); } /** * Read screenshot metadata from disk. * If not found, returns null. */ async function readMetadata(screenshotPath) { try { const metadata = await readFile(getMetadataPath(screenshotPath), "utf8"); return JSON.parse(metadata); } catch (error) { if (error instanceof Error && "code" in error && error.code === "ENOENT") return null; throw new Error("Failed to read metadata", { cause: error }); } } //#endregion //#region src/playwright-trace.ts /** * Get trace path from screenshot path. */ function getTracePath(screenshotPath) { return screenshotPath + ".pw-trace.zip"; } /** * Get playwright trace from screenshot path. * If not found, returns null. */ async function getPlaywrightTracePath(screenshotPath) { try { const tracePath = getTracePath(screenshotPath); await access(tracePath); return tracePath; } catch { return null; } } //#endregion export { createDirectory, createTemporaryDirectory, getGitRepositoryPath, getMetadataPath, getPlaywrightTracePath, getScreenshotName, readMetadata, readVersionFromPackage, validateThreshold, writeMetadata };