Skip to content

Commit

Permalink
Adding input and ouput paths cli arguments (#3)
Browse files Browse the repository at this point in the history
*  - added parametrized input and output

*  - updated readme

* Update README.md

Co-Authored-By: michaelrodov <[email protected]>

*  - re-added semicolons

* - removed redundant else
  • Loading branch information
michaelrodov authored and pamepeixinho committed Dec 24, 2018
1 parent 837eacd commit bb567b3
Show file tree
Hide file tree
Showing 4 changed files with 352 additions and 298 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,20 @@ Currently just reads from Istanbul's JSON summary reporter and downloads a badge

3. Run `jest-coverage-badges` (or just run: `npm run test:badges`)


Resulting in badges:
- `./coverage/badge-statements.svg`
- `./coverage/badge-lines.svg`
- `./coverage/badge-functions.svg`
- `./coverage/badge-branches.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:

## Why use this package?
Expand Down
66 changes: 49 additions & 17 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/usr/bin/env node

/* eslint-disable semi */
const mkdirp = require('mkdirp');
const { get } = require('https');
const { readFile, writeFile } = require('fs');

const outputPath = _findArgument('output', './coverage');
const inputPath = _findArgument('input', './coverage/coverage-summary.json');

const getColour = coverage => {
if (coverage < 80) {
return 'red';
Expand All @@ -25,33 +28,62 @@ const getBadge = (report, key) => {
const coverage = report.total[key].pct;
const colour = getColour(coverage);

return `https://img.shields.io/badge/` +
`Coverage${encodeURI(':')}${key}-` +
`${coverage}${encodeURI('%')}` +
`-${colour}.svg`;
};
return `https://img.shields.io/badge/Coverage-${coverage}${encodeURI('%')}-${colour}.svg`;
}

const download = (url, cb) => {
get(url, res => {
let file = '';
res.on('data', chunk => (file += chunk));
res.on('end', () => cb(null, file));
}).on('error', err => cb(err));
};
}

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, function (err) {
if (err) {
console.error(`Could not create output directory ${err}`);
} else {
writeFile(`${outputPath}/badge-${key}.svg`, res, 'utf8', err => {
if (err) throw err;
})
}
})
})
}

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));
});
})

/**
* 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
*/
function _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;
}
}
Loading

0 comments on commit bb567b3

Please sign in to comment.