| import { CtrfReport } from "../../types/ctrf"; | ||
| /** | ||
| * Merges multiple CTRF reports into a single report. | ||
| * | ||
| * @param reports Array of CTRF report objects to be merged. | ||
| * @returns The merged CTRF report object. | ||
| */ | ||
| export declare function mergeReports(reports: CtrfReport[]): CtrfReport; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.mergeReports = void 0; | ||
| /** | ||
| * Merges multiple CTRF reports into a single report. | ||
| * | ||
| * @param reports Array of CTRF report objects to be merged. | ||
| * @returns The merged CTRF report object. | ||
| */ | ||
| function mergeReports(reports) { | ||
| if (!reports || reports.length === 0) { | ||
| throw new Error('No reports provided for merging.'); | ||
| } | ||
| const mergedReport = { | ||
| results: { | ||
| tool: reports[0].results.tool, | ||
| summary: initializeEmptySummary(), | ||
| tests: [], | ||
| }, | ||
| }; | ||
| reports.forEach((report) => { | ||
| const { summary, tests, environment, extra } = report.results; | ||
| mergedReport.results.summary.tests += summary.tests; | ||
| mergedReport.results.summary.passed += summary.passed; | ||
| mergedReport.results.summary.failed += summary.failed; | ||
| mergedReport.results.summary.skipped += summary.skipped; | ||
| mergedReport.results.summary.pending += summary.pending; | ||
| mergedReport.results.summary.other += summary.other; | ||
| if (summary.suites !== undefined) { | ||
| mergedReport.results.summary.suites = | ||
| (mergedReport.results.summary.suites || 0) + summary.suites; | ||
| } | ||
| mergedReport.results.summary.start = Math.min(mergedReport.results.summary.start, summary.start); | ||
| mergedReport.results.summary.stop = Math.max(mergedReport.results.summary.stop, summary.stop); | ||
| mergedReport.results.tests.push(...tests); | ||
| if (environment) { | ||
| mergedReport.results.environment = Object.assign(Object.assign({}, mergedReport.results.environment), environment); | ||
| } | ||
| if (extra) { | ||
| mergedReport.results.extra = Object.assign(Object.assign({}, mergedReport.results.extra), extra); | ||
| } | ||
| }); | ||
| return mergedReport; | ||
| } | ||
| exports.mergeReports = mergeReports; | ||
| /** | ||
| * Initializes an empty summary object. | ||
| * | ||
| * @returns An empty Summary object. | ||
| */ | ||
| function initializeEmptySummary() { | ||
| return { | ||
| tests: 0, | ||
| passed: 0, | ||
| failed: 0, | ||
| skipped: 0, | ||
| pending: 0, | ||
| other: 0, | ||
| start: Number.MAX_SAFE_INTEGER, | ||
| stop: 0, | ||
| }; | ||
| } |
| import { CtrfReport } from '../../types/ctrf'; | ||
| /** | ||
| * Reads a single CTRF report file from a specified path. | ||
| * | ||
| * @param filePath Path to the JSON file containing the CTRF report. | ||
| * @returns The parsed `CtrfReport` object. | ||
| * @throws If the file does not exist, is not a valid JSON, or does not conform to the `CtrfReport` structure. | ||
| */ | ||
| export declare function readSingleReport(filePath: string): CtrfReport; | ||
| /** | ||
| * Reads all CTRF report files from a given directory. | ||
| * | ||
| * @param directory Path to the directory containing JSON files. | ||
| * @returns An array of parsed `CtrfReport` objects. | ||
| * @throws If the directory does not exist or no valid CTRF reports are found. | ||
| */ | ||
| export declare function readReportsFromDirectory(directoryPath: string): CtrfReport[]; | ||
| /** | ||
| * Reads all CTRF report files matching a glob pattern. | ||
| * | ||
| * @param pattern The glob pattern to match files (e.g., ctrf/*.json). | ||
| * @returns An array of parsed `CtrfReport` objects. | ||
| * @throws If no valid CTRF reports are found. | ||
| */ | ||
| export declare function readReportsFromGlobPattern(pattern: string): CtrfReport[]; |
| "use strict"; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.readReportsFromGlobPattern = exports.readReportsFromDirectory = exports.readSingleReport = void 0; | ||
| const fs_1 = __importDefault(require("fs")); | ||
| const path_1 = __importDefault(require("path")); | ||
| const glob_1 = require("glob"); | ||
| /** | ||
| * Reads a single CTRF report file from a specified path. | ||
| * | ||
| * @param filePath Path to the JSON file containing the CTRF report. | ||
| * @returns The parsed `CtrfReport` object. | ||
| * @throws If the file does not exist, is not a valid JSON, or does not conform to the `CtrfReport` structure. | ||
| */ | ||
| function readSingleReport(filePath) { | ||
| if (!fs_1.default.existsSync(filePath)) { | ||
| throw new Error(`JSON file not found: ${filePath}`); | ||
| } | ||
| const resolvedPath = path_1.default.resolve(filePath); | ||
| if (!fs_1.default.existsSync(resolvedPath)) { | ||
| throw new Error(`The file '${resolvedPath}' does not exist.`); | ||
| } | ||
| try { | ||
| const content = fs_1.default.readFileSync(resolvedPath, 'utf8'); | ||
| const parsed = JSON.parse(content); | ||
| if (!isCtrfReport(parsed)) { | ||
| throw new Error(`The file '${resolvedPath}' is not a valid CTRF report.`); | ||
| } | ||
| return parsed; | ||
| } | ||
| catch (error) { | ||
| const errorMessage = error.message || 'Unknown error'; | ||
| throw new Error(`Failed to read or parse the file '${resolvedPath}': ${errorMessage}`); | ||
| } | ||
| } | ||
| exports.readSingleReport = readSingleReport; | ||
| /** | ||
| * Reads all CTRF report files from a given directory. | ||
| * | ||
| * @param directory Path to the directory containing JSON files. | ||
| * @returns An array of parsed `CtrfReport` objects. | ||
| * @throws If the directory does not exist or no valid CTRF reports are found. | ||
| */ | ||
| function readReportsFromDirectory(directoryPath) { | ||
| directoryPath = path_1.default.resolve(directoryPath); | ||
| if (!fs_1.default.existsSync(directoryPath)) { | ||
| throw new Error(`The directory '${directoryPath}' does not exist.`); | ||
| } | ||
| const files = fs_1.default.readdirSync(directoryPath); | ||
| const reports = files | ||
| .filter((file) => path_1.default.extname(file) === '.json') | ||
| .map((file) => { | ||
| const filePath = path_1.default.join(directoryPath, file); | ||
| try { | ||
| const content = fs_1.default.readFileSync(filePath, 'utf8'); | ||
| const parsed = JSON.parse(content); | ||
| if (!isCtrfReport(parsed)) { | ||
| console.warn(`Skipping invalid CTRF report file: ${file}`); | ||
| return null; | ||
| } | ||
| return parsed; | ||
| } | ||
| catch (error) { | ||
| console.warn(`Failed to read or parse file '${file}':`, error); | ||
| return null; | ||
| } | ||
| }) | ||
| .filter((report) => report !== null); | ||
| if (reports.length === 0) { | ||
| throw new Error(`No valid CTRF reports found in the directory '${directoryPath}'.`); | ||
| } | ||
| return reports; | ||
| } | ||
| exports.readReportsFromDirectory = readReportsFromDirectory; | ||
| /** | ||
| * Reads all CTRF report files matching a glob pattern. | ||
| * | ||
| * @param pattern The glob pattern to match files (e.g., ctrf/*.json). | ||
| * @returns An array of parsed `CtrfReport` objects. | ||
| * @throws If no valid CTRF reports are found. | ||
| */ | ||
| function readReportsFromGlobPattern(pattern) { | ||
| const files = glob_1.glob.sync(pattern); | ||
| if (files.length === 0) { | ||
| throw new Error(`No files found matching the pattern '${pattern}'.`); | ||
| } | ||
| const reports = files | ||
| .map((file) => { | ||
| try { | ||
| const content = fs_1.default.readFileSync(file, 'utf8'); | ||
| const parsed = JSON.parse(content); | ||
| if (!isCtrfReport(parsed)) { | ||
| console.warn(`Skipping invalid CTRF report file: ${file}`); | ||
| return null; | ||
| } | ||
| return parsed; | ||
| } | ||
| catch (error) { | ||
| console.warn(`Failed to read or parse file '${file}':`, error); | ||
| return null; | ||
| } | ||
| }) | ||
| .filter((report) => report !== null); | ||
| if (reports.length === 0) { | ||
| throw new Error(`No valid CTRF reports found matching the pattern '${pattern}'.`); | ||
| } | ||
| return reports; | ||
| } | ||
| exports.readReportsFromGlobPattern = readReportsFromGlobPattern; | ||
| /** | ||
| * Checks if an object conforms to the `CtrfReport` structure. | ||
| * | ||
| * @param obj The object to validate. | ||
| * @returns `true` if the object matches the `CtrfReport` type; otherwise, `false`. | ||
| */ | ||
| function isCtrfReport(obj) { | ||
| return (obj && | ||
| typeof obj === 'object' && | ||
| obj.results && | ||
| Array.isArray(obj.results.tests) && | ||
| typeof obj.results.summary === 'object' && | ||
| typeof obj.results.tool === 'object'); | ||
| } |
| import { CtrfReport } from "../../types/ctrf"; | ||
| export declare function writeReportToFile(report: CtrfReport, path: string): void; | ||
| export declare function removeMergedReports(paths: string[]): void; |
| "use strict"; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.removeMergedReports = exports.writeReportToFile = void 0; | ||
| const fs_1 = __importDefault(require("fs")); | ||
| function writeReportToFile(report, path) { | ||
| if (!fs_1.default.existsSync(path)) { | ||
| fs_1.default.mkdirSync(path, { recursive: true }); | ||
| console.log(`Created output directory: ${path}`); | ||
| } | ||
| fs_1.default.writeFileSync(path, JSON.stringify(report, null, 2)); | ||
| } | ||
| exports.writeReportToFile = writeReportToFile; | ||
| function removeMergedReports(paths) { | ||
| paths.forEach((path) => { | ||
| fs_1.default.unlinkSync(path); | ||
| }); | ||
| } | ||
| exports.removeMergedReports = removeMergedReports; |
+3
-1
| #!/usr/bin/env node | ||
| export {}; | ||
| import { readReportsFromDirectory, readReportsFromGlobPattern } from './methods/read-reports'; | ||
| import { mergeReports } from './methods/merge-reports'; | ||
| export { mergeReports, readReportsFromDirectory, readReportsFromGlobPattern }; |
+6
-0
@@ -16,2 +16,3 @@ #!/usr/bin/env node | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.readReportsFromGlobPattern = exports.readReportsFromDirectory = exports.mergeReports = void 0; | ||
| const yargs_1 = __importDefault(require("yargs/yargs")); | ||
@@ -21,2 +22,7 @@ const helpers_1 = require("yargs/helpers"); | ||
| const flaky_1 = require("./flaky"); | ||
| const read_reports_1 = require("./methods/read-reports"); | ||
| Object.defineProperty(exports, "readReportsFromDirectory", { enumerable: true, get: function () { return read_reports_1.readReportsFromDirectory; } }); | ||
| Object.defineProperty(exports, "readReportsFromGlobPattern", { enumerable: true, get: function () { return read_reports_1.readReportsFromGlobPattern; } }); | ||
| const merge_reports_1 = require("./methods/merge-reports"); | ||
| Object.defineProperty(exports, "mergeReports", { enumerable: true, get: function () { return merge_reports_1.mergeReports; } }); | ||
| const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv)) | ||
@@ -23,0 +29,0 @@ .command('merge <directory>', 'Merge CTRF reports into a single report', (yargs) => { |
+2
-1
| { | ||
| "name": "ctrf", | ||
| "version": "0.0.10", | ||
| "version": "0.0.11", | ||
| "description": "", | ||
@@ -19,2 +19,3 @@ "main": "index.js", | ||
| "dependencies": { | ||
| "glob": "^11.0.1", | ||
| "typescript": "^5.4.5", | ||
@@ -21,0 +22,0 @@ "yargs": "^17.7.2" |
+18
-3
| # CTRF CLI | ||
| Various CTRF utilities available by command line | ||
| Various CTRF utilities available programatically and by command line | ||
@@ -11,3 +11,3 @@ ## Help us grow CTRF | ||
| ## Utilities | ||
| ## Command Line Utilities | ||
@@ -19,3 +19,2 @@ | Name |Details | | ||
| ## Merge | ||
@@ -76,2 +75,18 @@ | ||
| ## Programmatic Methods | ||
| ```sh | ||
| npm install ctrf | ||
| ``` | ||
| The following programmatic methods are available: | ||
| `mergeReports` - This method merges multiple CTRF reports into a single report. | ||
| `readSingleReport` - Reads and parses a single CTRF report file from a specified file path. | ||
| `readReportsFromDirectory` - Reads all CTRF report files from a given directory. | ||
| `readReportsFromGlobPattern` - Reads all CTRF report files from a given glob pattern. | ||
| ## What is CTRF? | ||
@@ -78,0 +93,0 @@ |
25372
73.44%15
66.67%457
122.93%102
17.24%3
50%5
66.67%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added