Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import path from "node:path";
import { describe, expect, it, vi } from "vitest";

process.env.GITHUB_REPOSITORY_OWNER ??= "hoverkraft-tech";
process.env.GITHUB_REPOSITORY ??= "hoverkraft-tech/public-docs";

const { HomepageProjectsUpdater } = await import(
"../lib/homepage/homepage-projects-updater.js"
);

describe("HomepageProjectsUpdater", () => {
it("formats the homepage file after updating featured projects", async () => {
const constDeclarationUpdater = {
update: vi.fn().mockResolvedValue(true),
};
const formatter = {
format: vi.fn().mockResolvedValue(undefined),
};
const homepagePath = path.join(
process.cwd(),
"application/src/pages/index.tsx",
);
const repositories = [
{
name: "compose-action",
html_url: "https://github.com/hoverkraft-tech/compose-action",
stargazers_count: 210,
language: "TypeScript",
description:
"This action runs your docker-compose file and clean up before action finished",
topics: ["continuous-integration", "docker-compose", "github-actions"],
},
];

const updater = new HomepageProjectsUpdater({
homepagePath,
constDeclarationUpdater,
formatter,
});

await updater.update(repositories);

expect(constDeclarationUpdater.update).toHaveBeenCalledWith(homepagePath, [
{
name: "projects",
value: [
{
accent: "primary",
description:
"This action runs your docker-compose file and clean up before action finished",
icon: "⚡",
language: "TypeScript",
name: "compose-action",
stars: 210,
tags: [
"continuous-integration",
"docker-compose",
"github-actions",
],
url: "https://github.com/hoverkraft-tech/compose-action",
},
],
},
]);
expect(formatter.format).toHaveBeenCalledWith(homepagePath);
});

it("skips formatting when the homepage file is unchanged", async () => {
const constDeclarationUpdater = {
update: vi.fn().mockResolvedValue(false),
};
const formatter = {
format: vi.fn().mockResolvedValue(undefined),
};
const homepagePath = path.join(
process.cwd(),
"application/src/pages/index.tsx",
);
const updater = new HomepageProjectsUpdater({
homepagePath,
constDeclarationUpdater,
formatter,
});

await updater.update([
{
name: "compose-action",
html_url: "https://github.com/hoverkraft-tech/compose-action",
stargazers_count: 210,
language: "TypeScript",
description:
"This action runs your docker-compose file and clean up before action finished",
topics: ["continuous-integration", "docker-compose", "github-actions"],
},
]);

expect(formatter.format).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const path = require("node:path");
const { execFile } = require("node:child_process");
const { promisify } = require("node:util");
const { APPLICATION_ROOT } = require("../constants");

const execFileAsync = promisify(execFile);

class ApplicationBiomeFormatter {
constructor({ applicationRoot = APPLICATION_ROOT } = {}) {
this.applicationRoot = applicationRoot;
}

async format(filePath) {
const relativePath = path.relative(this.applicationRoot, filePath);

await execFileAsync(
"npm",
[
"--prefix",
this.applicationRoot,
"exec",
"--",
"biome",
"format",
"--write",
relativePath,
],
{
cwd: this.applicationRoot,
},
);
}
}

module.exports = {
ApplicationBiomeFormatter,
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ConstDeclarationUpdater {
}

if (declarations.length === 0) {
return;
return false;
}

const originalContent = await this.fileSystem.readFile(filePath, "utf8");
Expand All @@ -37,6 +37,8 @@ class ConstDeclarationUpdater {
if (hasChanges) {
await this.fileSystem.writeFile(filePath, updatedContent, "utf8");
}

return hasChanges;
}

resolveSerializedValue(declaration) {
Expand Down
1 change: 1 addition & 0 deletions .github/actions/generate-docs/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const IGNORED_REPOSITORIES = new Set([REPOSITORY_NAME]);

module.exports = {
OWNER,
APPLICATION_ROOT,
DOCS_DIR,
PROJECTS_PAGE_PATH,
HOMEPAGE_PATH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ const { resolveIcon } = require("../builders/resolve-icon");
const {
ConstDeclarationUpdater,
} = require("../builders/const-declaration-updater");
const {
ApplicationBiomeFormatter,
} = require("../builders/application-biome-formatter");

class HomepageProjectsUpdater {
constructor({ homepagePath }) {
constructor({ homepagePath, constDeclarationUpdater, formatter }) {
this.homepagePath = homepagePath;
this.constDeclarationUpdater = new ConstDeclarationUpdater();
this.constDeclarationUpdater =
constDeclarationUpdater ?? new ConstDeclarationUpdater();
this.formatter = formatter ?? new ApplicationBiomeFormatter();
}

async update(repositories, pinnedRepositoryNames = []) {
Expand All @@ -24,9 +29,14 @@ class HomepageProjectsUpdater {
}

const projectsModel = this.buildProjectsModel(featured);
await this.constDeclarationUpdater.update(this.homepagePath, [
{ name: "projects", value: projectsModel },
]);
const hasChanges = await this.constDeclarationUpdater.update(
this.homepagePath,
[{ name: "projects", value: projectsModel }],
);

if (hasChanges) {
await this.formatter.format(this.homepagePath);
}
}

pickFeaturedRepositories(repositories, pinnedRepositoryNames) {
Expand Down
Loading
Loading