Skip to content

Commit

Permalink
feat: change badge label optionally
Browse files Browse the repository at this point in the history
  • Loading branch information
olavoparno committed Jun 10, 2021
1 parent 90dff1e commit 9aaa8ab
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
14 changes: 10 additions & 4 deletions README.md
Expand Up @@ -4,9 +4,9 @@

> Creates README badges from istanbul coverage report
| Statements | Branches | Functions | Lines |
| --------------------------------------------------------------------------- | ------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| ![Statements](https://img.shields.io/badge/statements-100%25-brightgreen.svg) | ![Branches](https://img.shields.io/badge/branches-100%25-brightgreen.svg) | ![Functions](https://img.shields.io/badge/functions-100%25-brightgreen.svg) | ![Lines](https://img.shields.io/badge/lines-100%25-brightgreen.svg) |
| Statements | Branches | Functions | Lines |
| ----------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- | ------------------------------------------------------------------- |
| ![Statements](https://img.shields.io/badge/statements-100%25-brightgreen.svg) | ![Branches](https://img.shields.io/badge/Branches%20are%20troublesome!-100%25-brightgreen.svg) | ![Functions](https://img.shields.io/badge/Mis%20funciones!-100%25-brightgreen.svg) | ![Lines](https://img.shields.io/badge/lines-100%25-brightgreen.svg) |

---

Expand Down Expand Up @@ -50,7 +50,7 @@
### Requirements

- **Must** have at least one of the [before mentioned markup items](<#example-markup-(paste-it-anywhere-in-your-README.md)>);
- You should have **json-summary** as a **coverageReporter** in your tests configuration;
- You **must** have **json-summary** as a **coverageReporter** in your tests configuration;
- For example, if you are using Jest, configuration should either be within `package.json` or inside your jest config file i.e. `jest.config.js` or `jestconfig.json` as shown below:

```json
Expand Down Expand Up @@ -95,6 +95,12 @@
npm run istanbul-badges-readme --silent
```

- You may also create custom labeling for the badges using the corresponding hash and _Label_ e.g. _branchesLabel_ **--branchesLabel='Branches are troublesome!'**:

```bash
npm run istanbul-badges-readme --functionsLabel='Mis funciones!' --branchesLabel='Branches are troublesome!'
```

- Or add it to your **package.json** scripts as follows:

```json
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -11,7 +11,7 @@
"start": "npm run build && node lib/index.js",
"start:dev": "nodemon",
"release": "standard-version",
"make-badges": "node lib/index.js",
"make-badges": "node lib/index.js -- --functionsLabel='Mis funciones!' --branchesLabel='Branches are troublesome!'",
"prepublishOnly": "npm run build"
},
"main": "lib/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/arguments.ts
@@ -1,4 +1,4 @@
export const getArgumentValue = (argName: string): string | boolean => {
export const getArgumentValue = (argName: string): string | false => {
const args = process.argv
.filter((item) => {
if (item.indexOf(argName) >= 0) {
Expand Down
5 changes: 4 additions & 1 deletion src/editor.ts
@@ -1,4 +1,5 @@
import fs from 'fs';
import { getArgumentValue } from './arguments';
import { readmePathConst, coveragePathConst, hashesConst, coverageUrlConst } from './constants';
import { getCoveragePath, getReadmePath, readFileAsync } from './helpers';
import { logger } from './logger';
Expand Down Expand Up @@ -45,8 +46,10 @@ export const getCoverageBadge = (coverageFile: string, hashKey: string): string

const coverage: number = parsedCoverage.total[hashKey].pct;
const color = getCoverageColor(coverage);
const customLabel = getArgumentValue(`${hashKey}Label`);
const badgeAlt = customLabel ? encodeURI(customLabel) : hashKey;

return coverageUrlConst(hashKey, coverage, color);
return coverageUrlConst(badgeAlt, coverage, color);
} catch {
return false;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/editor.spec.ts
Expand Up @@ -65,4 +65,15 @@ describe('Tests editor', () => {

expect(hasItSucceeded).toBeFalsy();
});

it('should accept custom label for hashKey', () => {
const fakeJsonCoveragePath = path.join(__dirname, '../tests/mocks/fakeBadCoverage.json');
const fakeJsonCoverageFile = fs.readFileSync(fakeJsonCoveragePath, 'utf-8');

process.argv.push('--badLabel=customBadLabel');

const customBadgeLabel = getCoverageBadge(fakeJsonCoverageFile, 'bad');

expect(customBadgeLabel).toEqual('https://img.shields.io/badge/customBadLabel-95.45%25-brightgreen.svg');
});
});

0 comments on commit 9aaa8ab

Please sign in to comment.