jest-coverage-shield
Advanced tools
Comparing version
@@ -8,11 +8,9 @@ #!/usr/bin/env node | ||
const cloverReportParser = require('./cloverReportParser'); | ||
const download = require('./downloader'); | ||
const xmlFileToJson = require('./xmlFileToJson'); | ||
const saveFile = require('./saveFile'); | ||
const createBadgeUrl = require('./createBadgeUrl'); | ||
const updateBadge = require('./updateBadge'); | ||
const fs = require('fs'); | ||
const help = require('./_help'); | ||
const input = path.resolve('./coverage/clover.xml'); | ||
const output = path.resolve('./coverage'); | ||
const output = path.resolve('./shields'); | ||
@@ -27,3 +25,2 @@ program | ||
) | ||
.option('-f, --defaults', 'Use the default values for all the input.') | ||
.option( | ||
@@ -51,27 +48,6 @@ '-e, --excellentThreashold <n>', | ||
.option('-v, --verbose', 'Prints the metadata for the command') | ||
.option('-s, --shieldStyle <shieldStyle>', 'Shield style') | ||
.option('-o, --overwrite', 'Do a fresh download from shield io') | ||
.parse(process.argv); | ||
program.on('--help', function() { | ||
console.log(' Examples:'); | ||
console.log(''); | ||
console.log( | ||
' $ coverage-badger -e 90 -g 65 -r coverage/clover.xml -d coverage/' | ||
); | ||
console.log(' * Green: coverage >= 90'); | ||
console.log(' * Yellow: 65 <= coverage < 90'); | ||
console.log(' * Red: coverage < 65'); | ||
console.log( | ||
' * Created at the coverage directory from the given report.' | ||
); | ||
console.log(''); | ||
}); | ||
program.on('--help', help); | ||
// Stop the execution if no options were provided. | ||
if (!program.defaults && !process.argv.slice(2).length) { | ||
program.outputHelp(); | ||
process.exit(0); | ||
} | ||
// The options to the badger API coming from the program. | ||
@@ -83,3 +59,2 @@ const opts = { | ||
), | ||
badgeFormat: program.format, | ||
destinationDir: path.resolve(program.destinationDir), | ||
@@ -90,21 +65,8 @@ reportFile: program.reportFile, | ||
good: program.goodThreashold | ||
}, | ||
shieldStyle: program.shieldStyle, | ||
overwrite: program.overwrite | ||
} | ||
}; | ||
if (!opts.overwrite && fs.existsSync(opts.badgeFileName)) { | ||
// Do not re-download, read from file and just change coverage... | ||
xmlFileToJson(opts.reportFile) | ||
.then(cloverReportParser) | ||
.then(updateBadge(opts)); | ||
} else { | ||
xmlFileToJson(opts.reportFile) | ||
.then(cloverReportParser) | ||
.then(createBadgeUrl(opts)) | ||
.then(download) | ||
.then(saveFile(opts.badgeFileName)) | ||
.then(console.log) | ||
.catch(console.error); | ||
} | ||
// Update badge | ||
xmlFileToJson(opts.reportFile) | ||
.then(cloverReportParser) | ||
.then(updateBadge(opts)); |
@@ -6,17 +6,18 @@ 'use strict'; | ||
module.exports = saveAtPath => buffer => | ||
// Creates directory and file | ||
function ensureDirectoryExistence(filePath) { | ||
var dirname = path.dirname(filePath); | ||
if (fs.existsSync(dirname)) return true; | ||
ensureDirectoryExistence(dirname); | ||
fs.mkdirSync(dirname); | ||
} | ||
module.exports = saveAtPath => (buffer, newPercent) => | ||
new Promise((resolve, reject) => { | ||
var dirToSave = path.dirname(saveAtPath); | ||
ensureDirectoryExistence(saveAtPath); | ||
fs.writeFile(saveAtPath, buffer, err => { | ||
if (err) reject(err); | ||
fs.stat(saveAtPath, (err, stat) => { | ||
if (err) reject(err); | ||
resolve({ | ||
filePath: saveAtPath, | ||
size: stat.size | ||
}); | ||
}); | ||
console.info(`Successfully created badge`); | ||
if (newPercent) console.info(`Overall coverage: ${newPercent}%`); | ||
}); | ||
}); |
'use strict'; | ||
const _ = require('lodash'); | ||
const xmlFileToJson = require('./xmlFileToJson'); | ||
const saveFile = require('./saveFile'); | ||
const createBadgeUrl = require('./createBadgeUrl'); | ||
const download = require('./downloader'); | ||
const fs = require('fs'); | ||
const decode = require('decode-html'); | ||
const window = require('svgdom'); | ||
const SVG = require('svg.js')(window); | ||
const document = window.document; | ||
const getColor = require('./getColor'); | ||
const path = require('path'); | ||
@@ -19,37 +14,2 @@ String.prototype.replaceAll = function(search, replacement) { | ||
const isSameLength = (x, y) => String(x).length === String(y).length; | ||
const isSameColor = opts => (x, y) => { | ||
const getColor = p => | ||
p >= opts.thresholds.excellent | ||
? 'brightgreen' | ||
: p >= opts.thresholds.good ? 'yellow' : 'red'; | ||
return getColor(x) === getColor(y); | ||
}; | ||
const findOldPercent = node => { | ||
let anyPercent; | ||
if (node.childNodes) { | ||
for (let x = 0; x < node.childNodes.length; x++) { | ||
const childNode = node.childNodes[x]; | ||
// Only look at TextNodes | ||
if (!childNode.constructor.name === 'TextNode') return; | ||
const p = findOldPercent(node.childNodes[x]); | ||
// Already found shortcut exit | ||
if (!isNaN(p)) return p; | ||
if (p && p.indexOf('%') > 0) { | ||
const trimmed = p.replace('%', ''); | ||
anyPercent = parseInt(trimmed, 10); | ||
break; | ||
} | ||
} | ||
} | ||
return parseInt(anyPercent || node.data, 10); | ||
}; | ||
const recursivelyUpdatePercentNodes = (node, percent) => { | ||
@@ -64,43 +24,27 @@ if (node.childNodes) | ||
module.exports = opts => report => { | ||
// Very crude method to update percentage | ||
const badgeFilePath = `${process.cwd()}/${opts.badgeFileName}`; | ||
const file = fs.readFileSync(badgeFilePath); | ||
const svgAsString = file.toString(); | ||
const indexOfP = svgAsString.indexOf('%'); | ||
// Try to find the percentage | ||
const badgeFilePath = path.resolve(process.cwd(), opts.badgeFileName); | ||
const newPercent = report.overallPercent; | ||
const isPerfect = newPercent === 100; | ||
// create svg.js instance | ||
const draw = SVG(document.documentElement); | ||
// Special case | ||
if (isPerfect) { | ||
const file = fs.readFileSync( | ||
path.resolve(__dirname, '../lib/templates/perfect.svg') | ||
); | ||
saveFile(badgeFilePath)(file, '100'); | ||
return; | ||
} | ||
draw.svg(svgAsString); | ||
// Else get the right color | ||
const color = getColor(opts, newPercent); | ||
const file = fs.readFileSync( | ||
path.resolve(__dirname, `../lib/templates/${color}.svg`) | ||
); | ||
const svgAsString = file.toString(); | ||
let oldPercent = findOldPercent(draw.node); | ||
// HACK - pretty dangerous but it should be ok | ||
const newSvg = svgAsString.replaceAll(`>00%<`, `>${newPercent}%<`); | ||
if ( | ||
!isSameLength(oldPercent, newPercent) || | ||
!isSameColor(oldPercent, newPercent) | ||
) { | ||
// Grab a new svg sizing has changed | ||
const url = createBadgeUrl(opts)(report); | ||
download(url) | ||
.then(saveFile(badgeFilePath)) | ||
.then(console.log) | ||
.then(() => { | ||
console.log(`Successfully created badge for ${newPercent}% coverage`); | ||
}) | ||
.catch(console.error); | ||
} else { | ||
// HACK - pretty dangerous but it should be ok | ||
const newSvg = svgAsString.replaceAll( | ||
`>${oldPercent}%<`, | ||
`>${newPercent}%<` | ||
); | ||
// Finally save the file | ||
fs.writeFile(badgeFilePath, newSvg, err => { | ||
if (err) throw err; | ||
console.log(`Successfully created badge for ${newPercent}% coverage`); | ||
}); | ||
} | ||
// Save file | ||
saveFile(badgeFilePath)(newSvg, newPercent); | ||
}; |
{ | ||
"name": "jest-coverage-shield", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Auto creates jest coverage shield from shield.io", | ||
@@ -14,5 +14,5 @@ "keywords": [ | ||
"license": "MIT", | ||
"homepage": "https://github.com/patrick-lai/coverage-badger", | ||
"homepage": "https://github.com/patrick-lai/jest-coverage-shield", | ||
"bugs": { | ||
"url": "https://github.com/patrick-lai/coverage-badger/issues" | ||
"url": "https://github.com/patrick-lai/jest-coverage-shield/issues" | ||
}, | ||
@@ -32,20 +32,16 @@ "files": [ | ||
"bin": { | ||
"coverage-badger": "./lib/cli.js" | ||
"coverage-shield": "./lib/cli.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:patrick-lai/coverage-badger.git" | ||
"url": "git@github.com:patrick-lai/jest-coverage-shield.git" | ||
}, | ||
"scripts": { | ||
"test": "istanbul cover ./node_modules/mocha/bin/_mocha --report text -- -R spec", | ||
"make-badge": "node ./lib/cli -s flat-square -d badges/" | ||
"shield": "node ./lib/cli", | ||
"refresh": "node ./tools/refresh" | ||
}, | ||
"dependencies": { | ||
"commander": "^2.8.1", | ||
"decode-html": "^2.0.0", | ||
"lodash": "^4.17.10", | ||
"svg.js": "^2.6.5", | ||
"svgdom": "0.0.15", | ||
"xml-splitter": "~1.2.1", | ||
"xtend": "~4.0.1" | ||
"xml-splitter": "~1.2.1" | ||
}, | ||
@@ -52,0 +48,0 @@ "devDependencies": { |
# coverage-badger | ||
This is a fork of [coverage-badger](https://github.com/notnotse/coverage-badger). Its quite slow to download from `shield.io` Every single time. So if there is no change to the size/color of the badge. We will just modify the %age instead. Also opens up the `shieldStyle` flag. | ||
This is a fork of [coverage-badger](https://github.com/notnotse/coverage-badger). This is much faster but more locked down method that leverages off pre-downloaded `svgs` instead of downloading from `shield.io` every time. | ||
 | ||
 | ||
@@ -10,4 +10,4 @@ Creates a coverage badge by reading the Clover XML coverage report using https://github.com/badges/shields. | ||
* The badge displays appropriate colors for the badge. | ||
* Green: >= 80% overall coverage | ||
* Yellow: 65% <= overall coverage < 80% | ||
* Green: >= 90% overall coverage | ||
* Yellow: 65% <= overall coverage < 90% | ||
* Red: < 65% overall coverage | ||
@@ -18,3 +18,3 @@ | ||
``` | ||
npm install --save-dev coverage-badger | ||
npm install --save-dev jest-coverage-shield | ||
``` | ||
@@ -30,4 +30,3 @@ | ||
"scripts": { | ||
"coverage-badge": "coverage-badger -r coverage/clover.xml -d coverage/", | ||
"coverage": "npm test -- --coverage && npm run coverage-badge" | ||
"test": "jest && coverage-shield" | ||
} | ||
@@ -43,3 +42,3 @@ ``` | ||
``` | ||
$ ./node_modules/coverage-badger/lib/cli.js | ||
$ ./node_modules/jest-coverage-shield/lib/cli.js | ||
@@ -54,3 +53,2 @@ Usage: cli [options] | ||
-V, --version output the version number | ||
-f, --defaults Use the default values for all the input. | ||
-e, --excellentThreshold <n> The threshold for green badges, where coverage >= -e | ||
@@ -61,3 +59,2 @@ -g, --goodThreshold <n> The threshold for yellow badges, where -g <= coverage < -e | ||
-d, --destinationDir <destination> The directory where 'coverage.svg' will be generated at. | ||
-s, --shieldStyle <style> The badge style check shields.io for more info | ||
Examples: | ||
@@ -64,0 +61,0 @@ |
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
2
-71.43%14
40%0
-100%11740
-10.71%178
-32.06%62
-4.62%- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed