UNPKG

@alphabin/trx

Version:

TRX reporter for Playwright tests with Azure Blob Storage upload support

209 lines (208 loc) 6.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.readJsonFile = readJsonFile; exports.writeJsonFile = writeJsonFile; exports.findGitRoot = findGitRoot; exports.findPackageJson = findPackageJson; exports.directoryExists = directoryExists; exports.fileExists = fileExists; exports.getDirectorySize = getDirectorySize; exports.listFilesRecursive = listFilesRecursive; exports.ensureDirectory = ensureDirectory; exports.getFileStats = getFileStats; exports.isPathWithin = isPathWithin; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const logger_util_1 = __importDefault(require("./logger.util")); /** * Reads a JSON file and parses it */ function readJsonFile(filePath) { try { const fullPath = path_1.default.resolve(filePath); logger_util_1.default.debug(`Reading JSON file: ${fullPath}`); if (!fs_1.default.existsSync(fullPath)) { logger_util_1.default.debug(`File not found: ${fullPath}`); return null; } const content = fs_1.default.readFileSync(fullPath, 'utf8'); return JSON.parse(content); } catch (error) { logger_util_1.default.error(`Error reading JSON file: ${filePath}`, error); return null; } } /** * Writes an object to a JSON file */ function writeJsonFile(filePath, data) { try { const fullPath = path_1.default.resolve(filePath); logger_util_1.default.debug(`Writing JSON file: ${fullPath}`); const dirPath = path_1.default.dirname(fullPath); if (!fs_1.default.existsSync(dirPath)) { fs_1.default.mkdirSync(dirPath, { recursive: true }); } fs_1.default.writeFileSync(fullPath, JSON.stringify(data, null, 2), 'utf8'); return true; } catch (error) { logger_util_1.default.error(`Error writing JSON file: ${filePath}`, error); return false; } } /** * Finds the git root directory by searching for .git */ function findGitRoot(startDir = process.cwd()) { let currentDir = startDir; while (currentDir !== path_1.default.parse(currentDir).root) { if (fs_1.default.existsSync(path_1.default.join(currentDir, '.git'))) { return currentDir; } currentDir = path_1.default.dirname(currentDir); } return null; } /** * Gets package.json from the nearest directory */ function findPackageJson(startDir = process.cwd()) { try { let currentDir = startDir; while (currentDir !== path_1.default.parse(currentDir).root) { const packagePath = path_1.default.join(currentDir, 'package.json'); if (fs_1.default.existsSync(packagePath)) { return readJsonFile(packagePath); } currentDir = path_1.default.dirname(currentDir); } return null; } catch (error) { logger_util_1.default.error('Error finding package.json', error); return null; } } /** * Checks if a directory exists */ function directoryExists(dirPath) { try { const stats = fs_1.default.statSync(dirPath); return stats.isDirectory(); } catch { return false; } } /** * Checks if a file exists */ function fileExists(filePath) { try { const stats = fs_1.default.statSync(filePath); return stats.isFile(); } catch { return false; } } /** * Gets directory size recursively */ function getDirectorySize(dirPath) { try { let totalSize = 0; function calculateSize(currentPath) { const items = fs_1.default.readdirSync(currentPath); for (const item of items) { const fullPath = path_1.default.join(currentPath, item); const stats = fs_1.default.statSync(fullPath); if (stats.isDirectory()) { calculateSize(fullPath); } else { totalSize += stats.size; } } } calculateSize(dirPath); return totalSize; } catch (error) { logger_util_1.default.debug(`Error calculating directory size for ${dirPath}`, error); return 0; } } /** * Lists files in directory recursively */ function listFilesRecursive(dirPath, extensions) { const files = []; try { function scanDirectory(currentPath) { const items = fs_1.default.readdirSync(currentPath); for (const item of items) { const fullPath = path_1.default.join(currentPath, item); const stats = fs_1.default.statSync(fullPath); if (stats.isDirectory()) { scanDirectory(fullPath); } else { if (!extensions || extensions.length === 0) { files.push(fullPath); } else { const ext = path_1.default.extname(item).toLowerCase().substring(1); if (extensions.includes(ext)) { files.push(fullPath); } } } } } scanDirectory(dirPath); } catch (error) { logger_util_1.default.debug(`Error listing files in ${dirPath}`, error); } return files; } /** * Creates a directory if it doesn't exist */ function ensureDirectory(dirPath) { try { if (!fs_1.default.existsSync(dirPath)) { fs_1.default.mkdirSync(dirPath, { recursive: true }); } return true; } catch (error) { logger_util_1.default.error(`Error creating directory ${dirPath}`, error); return false; } } /** * Gets file stats safely */ function getFileStats(filePath) { try { return fs_1.default.statSync(filePath); } catch { return null; } } /** * Checks if path is within another path (prevents directory traversal) */ function isPathWithin(childPath, parentPath) { const relative = path_1.default.relative(parentPath, childPath); return !relative.startsWith('..') && !path_1.default.isAbsolute(relative); }