Skip to content

Commit 63c5780

Browse files
author
Quentin Nativel
committed
update my article
1 parent 103c7e9 commit 63c5780

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed
59 KB
Loading
636 KB
Loading

blog-posts/cli-reporting/cli-reporting.md

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,55 @@
22
published: false
33
title: 'Generate pretty reports from your cli tool'
44
cover_image: 'https://raw.githubusercontent.com/QuentinNativel/articles/master/blog-posts/cli-reporting/assets/dummy-report-100.png'
5-
description: 'Simple way to generate report from your cli using only vite and react'
5+
description: 'Feature insight: sls-mentor awesome new report'
66
tags: webdev, javascript, react, tutorial
77
series:
88
canonical_url:
99
---
1010

11-
# Context
11+
# TLDR
1212

13-
I work on an open source project: [sls-mentor](https://www.sls-mentor.dev/), that audits an aws stack and give advices on the best practices to configure serverless resources on AWS.
13+
In this feature insight, we explain how we generate the new reporting feature from sls-mentor, getting inspiration from [jest-html-reporter](https://github.com/Hazyzh/jest-html-reporters). And we show you how to code your own report tool using react.
1414

15-
On the project, we wanted to add a feature of report generation to have a visual document with every important information at a glance and that can be shared easily.
15+
# Getting a fresh new report for sls-mentor ?
1616

17-
# Requirements
17+
With my team we have been building [sls-mentor](https://www.sls-mentor.dev/), a **free and open source** tool to audit your serverless stack on AWSand to give you tips to improve it!
1818

19-
- Report generation is done in a CLI ⌨️
20-
- The report is pretty 💄
21-
- As a fullstack developper, I wanted to use a tool that I know well to generate the report: [React](https://react.dev/)
19+
The tool runs in the cli and outputs the result directly in the terminal. We pimped the result, and it is already quite good-looking 😎.
2220

23-
# Tech strategy
21+
![](./assets/sls-mentor-cli.png)
2422

25-
## Find inspiration 💡
23+
But a cli print is not easy to share and the look still feels a bit rough! And as developers, we have to present technical concepts all the time, and we learned that the presentation is as important as the content. That's why we wanted a report that can be presented to our clients, our managers to convey the quality of our serverless infrastructures or the need for real work on it.
2624

27-
First, I started with a naive search on how other auditing tools were generating their reports. I found the example of [jest-html-reporter](https://github.com/Hazyzh/jest-html-reporters/tree/master)
25+
That's why we decided to add a reporting feature to sls-mentor.
2826

29-
The report looks amazing ! 😍
27+
# It does look better doesn't it ? 😍
3028

31-
![](./assets/jest-html-reporter.png)
29+
![](./assets/sls-mentor-report.png)
3230

33-
And guess what ? It is written in react ! 🚀
31+
The report is generated in the cli as a single html file. So you can share it and display it in any browser! Showing your audit results has never been so easy! 🚀
3432

35-
## Dig in how it works 🪛
33+
Moreover tips are displayed on the bottom to help you improve your stack, and they redirect to sls-mentor website to get more details. 🤓
3634

37-
In the details, I didn't understand everything 🙈, but it doesn't matter! What matters is the general idea !
35+
# What's under the hood ? 🪛
3836

39-
- They have a React application, the app is built into a template html file.
37+
The report is generated in React, it is a one of most popular and well known framework in web development so it is easy to customize and to add new features. we are already working on it! 💪
4038

41-
- The template file contains a placeholder string, for example for jest-html-report, it is:
39+
We actually got inspired by [jest-html-reporter](https://github.com/Hazyzh/jest-html-reporters) to generate the report.
4240

43-
```
41+
First we write a template react app, and we build it into a single html file.
42+
43+
The template file contains a placeholder string, for example for jest-html-report, it is:
44+
45+
```javascript
4446
module.exports = '<<<JEST-HTML-REPLACE-PLACEHOLDER>>>';
4547
```
4648

47-
- Then at runtime, jest copies the template file, then replace the placeholder with the result of the analysis and 💥 ! It generates an html report !
49+
Then at runtime, jest copies the template file, then replaces the placeholder with the result of the analysis and 💥! It generates an html report!
50+
51+
Simple isn't it ? 😎
4852

49-
# Let's write a report template
53+
# Coding time 👨🏽‍💻! Let's write a report template
5054

5155
Ok, now we know the general idea, let's write our own report template.
5256

@@ -66,7 +70,7 @@ Now you have a react app. Try to build it.
6670

6771
It generates a dist folder with an index.html file. But if you try to open it in your browser, you will see a blank page.
6872

69-
If you open the index.html in an editor, you will notice it is almost empty, it just an empty div and some scripts to js files.
73+
If you open the index.html in an editor, you will notice it just an empty div and some scripts to js files.
7074

7175
That's not what we want, we want a single html file that contains everything. So it is easy to share.
7276

@@ -89,7 +93,7 @@ export default defineConfig({
8993

9094
Now if you build again, you will have a single html file in the dist folder. 🎉
9195

92-
If you open it in your browser, you will see your app, except for a small detail: the images are not displayed! 😱 Because the images are absolute imports to other svg files in the dist folder. The issue is not problematic but a bit painful to fix: the svg must be converted into react components (but that is not the topic of this article 😜).
96+
If you open it in your browser, you will see your app, except for a small detail: the images are not displayed! 😱 Because the images are absolute imports to other svg files in the dist folder. The issue is not problematic but a bit painful to fix: the svg must be converted into react components (which can be done using this great [tool](https://react-svgr.com/playground/)).
9397

9498
## Customize the report
9599

@@ -197,7 +201,6 @@ The placeholder `<<<RESULTS_PLACEHOLDER>>>` will be replaced by the result of th
197201
Now we have a template, we want to test it.
198202

199203
- First, let's build the report again and copy it to a new file report.html.
200-
201204
- Open the report.html in your editor, and search for `<<<RESULTS_PLACEHOLDER>>>`.
202205
- Replace it with `{\"score\": 100}` (⚠️ don't forget to escape the quotes !).
203206
- Save the file and open it in your browser.
@@ -213,10 +216,27 @@ In order to build the report from your cli, just automate the previous steps wit
213216
- duplicate the template file to a new file report.html
214217
- replace the placeholder with the result of the analysis and don't forget to escape the quotes
215218

216-
Easy isn't it ? 😎
219+
Here is an example of script with nodejs:
220+
221+
```typescript
222+
import { readFileSync, writeFileSync } from 'node:fs';
223+
224+
const TEMPLATE_PATH = 'template/index.html';
225+
const PLACEHOLDER = '<<<RESULTS_PLACEHOLDER>>>';
226+
const REPORT_OUTPUT_PATH = 'public/report.html';
227+
const auditResult = { score: 100 };
228+
229+
const data = JSON.stringify(auditResult).replace(/"/g, '\\"');
230+
231+
const template = readFileSync(TEMPLATE_PATH).toString();
232+
233+
const report = template.replace(PLACEHOLDER, data);
234+
235+
writeFileSync(REPORT_OUTPUT_PATH, report);
236+
```
217237

218238
Now it's your time to shine ! 🌟
219239

220240
# Found a typo?
221241

222-
If you've found a typo, a sentence that could be improved or anything else that should be updated on this blog post, you can access it through a git repository and make a pull request. Instead of posting a comment, please go directly to <REPO URL> and open a new pull request with your changes.
242+
If you've found a typo, a sentence that could be improved or anything else that should be updated on this blog post, you can access it through a git repository and make a pull request. Instead of posting a comment, please go directly to https://github.com/QuentinNativel/articles and open a new pull request with your changes.

0 commit comments

Comments
 (0)