Skip to content

Commit

Permalink
Add possibility to save to directory
Browse files Browse the repository at this point in the history
  • Loading branch information
emazzotta committed Nov 19, 2018
1 parent 630d09c commit cf18b69
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1,529 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[![Greenkeeper badge](https://badges.greenkeeper.io/emazzotta/lighthouse-badges.svg)](https://greenkeeper.io/)
[![NPM version](https://img.shields.io/npm/v/lighthouse-badges.svg)](https://www.npmjs.org/package/lighthouse-badges)
[![License](http://img.shields.io/:license-mit-blue.svg?style=flat)](https://emanuelemazzotta.com/mit-license)
[![Twitter](https://img.shields.io/badge/Twitter-emazzotta-00aced.svg?style=flat)](https://twitter.com/emazzotta)

# Lighthouse Badges

Expand Down Expand Up @@ -59,15 +58,16 @@ Required arguments:
### Run

```bash
# Option 1: Using npm
npm i -g lighthouse-badges
# The badges will be saved in your current directory
lighthouse-badges --urls https://www.youtube.com/ https://www.youtube.com/feed/trending

# Option 2: Using Docker
docker pull emazzotta/lighthouse-badges
# Replace $(pwd) with the report/svg save path on your host
docker run -v $(pwd):/home/chrome/reports emazzotta/lighthouse-badges /bin/sh -c "lighthouse-badges --urls https://www.youtube.com/ https://www.youtube.com/feed/trending"
# Option 1: npm
npm i -g lighthouse-badges && \
mkdir test_results && \
lighthouse-badges --urls https://www.youtube.com/ https://www.youtube.com/feed/trending -o test_results

# Option 2: Docker
docker run --rm \
-v $PWD/test_results:/home/chrome/reports \
emazzotta/lighthouse-badges \
/bin/sh -c "lighthouse-badges --urls https://www.youtube.com/ https://www.youtube.com/feed/trending"
```

## Contributing
Expand Down
Binary file removed assets/fonts/Verdana.ttf
Binary file not shown.
17 changes: 11 additions & 6 deletions lib/lighthouse-badges.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ const { percentageToColor } = require('./calculations');
const maxBuffer = 1024 * 50000;


const metricsToSvg = async (lighthouseMetrics, template, outputPath) => {
const metricsToSvg = async (lighthouseMetrics, badgeStyle, outputPath) => {
Object.keys(lighthouseMetrics).map((lighthouseMetricKey) => {
const fontPath = path.join(__dirname, '..', 'assets', 'fonts', 'Verdana.ttf');
const filepath = path.join(outputPath, `${lighthouseMetricKey.replace(/ /g, '_')}.svg`);
const text = [lighthouseMetricKey, `${lighthouseMetrics[lighthouseMetricKey]}%`];
const colorscheme = percentageToColor(lighthouseMetrics[lighthouseMetricKey]);
const badgeFactory = new BadgeFactory({ fontPath });
const svg = badgeFactory.create({ text, colorscheme, template });
const badgeColor = percentageToColor(lighthouseMetrics[lighthouseMetricKey]);
const bf = new BadgeFactory();
const format = {
text,
format: 'svg',
colorscheme: badgeColor,
template: badgeStyle,
};
const svg = bf.create(format);
fs.writeFile(filepath, svg, (err) => {
if (err) {
throw new Error(`Failed to save svg files to ${outputPath}`);
Expand Down Expand Up @@ -83,7 +88,7 @@ const processParameters = async (args, lighthouseMetricFunction) => {
await generateArtifacts({
reports,
svg: { results: metricsResults, style: args.badge_style },
path: args.output_path,
savePath: args.output_path,
});
};

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lighthouse-badges",
"version": "1.0.4",
"version": "1.0.5",
"description": "🚦Generate gh-badges (shields.io) based on Lighthouse performance.",
"main": "bin/index.js",
"bin": {
Expand Down Expand Up @@ -29,7 +29,7 @@
},
"dependencies": {
"argparse": "^1.0.10",
"gh-badges": "^2.0.0",
"gh-badges": "^2.1.0",
"lighthouse": "^3.1.1"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit cf18b69

Please sign in to comment.