Skip to content

Commit c5cfbce

Browse files
committed
chore: fix lint issues
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
1 parent 073052e commit c5cfbce

9 files changed

Lines changed: 206 additions & 112 deletions

File tree

Lines changed: 76 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import path from "node:path";
2+
import fs from "node:fs";
3+
import mockFs from "mock-fs";
24
import { describe, expect, it, vi } from "vitest";
35

46
process.env.GITHUB_REPOSITORY_OWNER ??= "hoverkraft-tech";
@@ -7,6 +9,22 @@ process.env.GITHUB_REPOSITORY ??= "hoverkraft-tech/public-docs";
79
const { HomepageProjectsUpdater } = await import(
810
"../lib/homepage/homepage-projects-updater.js"
911
);
12+
const { ConstDeclarationUpdater } = await import(
13+
"../lib/builders/const-declaration-updater.js"
14+
);
15+
16+
const homepagePath = path.join(process.cwd(), "application/src/pages/index.tsx");
17+
const repositories = [
18+
{
19+
name: "compose-action",
20+
html_url: "https://github.com/hoverkraft-tech/compose-action",
21+
stargazers_count: 210,
22+
language: "TypeScript",
23+
description:
24+
"This action runs your docker-compose file and clean up before action finished",
25+
topics: ["continuous-integration", "docker-compose", "github-actions"],
26+
},
27+
];
1028

1129
describe("HomepageProjectsUpdater", () => {
1230
it("formats the homepage file after updating featured projects", async () => {
@@ -16,22 +34,6 @@ describe("HomepageProjectsUpdater", () => {
1634
const formatter = {
1735
format: vi.fn().mockResolvedValue(undefined),
1836
};
19-
const homepagePath = path.join(
20-
process.cwd(),
21-
"application/src/pages/index.tsx",
22-
);
23-
const repositories = [
24-
{
25-
name: "compose-action",
26-
html_url: "https://github.com/hoverkraft-tech/compose-action",
27-
stargazers_count: 210,
28-
language: "TypeScript",
29-
description:
30-
"This action runs your docker-compose file and clean up before action finished",
31-
topics: ["continuous-integration", "docker-compose", "github-actions"],
32-
},
33-
];
34-
3537
const updater = new HomepageProjectsUpdater({
3638
homepagePath,
3739
constDeclarationUpdater,
@@ -40,28 +42,32 @@ describe("HomepageProjectsUpdater", () => {
4042

4143
await updater.update(repositories);
4244

43-
expect(constDeclarationUpdater.update).toHaveBeenCalledWith(homepagePath, [
44-
{
45-
name: "projects",
46-
value: [
47-
{
48-
accent: "primary",
49-
description:
50-
"This action runs your docker-compose file and clean up before action finished",
51-
icon: "⚡",
52-
language: "TypeScript",
53-
name: "compose-action",
54-
stars: 210,
55-
tags: [
56-
"continuous-integration",
57-
"docker-compose",
58-
"github-actions",
59-
],
60-
url: "https://github.com/hoverkraft-tech/compose-action",
61-
},
62-
],
63-
},
64-
]);
45+
expect(constDeclarationUpdater.update).toHaveBeenCalledWith(
46+
homepagePath,
47+
[
48+
expect.objectContaining({
49+
name: "projects",
50+
serialize: expect.any(Function),
51+
value: [
52+
{
53+
accent: "primary",
54+
description:
55+
"This action runs your docker-compose file and clean up before action finished",
56+
icon: "⚡",
57+
language: "TypeScript",
58+
name: "compose-action",
59+
stars: 210,
60+
tags: [
61+
"continuous-integration",
62+
"docker-compose",
63+
"github-actions",
64+
],
65+
url: "https://github.com/hoverkraft-tech/compose-action",
66+
},
67+
],
68+
}),
69+
],
70+
);
6571
expect(formatter.format).toHaveBeenCalledWith(homepagePath);
6672
});
6773

@@ -72,10 +78,6 @@ describe("HomepageProjectsUpdater", () => {
7278
const formatter = {
7379
format: vi.fn().mockResolvedValue(undefined),
7480
};
75-
const homepagePath = path.join(
76-
process.cwd(),
77-
"application/src/pages/index.tsx",
78-
);
7981
const updater = new HomepageProjectsUpdater({
8082
homepagePath,
8183
constDeclarationUpdater,
@@ -96,4 +98,36 @@ describe("HomepageProjectsUpdater", () => {
9698

9799
expect(formatter.format).not.toHaveBeenCalled();
98100
});
101+
102+
it("writes homepage projects using lint-compatible TSX style", async () => {
103+
const homepageContent = fs.readFileSync(homepagePath, "utf8");
104+
105+
mockFs({
106+
[homepagePath]: homepageContent,
107+
});
108+
109+
try {
110+
const formatter = {
111+
format: vi.fn().mockResolvedValue(undefined),
112+
};
113+
const updater = new HomepageProjectsUpdater({
114+
homepagePath,
115+
constDeclarationUpdater: new ConstDeclarationUpdater(),
116+
formatter,
117+
});
118+
119+
await updater.update(repositories);
120+
121+
const updatedHomepage = fs.readFileSync(homepagePath, "utf8");
122+
123+
expect(updatedHomepage).toContain('name: "compose-action"');
124+
expect(updatedHomepage).toContain(
125+
'tags: ["continuous-integration", "docker-compose", "github-actions"]',
126+
);
127+
expect(updatedHomepage).not.toContain("name: 'compose-action'");
128+
expect(updatedHomepage).not.toContain("'continuous-integration'");
129+
} finally {
130+
mockFs.restore();
131+
}
132+
});
99133
});

.github/actions/generate-docs/lib/homepage/homepage-projects-updater.js

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ class HomepageProjectsUpdater {
3131
const projectsModel = this.buildProjectsModel(featured);
3232
const hasChanges = await this.constDeclarationUpdater.update(
3333
this.homepagePath,
34-
[{ name: "projects", value: projectsModel }],
34+
[
35+
{
36+
name: "projects",
37+
value: projectsModel,
38+
serialize: serializeHomepageProjects,
39+
},
40+
],
3541
);
3642

3743
if (hasChanges) {
@@ -79,6 +85,69 @@ class HomepageProjectsUpdater {
7985
}
8086
}
8187

88+
function serializeHomepageProjects(projects) {
89+
if (!Array.isArray(projects) || projects.length === 0) {
90+
return "[]";
91+
}
92+
93+
const body = projects
94+
.map((project) => serializeHomepageProject(project, 1))
95+
.join(",\n");
96+
97+
return `\n${body}\n`;
98+
}
99+
100+
function serializeHomepageProject(project, indentLevel) {
101+
const indent = " ".repeat(indentLevel);
102+
const propertyIndent = " ".repeat(indentLevel + 1);
103+
104+
return [
105+
`${indent}{`,
106+
`${propertyIndent}name: ${serializeHomepageString(project.name)},`,
107+
`${propertyIndent}icon: ${serializeHomepageString(project.icon)},`,
108+
`${propertyIndent}url: ${serializeHomepageString(project.url)},`,
109+
`${propertyIndent}stars: ${String(project.stars ?? 0)},`,
110+
`${propertyIndent}language: ${serializeHomepageString(project.language)},`,
111+
serializeHomepageDescription(project.description, propertyIndent),
112+
`${propertyIndent}tags: ${serializeHomepageInlineStringArray(project.tags)},`,
113+
`${propertyIndent}accent: ${serializeHomepageString(project.accent)},`,
114+
`${indent}}`,
115+
].join("\n");
116+
}
117+
118+
function serializeHomepageDescription(description, indent) {
119+
const normalizedDescription = description ?? "";
120+
const serialized = serializeHomepageString(normalizedDescription);
121+
const singleLine = `${indent}description: ${serialized},`;
122+
123+
if (singleLine.length <= 80) {
124+
return singleLine;
125+
}
126+
127+
return `${indent}description:\n${indent} ${serialized},`;
128+
}
129+
130+
function serializeHomepageInlineStringArray(values) {
131+
if (!Array.isArray(values) || values.length === 0) {
132+
return "[]";
133+
}
134+
135+
return `[${values.map((value) => serializeHomepageString(value)).join(", ")}]`;
136+
}
137+
138+
function serializeHomepageString(value) {
139+
return `"${escapeHomepageString(String(value ?? ""))}"`;
140+
}
141+
142+
function escapeHomepageString(value) {
143+
return value
144+
.replace(/\\/g, "\\\\")
145+
.replace(/\n/g, "\\n")
146+
.replace(/\r/g, "\\r")
147+
.replace(/\t/g, "\\t")
148+
.replace(/"/g, '\\"');
149+
}
150+
82151
module.exports = {
83152
HomepageProjectsUpdater,
84153
};

application/docs/projects/ci-cd-tools/docker-base-images/actions/should-build-images/index.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ Check if some files have changed requiring the build of the given images.
5858
5959
## Inputs
6060
61-
| **Input** | **Description** | **Required** | **Default** |
62-
| -------------- | ----------------------------------------------------------------------------------------------------------------- | ------------ | ----------- |
63-
| **`images`** | Image names located in the 'images' folder. | **true** | - |
64-
| | Formatted as a JSON array. | | |
65-
| | Example: `["php-8", "nodejs-24"]` | | |
61+
| **Input** | **Description** | **Required** | **Default** |
62+
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ----------- |
63+
| **`images`** | Image names located in the 'images' folder. | **true** | - |
64+
| | Formatted as a JSON array. | | |
65+
| | Example: `["php-8", "nodejs-24"]` | | |
6666
| **`base-sha`** | Specify a different base commit SHA used for comparing changes. See [https://github.com/tj-actions/changed-files](https://github.com/tj-actions/changed-files) | **false** | - |
6767

6868
<!-- inputs:end -->

application/docs/projects/ci-cd-tools/docker-base-images/github/workflows/continuous-integration.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,17 @@ jobs:
172172
173173
### Workflow Call Inputs
174174
175-
| **Input** | **Description** | **Required** | **Type** | **Default** |
176-
| --------------------------- | -------------------------------------------------------------------------------------------- | ------------ | ---------- | -------------------------------- |
177-
| **`runs-on`** | JSON array of runner(s) to use. | **false** | **string** | `["ubuntu-latest"]` |
178-
| | See [https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job](https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job). | | | |
179-
| **`oci-registry`** | OCI registry where to pull and push images. | **false** | **string** | `ghcr.io` |
180-
| **`oci-registry-username`** | Username used to log against the OCI registry. | **false** | **string** | `${{ github.repository_owner }}` |
181-
| | See [https://github.com/docker/login-action#usage](https://github.com/docker/login-action#usage). | | | |
182-
| **`platforms`** | JSON array of platforms to build images for by default. | **false** | **string** | `["linux/amd64","linux/arm64"]` |
183-
| | Can be overridden per image with `images/<image>/build.json` or an image object in `images`. | | | |
184-
| | See [https://docs.docker.com/buildx/working-with-buildx/#build-multi-platform-images](https://docs.docker.com/buildx/working-with-buildx/#build-multi-platform-images). | | | |
185-
| **`test-image-tag`** | Tag of the published `testcontainers-node` runner image to use for tests. | **false** | **string** | `latest` |
175+
| **Input** | **Description** | **Required** | **Type** | **Default** |
176+
| --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ---------- | -------------------------------- |
177+
| **`runs-on`** | JSON array of runner(s) to use. | **false** | **string** | `["ubuntu-latest"]` |
178+
| | See [https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job](https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job). | | | |
179+
| **`oci-registry`** | OCI registry where to pull and push images. | **false** | **string** | `ghcr.io` |
180+
| **`oci-registry-username`** | Username used to log against the OCI registry. | **false** | **string** | `${{ github.repository_owner }}` |
181+
| | See [https://github.com/docker/login-action#usage](https://github.com/docker/login-action#usage). | | | |
182+
| **`platforms`** | JSON array of platforms to build images for by default. | **false** | **string** | `["linux/amd64","linux/arm64"]` |
183+
| | Can be overridden per image with `images/<image>/build.json` or an image object in `images`. | | | |
184+
| | See [https://docs.docker.com/buildx/working-with-buildx/#build-multi-platform-images](https://docs.docker.com/buildx/working-with-buildx/#build-multi-platform-images). | | | |
185+
| **`test-image-tag`** | Tag of the published `testcontainers-node` runner image to use for tests. | **false** | **string** | `latest` |
186186
187187
<!-- inputs:end -->
188188
@@ -204,9 +204,9 @@ jobs:
204204
205205
## Outputs
206206
207-
| **Output** | **Description** |
208-
| ------------------ | ------------------------------------------------------------------------------------------------------------------------ |
209-
| **`built-images`** | Built images data. |
207+
| **Output** | **Description** |
208+
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
209+
| **`built-images`** | Built images data. |
210210
| | See [https://github.com/hoverkraft-tech/ci-github-container/blob/main/.github/workflows/docker-build-images.md#outputs](https://github.com/hoverkraft-tech/ci-github-container/blob/main/.github/workflows/docker-build-images.md#outputs). |
211211
212212
<!-- outputs:end -->

application/docs/projects/ci-cd-tools/docker-base-images/github/workflows/prepare-release.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ jobs:
7777
7878
### Workflow Call Inputs
7979
80-
| **Input** | **Description** | **Required** | **Type** | **Default** |
81-
| ------------- | ---------------------------------------------------------------------------------- | ------------ | ---------- | ------------------- |
82-
| **`runs-on`** | JSON array of runner(s) to use. | **false** | **string** | `["ubuntu-latest"]` |
80+
| **Input** | **Description** | **Required** | **Type** | **Default** |
81+
| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ---------- | ------------------- |
82+
| **`runs-on`** | JSON array of runner(s) to use. | **false** | **string** | `["ubuntu-latest"]` |
8383
| | See [https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job](https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job). | | | |
8484

8585
<!-- inputs:end -->

application/docs/projects/ci-cd-tools/docker-base-images/github/workflows/prune-pull-requests-images-tags.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ jobs:
8181
8282
### Workflow Call Inputs
8383
84-
| **Input** | **Description** | **Required** | **Type** | **Default** |
85-
| ------------- | ---------------------------------------------------------------------------------- | ------------ | ---------- | ------------------- |
86-
| **`runs-on`** | JSON array of runner(s) to use. | **false** | **string** | `["ubuntu-latest"]` |
84+
| **Input** | **Description** | **Required** | **Type** | **Default** |
85+
| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ---------- | ------------------- |
86+
| **`runs-on`** | JSON array of runner(s) to use. | **false** | **string** | `["ubuntu-latest"]` |
8787
| | See [https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job](https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job). | | | |
8888

8989
<!-- inputs:end -->

0 commit comments

Comments
 (0)