Skip to content

Commit 648353b

Browse files
kodster28maxvp
authored andcommitted
[GitHub] Unused partials check (#24632)
* [GitHub] Unused partials check * logging * Test 2 * test 3 * test 4 * test 5 * test * more test * test * another test * [Chore] Add zero and 1 use partials to style guide page * update title for reg one * Change behavior a bit * eslint * fix error * test
1 parent 370f382 commit 648353b

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

src/components/PartialsUsage.astro

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,23 @@ import { getPartialsUsage } from "~/util/components";
44
import Details from "./Details.astro";
55
import UsageList from "./UsageList.astro";
66
7-
const partials = await getPartialsUsage();
7+
const allPartials = await getPartialsUsage();
8+
9+
const singleUsePartials = Object.fromEntries(
10+
Object.entries(allPartials).filter(([, value]) => value.count === 1),
11+
);
12+
13+
const zeroUsePartials = Object.fromEntries(
14+
Object.entries(allPartials).filter(([, value]) => value.count === 0),
15+
);
816
---
917

10-
<Details header="Usage" id="partials-container">
18+
<Details
19+
header={`All partials (${Object.keys(allPartials).length} files)`}
20+
id="partials-container"
21+
>
1122
{
12-
[...Object.entries(partials)]
23+
[...Object.entries(allPartials)]
1324
.sort((a, b) => a[0].localeCompare(b[0]))
1425
.map(([name, usage]) => (
1526
<Details
@@ -22,6 +33,36 @@ const partials = await getPartialsUsage();
2233
}
2334
</Details>
2435

36+
<br />
37+
38+
<Details
39+
header={`1 use partials (${Object.keys(singleUsePartials).length} files)`}
40+
id="one-use-partials-container"
41+
>
42+
{
43+
[...Object.entries(singleUsePartials)]
44+
.sort((a, b) => a[0].localeCompare(b[0]))
45+
.map(([name, usage]) => (
46+
<p>{`${name} (${usage.count} uses on ${usage.pages.size} pages)`}</p>
47+
))
48+
}
49+
</Details>
50+
51+
<br />
52+
53+
<Details
54+
header={`0 use partials (${Object.keys(zeroUsePartials).length} files)`}
55+
id="unused-partials-container"
56+
>
57+
{
58+
[...Object.entries(zeroUsePartials)]
59+
.sort((a, b) => a[0].localeCompare(b[0]))
60+
.map(([name, usage]) => (
61+
<p>{`${name} (${usage.count} uses on ${usage.pages.size} pages)`}</p>
62+
))
63+
}
64+
</Details>
65+
2566
<script>
2667
const params = new URLSearchParams(window.location.search);
2768
console.log(params);

src/util/components.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ export async function getPartialsUsage(): Promise<Record<string, Usage>> {
8484
withFileTypes: true,
8585
});
8686

87+
const partialEntities = await readdir("./src/content/partials/", {
88+
recursive: true,
89+
withFileTypes: true,
90+
});
91+
92+
// Populate all partials with zero usage
93+
const partialFiles = partialEntities.filter(
94+
(entity) => entity.isFile() && entity.name.endsWith(".mdx"),
95+
);
96+
for (const file of partialFiles) {
97+
const parentPath =
98+
process.platform === "win32"
99+
? file.parentPath.replaceAll("\\", "/")
100+
: file.parentPath;
101+
const product = parentPath.split("/")[3];
102+
const partialName = `${product}/${file.name.replace(".mdx", "")}`;
103+
partials[partialName] = { count: 0, pages: new Set() };
104+
}
105+
87106
const files = entities.filter(
88107
(entity) => entity.isFile() && entity.name.endsWith(".mdx"),
89108
);
@@ -132,6 +151,5 @@ export async function getPartialsUsage(): Promise<Record<string, Usage>> {
132151
});
133152
}
134153
}
135-
136154
return partials;
137155
}

0 commit comments

Comments
 (0)