From 5559e65c5dd092617296e3df64190329a6c11c30 Mon Sep 17 00:00:00 2001 From: Nico Ismaili Date: Sat, 6 Jan 2024 14:17:06 +0100 Subject: [PATCH 1/3] feat: added bruno dev files + updated readme --- .bruno/psi-svg/Basic.bru | 11 +++++++++++ .bruno/psi-svg/bruno.json | 5 +++++ README.md | 12 +++--------- 3 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 .bruno/psi-svg/Basic.bru create mode 100644 .bruno/psi-svg/bruno.json diff --git a/.bruno/psi-svg/Basic.bru b/.bruno/psi-svg/Basic.bru new file mode 100644 index 0000000..c7d494f --- /dev/null +++ b/.bruno/psi-svg/Basic.bru @@ -0,0 +1,11 @@ +meta { + name: Basic + type: http + seq: 1 +} + +get { + url: https://www.vitalback-baeckereitechnik.de. + body: none + auth: none +} diff --git a/.bruno/psi-svg/bruno.json b/.bruno/psi-svg/bruno.json new file mode 100644 index 0000000..8e7b091 --- /dev/null +++ b/.bruno/psi-svg/bruno.json @@ -0,0 +1,5 @@ +{ + "version": "1", + "name": "psi-svg", + "type": "collection" +} \ No newline at end of file diff --git a/README.md b/README.md index 0d8fa91..7ab1e69 100644 --- a/README.md +++ b/README.md @@ -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. From bc0c594a3cf2bb03b31742ed16c59d1febe8da25 Mon Sep 17 00:00:00 2001 From: Nico Ismaili Date: Sat, 6 Jan 2024 14:22:21 +0100 Subject: [PATCH 2/3] chore: added fallbacks and flooring to prevent infinite zeros glitch --- .bruno/psi-svg/Basic.bru | 6 +++++- src/domain/services/insights-service/index.ts | 6 +++--- src/domain/services/svg-service/index.ts | 16 ++++++++-------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/.bruno/psi-svg/Basic.bru b/.bruno/psi-svg/Basic.bru index c7d494f..c8e2748 100644 --- a/.bruno/psi-svg/Basic.bru +++ b/.bruno/psi-svg/Basic.bru @@ -5,7 +5,11 @@ meta { } get { - url: https://www.vitalback-baeckereitechnik.de. + url: http://localhost:3000/?url=https://www.vitalback-baeckereitechnik.de. body: none auth: none } + +query { + url: https://www.vitalback-baeckereitechnik.de. +} diff --git a/src/domain/services/insights-service/index.ts b/src/domain/services/insights-service/index.ts index 14ff199..96bf2b2 100644 --- a/src/domain/services/insights-service/index.ts +++ b/src/domain/services/insights-service/index.ts @@ -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( diff --git a/src/domain/services/svg-service/index.ts b/src/domain/services/svg-service/index.ts index cd3d2f1..f982047 100644 --- a/src/domain/services/svg-service/index.ts +++ b/src/domain/services/svg-service/index.ts @@ -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"); @@ -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); @@ -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); From 46276220c855b1a66faf38822e811dba18068795 Mon Sep 17 00:00:00 2001 From: Nico Ismaili Date: Sat, 6 Jan 2024 14:27:22 +0100 Subject: [PATCH 3/3] feat: added dockerignore --- .dockerignore | 5 +++++ .npmignore | 1 + README.md | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f858c13 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.git/**/* +.github/**/* +.bruno/**/* +docs/**/* +example.env \ No newline at end of file diff --git a/.npmignore b/.npmignore index cf8484f..2a72aa7 100644 --- a/.npmignore +++ b/.npmignore @@ -2,6 +2,7 @@ docs/**/* src/**/* .git/**/* .github/**/* +.bruno/**/* Dockerfile example.env diff --git a/README.md b/README.md index 7ab1e69..5545bac 100644 --- a/README.md +++ b/README.md @@ -139,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).