|
| 1 | +const chalk = require('chalk'); |
| 2 | + |
| 3 | +const cliReporter = require('../reporters/cliReporter'); |
| 4 | +const markdownReporter = require('../reporters/markdownReporter'); |
| 5 | +const collectLocalReport = require('../utils/collectLocalReport'); |
| 6 | +const { compareResultsInReports } = require('../utils/compareResultsInReports'); |
| 7 | +const getRemoteReport = require('../utils/getRemoteReport'); |
| 8 | +const { hrToSeconds } = require('../utils/helpers'); |
| 9 | + |
| 10 | +/** |
| 11 | + * @param {typeof import('../index') & { branch: string, output: 'cli' | 'markdown' }} options |
| 12 | + */ |
| 13 | +async function compareReports(options) { |
| 14 | + const { branch, output, quiet } = options; |
| 15 | + const startTime = process.hrtime(); |
| 16 | + |
| 17 | + const localReportStartTime = process.hrtime(); |
| 18 | + const localReport = await collectLocalReport(); |
| 19 | + |
| 20 | + if (!quiet) { |
| 21 | + console.log( |
| 22 | + [chalk.blue('[i]'), `Local report prepared in ${hrToSeconds(process.hrtime(localReportStartTime))}`].join(' '), |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + const remoteReportStartTime = process.hrtime(); |
| 27 | + const { commitSHA, remoteReport } = await getRemoteReport(branch); |
| 28 | + |
| 29 | + if (!quiet) { |
| 30 | + if (commitSHA === '') { |
| 31 | + console.log([chalk.blue('[i]'), `Remote report for "${branch}" branch was not found`].join(' ')); |
| 32 | + } else { |
| 33 | + console.log( |
| 34 | + [ |
| 35 | + chalk.blue('[i]'), |
| 36 | + `Remote report for "${commitSHA}" commit fetched in ${hrToSeconds(process.hrtime(remoteReportStartTime))}`, |
| 37 | + ].join(' '), |
| 38 | + ); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + const result = compareResultsInReports(localReport, remoteReport); |
| 43 | + |
| 44 | + switch (output) { |
| 45 | + case 'cli': |
| 46 | + await cliReporter(result); |
| 47 | + break; |
| 48 | + case 'markdown': |
| 49 | + await markdownReporter(result, commitSHA, quiet); |
| 50 | + break; |
| 51 | + } |
| 52 | + |
| 53 | + if (!quiet) { |
| 54 | + console.log(`Completed in ${hrToSeconds(process.hrtime(startTime))}`); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// --- |
| 59 | + |
| 60 | +/** @type {import('yargs').CommandModule} */ |
| 61 | +const api = { |
| 62 | + command: 'compare-reports', |
| 63 | + describe: 'compares local and remote results', |
| 64 | + builder: { |
| 65 | + branch: { |
| 66 | + alias: 'b', |
| 67 | + type: 'string', |
| 68 | + description: 'A branch to compare against', |
| 69 | + default: 'main', |
| 70 | + }, |
| 71 | + output: { |
| 72 | + alias: 'o', |
| 73 | + type: 'string', |
| 74 | + choices: ['cli', 'markdown'], |
| 75 | + description: 'Defines a reporter to produce output', |
| 76 | + default: 'cli', |
| 77 | + }, |
| 78 | + }, |
| 79 | + handler: compareReports, |
| 80 | +}; |
| 81 | + |
| 82 | +module.exports = api; |
0 commit comments