Skip to content

Commit

Permalink
Add a badge reporter
Browse files Browse the repository at this point in the history
Addresses: rpl#129
  • Loading branch information
runarberg committed Nov 15, 2017
1 parent d4963be commit 9d7eb92
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"dependencies": {
"array.prototype.find": "2.0.4",
"babel-runtime": "6.23.0",
"badge-up": "2.3.0",
"flow-annotation-check": "1.3.1",
"glob": "7.1.1",
"minimatch": "3.0.4",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function processArgv(argv) {
.option('type', {
alias: 't',
type: 'choice',
choices: ['html', 'json', 'text'],
choices: ['html', 'json', 'text', 'badge'],
describe: `format of the generated reports (defaults to "${defaultConfig.type.join(', ')}")`
})
// --project-dir "/project/dir/path"
Expand Down
7 changes: 6 additions & 1 deletion src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import path from 'path';
import {collectFlowCoverage} from './flow';
import {withTmpDir} from './promisified';
import reportHTML from './report-html';
import reportBadge from './report-badge';
import reportJSON from './report-json';
import reportText from './report-text';

import type {FlowCoverageSummaryData} from './flow'; // eslint-disable-line no-duplicate-imports

export type FlowCoverageReportType = 'json' | 'text' | 'html';
export type FlowCoverageReportType = 'json' | 'text' | 'badge' |'html';

export type FlowCoverageReportOptions = {
projectDir: string,
Expand Down Expand Up @@ -94,6 +95,10 @@ export default async function generateFlowCoverageReport(opts: FlowCoverageRepor
reportResults.push(reportText.generate(coverageData, opts));
}

if (reportTypes.indexOf('badge') >= 0) {
reportResults.push(reportBadge.generate(coverageData, opts));
}

if (reportTypes.indexOf('html') >= 0) {
reportResults.push(reportHTML.generate(coverageData, opts).then(() => {
console.log(`View generated HTML Report at file://${opts.outputDir}/index.html`);
Expand Down
58 changes: 58 additions & 0 deletions src/lib/report-badge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

// @flow

import path from 'path';

import badge from 'badge-up';

import {mkdirp, writeFile} from './promisified';

import type {FlowCoverageSummaryData} from './flow';
import type {FlowCoverageReportOptions} from './index';

function saveBadgeReport(
coverageData: FlowCoverageSummaryData,
opts: FlowCoverageReportOptions
): Promise<void> {
const percent = coverageData.percent;
const threshold = opts.threshold || 80;
const difference = percent - threshold;

let color;

if (difference < -40) {
color = 'red';
} else if (difference < -30) {
color = 'orange';
} else if (difference < -20) {
color = 'yellow';
} else if (difference < -10) {
color = 'yellowgreen';
} else if (difference < 0) {
color = 'green';
} else {
color = 'brightgreen';
}

const badgeGen = () => new Promise((resolve, reject) => {
badge('flow', `${percent}%`, badge.colors[color], (err, svg) => {
if (err) {
reject(err);
} else {
resolve(svg);
}
});
});

const projectDir = opts.projectDir;
const outputDir = opts.outputDir || path.join(projectDir, 'flow-coverage');

return mkdirp(outputDir).then(badgeGen).then(svg => {
return writeFile(path.join(outputDir, 'flow-coverage.svg'), svg);
});
}

export default {
generate: saveBadgeReport
};

0 comments on commit 9d7eb92

Please sign in to comment.