From 9d7eb92ac6ff60196dcd69b227ed7fc8c8486609 Mon Sep 17 00:00:00 2001 From: runarberg Date: Wed, 15 Nov 2017 16:22:26 +0000 Subject: [PATCH] Add a badge reporter Addresses: #129 --- package.json | 1 + src/lib/cli/args.js | 2 +- src/lib/index.js | 7 ++++- src/lib/report-badge.js | 58 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/lib/report-badge.js diff --git a/package.json b/package.json index 3f7ee15..1de34fd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/lib/cli/args.js b/src/lib/cli/args.js index 28b1843..ae176d5 100644 --- a/src/lib/cli/args.js +++ b/src/lib/cli/args.js @@ -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" diff --git a/src/lib/index.js b/src/lib/index.js index 6dca161..5aad7fa 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -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, @@ -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`); diff --git a/src/lib/report-badge.js b/src/lib/report-badge.js new file mode 100644 index 0000000..4b5d0d3 --- /dev/null +++ b/src/lib/report-badge.js @@ -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 { + 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 +};