Skip to content

Commit

Permalink
Merge pull request #7 from nico-i/issue/5-infinite-zeros
Browse files Browse the repository at this point in the history
Issue/5 infinite zeros
  • Loading branch information
nico-i authored Jan 6, 2024
2 parents a7a9271 + 4627622 commit c17678f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 20 deletions.
15 changes: 15 additions & 0 deletions .bruno/psi-svg/Basic.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
meta {
name: Basic
type: http
seq: 1
}

get {
url: http://localhost:3000/?url=https://www.vitalback-baeckereitechnik.de.
body: none
auth: none
}

query {
url: https://www.vitalback-baeckereitechnik.de.
}
5 changes: 5 additions & 0 deletions .bruno/psi-svg/bruno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"version": "1",
"name": "psi-svg",
"type": "collection"
}
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.git/**/*
.github/**/*
.bruno/**/*
docs/**/*
example.env
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ docs/**/*
src/**/*
.git/**/*
.github/**/*
.bruno/**/*

Dockerfile
example.env
Expand Down
32 changes: 23 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,24 @@ This node module performs a [Google PageSpeed Insights analysis](https://develop

## Installation

### npm
### [npm](https://www.npmjs.com/)

```bash
npm i -g psi-svg
```

[More info on `npm`](https://www.npmjs.com/)

### bun
### [bun](https://bun.sh/)

```bash
bun i -g psi-svg
```

[More info on `bun`](https://bun.sh/)

### yarn
### [yarn](https://yarnpkg.com/)

```bash
yarn global add psi-svg
```

[More info on `yarn`](https://yarnpkg.com/)

## Usage

The module can be used as a CLI tool or as a web server. Different options can be used to customize the output for each use case.
Expand Down Expand Up @@ -145,6 +139,26 @@ docker run -p 3000:3000 psi-svg

The application can also be run as a Github Action an example workflow can be found in [`.github/workflows/pagespeed.yml`](.github/workflows/pagespeed.yml). The results of which are visible in [`docs/img/`](docs/img/). To use the action, simply copy the workflow file to your repository and modify the value of the `URL_TO_ANALYZE` and `RESULTS_DIR` variables. Also ensure that the Github Action Workers have write access to the repository. You can configure this under `Settings > Code and automation > Actions > General > Workflow permissions`.

## Development

This project uses [Node.js](https://nodejs.org/en/) and [Typescript](https://www.typescriptlang.org/) for development. To get started, clone the repository and install the dependencies:

```bash
git clone https://www.github.com/nico-i/psi-svg
cd psi-svg
npm install
```

To run the application, use the following command:

```bash
npm run start
```

This will start the web server on port 3000.

For development I recommend using the API testing tool [bruno](https://github.com/usebruno/bruno), which you can point to the [`/.bruno/`](./.bruno/) directory of this repo. It contains some helpful requests to test the application.

## Credits

This project is based on [ankurparihar](https://github.com/ankurparihar)'s [readme-pagespeed-insights](https://github.com/ankurparihar/readme-pagespeed-insights).
Expand Down
6 changes: 3 additions & 3 deletions src/domain/services/insights-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ export class InsightsService {
const res = await fetch(url);
const resJson = await res.json();

if (!resJson?.lighthouseResult?.categories?.[category].score) {
if (!resJson?.lighthouseResult?.categories?.[category]?.score) {
throw new DOMException(
`Could not retrieve insights for '${category}' category}`
);
}

let score;
let score: number;
if (category === InsightCategory.PWA) {
score = calcPWAScore(resJson.lighthouseResult);
} else {
score = resJson.lighthouseResult.categories[category].score;
score = resJson.lighthouseResult.categories[category].score ?? -1;
}

logger.info(
Expand Down
16 changes: 8 additions & 8 deletions src/domain/services/svg-service/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { optimize } from 'svgo';
import { Insights, PWAMetric } from "@domain/valueobjects/insights";
import {
InsightCategory,
Options,
getInsightCategoryText,
} from "@domain/valueobjects/options";
import { optimize } from "svgo";
const { JSDOM } = require("jsdom");
const fs = require("fs");
const path = require("path");
Expand Down Expand Up @@ -89,13 +89,13 @@ export class SvgService {
name: "preset-default",
params: {
overrides: {
removeViewBox: false
}
}
}
]
removeViewBox: false,
},
},
},
],
});

// Write to file
if (this.outputPath) {
fs.writeFileSync(this.outputPath, result.data);
Expand Down Expand Up @@ -136,7 +136,7 @@ export class SvgService {
gaugeSVGElement.setAttribute("x", xOffset);

const scoreTextElement = gaugeDocument.querySelector("#score");
scoreTextElement.textContent = score.toString();
scoreTextElement.textContent = Math.floor(score).toString();

const categoryTextElement = gaugeDocument.querySelector("#category");
categoryTextElement.textContent = getInsightCategoryText(category);
Expand Down

0 comments on commit c17678f

Please sign in to comment.