diff --git a/.gitignore b/.gitignore index 58a4708..70643f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /node_modules /.nyc_output -/coverage +/coverage/* +!/coverage/coverage-summary.json diff --git a/README.md b/README.md index 1f4da36..8809bc0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # pacman-js -![Code Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen.svg) +[![Code Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)](https://github.com/bward2/pacman-js/blob/master/coverage/coverage-summary.json) [![Code Style](https://img.shields.io/badge/code%20style-airbnb-brightgreen.svg)](https://github.com/airbnb/javascript) Pacman clone made with Javascript, HTML, and CSS. diff --git a/coverage/coverage-summary.json b/coverage/coverage-summary.json new file mode 100644 index 0000000..5f187ba --- /dev/null +++ b/coverage/coverage-summary.json @@ -0,0 +1,22 @@ +{ + "statements": { + "covered": 1116, + "total": 1116, + "pct": 100 + }, + "branches": { + "covered": 451, + "total": 451, + "pct": 100 + }, + "functions": { + "covered": 214, + "total": 214, + "pct": 100 + }, + "lines": { + "covered": 1115, + "total": 1115, + "pct": 100 + } +} \ No newline at end of file diff --git a/package.json b/package.json index 0a0d862..89ff770 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,6 @@ { + "name": "pacman-js", + "author": "Brent Ward", "nyc": { "include": [ "app/scripts/**/*.js" @@ -6,7 +8,8 @@ "all": true }, "scripts": { - "test": "nyc --reporter=html --reporter=text --check-coverage --lines 100 --functions 100 --branches 100 mocha app/tests", + "test": "nyc --reporter=html --reporter=text --reporter=json --check-coverage --lines 100 --functions 100 --branches 100 mocha app/tests", + "posttest": "node scripts/coverage-summary.js", "lint": "eslint .", "update": "ncu -i --format group", "serve": "node ./node_modules/http-server/bin/http-server", diff --git a/scripts/coverage-summary.js b/scripts/coverage-summary.js new file mode 100644 index 0000000..676692b --- /dev/null +++ b/scripts/coverage-summary.js @@ -0,0 +1,43 @@ +/* eslint-disable no-console */ +const fs = require('fs'); +const path = require('path'); + +const htmlPath = path.join(__dirname, '..', 'coverage', 'index.html'); +const outputPath = path.join(__dirname, '..', 'coverage', 'coverage-summary.json'); + +if (!fs.existsSync(htmlPath)) { + console.error('coverage/index.html not found'); + process.exit(1); +} + +const html = fs.readFileSync(htmlPath, 'utf8'); + +const metrics = ['Statements', 'Branches', 'Functions', 'Lines']; +const summary = {}; + +metrics.forEach((metric) => { + const regex = new RegExp( + `${metric}<\\/span>[\\s\\S]*?(\\d+)\\/(\\d+)<\\/span>`, + 'i', + ); + + const match = html.match(regex); + + if (!match) { + console.warn(`Could not find ${metric} in coverage HTML`); + return; + } + + const covered = Number(match[1]); + const total = Number(match[2]); + + summary[metric.toLowerCase()] = { + covered, + total, + pct: total === 0 ? 100 : Math.round((covered / total) * 100), + }; +}); + +fs.writeFileSync(outputPath, JSON.stringify(summary, null, 2)); + +console.log('✅ Report summary ready at coverage/coverage-summary.json!');