Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,37 @@ This might be useful if you need a single report, but your chosen reporter gener
To merge CTRF reports in a specified directory, use the following command:

```sh
npx ctrf-cli merge <directory>
npx ctrf-cli@0.0.3 merge <directory>
```

Replace `directory` with the path to the directory containing the CTRF reports you want to merge.
Replace `directory` with the path to the directory containing the CTRF reports you want to merge. Your merged report will be saved as `ctrf-report.json` in the same directory by default.

### Options

-o, --output `filename`: Output file name for the merged report. Default is ctrf-report.json.
-o, --output `path`: Output file path for the merged report. Can be a filename (saved in input directory), relative path from current working directory, or absolute path. Default is `ctrf-report.json`.

```sh
npx ctrf-cli merge <directory> --output my-merged-report.json
```
# Save with custom filename in input directory
npx [email protected] merge ./reports --output my-merged-report.json
# Merged report saved to: ./reports/my-merged-report.json

-d, --output-dir `directory`: Output directory for the merged report. Default is the same directory as the input reports.
# Save with relative path from current directory
npx [email protected] merge ./reports --output ./output/merged.json
# Merged report saved to: ./output/merged.json

```sh
npx ctrf-cli merge <directory> --output-dir /path/to/output
# Save to directory with default filename
npx [email protected] merge ./reports --output ./output/
# Merged report saved to: ./output/ctrf-report.json

# Save to absolute path
npx [email protected] merge ./reports --output /tmp/merged.json
# Merged report saved to: /tmp/merged.json
```

-k, --keep-reports: Keep existing reports after merging. By default, the original reports will be deleted after merging.

```sh
npx ctrf-cli merge <directory> --keep-reports
npx ctrf-cli@0.0.3 merge <directory> --keep-reports
```

## Flaky
Expand All @@ -74,7 +82,7 @@ Usage
To output flaky tests, use the following command:

```sh
npx ctrf-cli flaky <file-path>
npx ctrf-cli@0.0.3 flaky <file-path>
```

Replace <file-path> with the path to the CTRF report file you want to analyze.
Expand Down
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ctrf-cli",
"version": "0.0.2",
"version": "0.0.3",
"description": "Various CTRF utilities available from the command line",
"main": "dist/index.js",
"bin": {
Expand Down
6 changes: 4 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ const argv = yargs(hideBin(process.argv))
})
.option('output', {
alias: 'o',
describe: 'Output file name for merged report',
describe: 'Output file path (default: ctrf-report.json)',
type: 'string',
default: 'ctrf-report.json',
})
.option('output-dir', {
alias: 'd',
describe: 'Output directory for merged report',
type: 'string',
hidden: true,
deprecated: 'Use --output with a path instead',
})
.option('keep-reports', {
alias: 'k',
Expand All @@ -35,7 +37,7 @@ const argv = yargs(hideBin(process.argv))
});
},
async (argv) => {
await mergeReports(argv.directory as string, argv.output as string, argv['output-dir'] as string, argv['keep-reports'] as boolean);
await mergeReports(argv.directory as string, argv.output as string, argv['keep-reports'] as boolean, argv['output-dir'] as string);
}
)
.command(
Expand Down
36 changes: 29 additions & 7 deletions src/merge.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
import fs from 'fs';
import path from 'path';

export async function mergeReports(directory: string, output: string, outputDir: string, keepReports: boolean) {
export async function mergeReports(directory: string, output: string, keepReports: boolean, outputDir?: string) {
try {
const directoryPath = path.resolve(directory);
const outputFileName = output;
const resolvedOutputDir = outputDir ? path.resolve(outputDir) : directoryPath;
const outputPath = path.join(resolvedOutputDir, outputFileName);

let outputPath: string;

if (outputDir) {
console.warn('Warning: --output-dir is deprecated. Use --output with a path instead.');
const outputFileName = output;
const resolvedOutputDir = path.resolve(outputDir);
outputPath = path.join(resolvedOutputDir, outputFileName);
} else if (output.includes('/') || output.includes('\\')) {
const resolvedPath = path.resolve(output);

const endsWithSeparator = output.endsWith('/') || output.endsWith('\\');
const isExistingDirectory = fs.existsSync(resolvedPath) && fs.statSync(resolvedPath).isDirectory();
const hasNoExtension = path.extname(output) === '';

if (endsWithSeparator || isExistingDirectory || hasNoExtension) {
outputPath = path.join(resolvedPath, 'ctrf-report.json');
} else {
outputPath = resolvedPath;
}
} else {
outputPath = path.join(directoryPath, output);
}

console.log("Merging CTRF reports...");

Expand Down Expand Up @@ -41,9 +61,10 @@ export async function mergeReports(directory: string, output: string, outputDir:
return;
}

if (!fs.existsSync(resolvedOutputDir)) {
fs.mkdirSync(resolvedOutputDir, { recursive: true });
console.log(`Created output directory: ${resolvedOutputDir}`);
const outputDirPath = path.dirname(outputPath);
if (!fs.existsSync(outputDirPath)) {
fs.mkdirSync(outputDirPath, { recursive: true });
console.log(`Created output directory: ${outputDirPath}`);
}

const mergedReport = ctrfReportFiles
Expand Down Expand Up @@ -76,6 +97,7 @@ export async function mergeReports(directory: string, output: string, outputDir:
fs.writeFileSync(outputPath, JSON.stringify(mergedReport, null, 2));

if (!keepReports) {
const outputFileName = path.basename(outputPath);
ctrfReportFiles.forEach((file) => {
const filePath = path.join(directoryPath, file);
if (file !== outputFileName) {
Expand Down