jest-coverage-badges
Advanced tools
Comparing version 1.0.0 to 1.1.2
83
cli.js
#!/usr/bin/env node | ||
const { get } = require('https') | ||
const { readFile, writeFile } = require('fs') | ||
/* eslint-disable semi */ | ||
const mkdirp = require('mkdirp'); | ||
const { get } = require('https'); | ||
const { readFile, writeFile } = require('fs'); | ||
const getColour = coverage => { | ||
/** | ||
* Will lookup the argument in the cli arguments list and will return a | ||
* value passed as CLI arg (if found) | ||
* Otherwise will return default value passed | ||
* @param argName - name of hte argument to look for | ||
* @param defaultOutput - default value to return if could not find argument in cli command | ||
* @private | ||
*/ | ||
const findArgument = (argName, defaultOutput) => { | ||
if (!argName) { | ||
return defaultOutput; | ||
} | ||
const index = process.argv.findIndex(a => a.match(argName)) | ||
if (index < 0) { | ||
return defaultOutput; | ||
} | ||
try { | ||
return process.argv[index + 1]; | ||
} catch (e) { | ||
return defaultOutput; | ||
} | ||
} | ||
const outputPath = findArgument('output', './coverage'); | ||
const inputPath = findArgument('input', './coverage/coverage-summary.json'); | ||
const getColour = (coverage) => { | ||
if (coverage < 80) { | ||
@@ -16,3 +47,3 @@ return 'red'; | ||
return 'brightgreen'; | ||
} | ||
}; | ||
@@ -29,29 +60,47 @@ const reportKeys = ['lines', 'statements', 'functions', 'branches']; | ||
return `https://img.shields.io/badge/Coverage-${coverage}${encodeURI('%')}-${colour}.svg`; | ||
return `https://img.shields.io/badge/Coverage${encodeURI(':')}${key}-${coverage}${encodeURI('%')}-${colour}.svg`; | ||
} | ||
const download = (url, cb) => { | ||
get(url, res => { | ||
get(url, (res) => { | ||
let file = ''; | ||
res.on('data', chunk => (file += chunk)); | ||
res.on('data', (chunk) => { | ||
file += chunk; | ||
}); | ||
res.on('end', () => cb(null, file)); | ||
}).on('error', err => cb(err)); | ||
}; | ||
} | ||
const writeBadgeInFolder = (key, res) => { | ||
writeFile(`${outputPath}/badge-${key}.svg`, res, 'utf8', (writeError) => { | ||
if (writeError) { | ||
throw writeError; | ||
} | ||
}); | ||
} | ||
const getBadgeByKey = (report) => (key) => { | ||
const getBadgeByKey = report => (key) => { | ||
const url = getBadge(report, key); | ||
download(url, (err, res) => { | ||
if (err) throw err | ||
writeFile(`./coverage/badge-${key}.svg`, res, 'utf8', err => { | ||
if (err) throw err | ||
}); | ||
}); | ||
}; | ||
if (err) { | ||
throw err; | ||
} | ||
mkdirp(outputPath, (folderError) => { | ||
if (folderError) { | ||
console.error(`Could not create output directory ${folderError}`); | ||
} else { | ||
writeBadgeInFolder(key, res); | ||
} | ||
}) | ||
}) | ||
} | ||
readFile('./coverage/coverage-summary.json', 'utf8', (err, res) => { | ||
if (err) throw err | ||
readFile(`${inputPath}`, 'utf8', (err, res) => { | ||
if (err) { | ||
throw err; | ||
} | ||
const report = JSON.parse(res); | ||
reportKeys.forEach(getBadgeByKey(report)); | ||
}); |
{ | ||
"name": "jest-coverage-badges", | ||
"version": "1.0.0", | ||
"version": "1.1.2", | ||
"description": "Create a group of coverage badges from jest", | ||
@@ -26,8 +26,19 @@ "main": "cli.js", | ||
"homepage": "https://github.com/pamepeixinho/jest-coverage-badge#readme", | ||
"dependencies": {}, | ||
"scripts": { | ||
"lint": "eslint --fix -- ." | ||
}, | ||
"dependencies": { | ||
"mkdirp": "0.5.1" | ||
}, | ||
"devDependencies": { | ||
"eslint": "5.11.0", | ||
"eslint-config-airbnb": "17.1.0", | ||
"eslint-config-quintoandar-base": "1.0.1", | ||
"eslint-plugin-import": "2.14.0", | ||
"eslint-plugin-jsx-a11y": "6.1.2", | ||
"eslint-plugin-react": "7.11.1", | ||
"husky": "0.13.3", | ||
"semantic-release": "^6.3.6", | ||
"standard": "10.0.2", | ||
"validate-commit-msg": "2.12.1", | ||
"semantic-release": "^6.3.6" | ||
"validate-commit-msg": "2.12.1" | ||
}, | ||
@@ -34,0 +45,0 @@ "files": [ |
@@ -6,2 +6,3 @@ # Jest Coverage Badges | ||
[![License][license-image]][license-url] | ||
[](https://www.npmjs.com/package/jest-coverage-badges) | ||
@@ -19,4 +20,10 @@ [license-url]: https://opensource.org/licenses/MIT | ||
Currently just reads from Istanbul's JSON summary reporter and downloads a badge from https://shields.io/ for each jest coverage type (`statement`, `branch`, `functions`, `lines`). | ||
Currently just reads from Istanbul's JSON summary reporter and downloads a badge from https://shields.io/ for each jest coverage type (`statement`, `branch`, `functions`, `lines`). Like this: | ||
 | ||
 | ||
 | ||
 | ||
*This package is an extension of [make-coverage-badge], but this one creates all the types of coverage.* | ||
@@ -69,3 +76,2 @@ | ||
Resulting in badges: | ||
@@ -77,2 +83,10 @@ - `./coverage/badge-statements.svg` | ||
#### CLI Options | ||
* **input** [default: ./coverage/coverage-summary.json] - the file (and its path) of the summary json that contains the coverage data | ||
* **output** [default: ./coverage] - the path to the directory where the svg files will be placed after download. If path doesn't exist it will be created. | ||
**Example**: | ||
```$ jest-coverage-badges input "./cov" output "./badges"``` | ||
After this you can add into Github readme (for example) :smiley: | ||
@@ -79,0 +93,0 @@ |
8133
85
98
1
10
+ Addedmkdirp@0.5.1
+ Addedminimist@0.0.8(transitive)
+ Addedmkdirp@0.5.1(transitive)