Skip to content

Commit c9630aa

Browse files
committed
refactor: Made a small script to help me keep the game list sorted in the code, to be able to remove/add games easier
1 parent 35bbf84 commit c9630aa

File tree

11 files changed

+7268
-7423
lines changed

11 files changed

+7268
-7423
lines changed

package-lock.json

Lines changed: 2395 additions & 1748 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"clsx": "^1.1.1",
2222
"prism-react-renderer": "^1.3.1",
2323
"react": "^17.0.2",
24-
"react-dom": "^17.0.2"
24+
"react-dom": "^17.0.2",
25+
"ts-morph": "^25.0.1"
2526
},
2627
"devDependencies": {
2728
"@docusaurus/module-type-aliases": "2.0.0-beta.20",

sort-games.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import {
2+
Project,
3+
SyntaxKind,
4+
ObjectLiteralExpression,
5+
ArrayLiteralExpression,
6+
} from "ts-morph";
7+
8+
console.clear();
9+
10+
// Initialize a project (adjust the tsconfig path as needed)
11+
const project = new Project({
12+
tsConfigFilePath: "./tsconfig.json",
13+
});
14+
15+
// Load the source file you want to modify
16+
// Replace 'src/data.ts' with the path to your TypeScript file
17+
const sourceFile = project.getSourceFileOrThrow(
18+
"src/components/games.information.tsx"
19+
);
20+
21+
// Find the variable declaration named 'GamesInformation'
22+
const variableDeclaration =
23+
sourceFile.getVariableDeclaration("GamesInformation");
24+
if (!variableDeclaration) {
25+
console.error("Variable 'GamesInformation' not found in the source file.");
26+
process.exit(1);
27+
}
28+
29+
// Get the initializer; we expect it to be an array literal
30+
const initializer = variableDeclaration.getInitializer();
31+
if (
32+
!initializer ||
33+
initializer.getKind() !== SyntaxKind.ArrayLiteralExpression
34+
) {
35+
console.error(
36+
"The initializer for 'GamesInformation' is not an array literal."
37+
);
38+
process.exit(1);
39+
}
40+
41+
// Now that we know it’s an array literal, cast it appropriately
42+
const arrayLiteral = initializer.asKind(SyntaxKind.ArrayLiteralExpression);
43+
44+
// Get all elements from the array literal
45+
const elements = arrayLiteral.getElements();
46+
47+
// Helper function: Extract the sort key from an element.
48+
// Adjust this if your objects have a different structure or property.
49+
function getSortKey(element: any): number {
50+
// We expect each element to be an object literal
51+
if (element.getKind() === SyntaxKind.ObjectLiteralExpression) {
52+
const objLiteral = element as ObjectLiteralExpression;
53+
// Look for the property assignment named 'id'
54+
const idProp = objLiteral.getProperty("id");
55+
if (idProp && idProp.getKind() === SyntaxKind.PropertyAssignment) {
56+
// Get the initializer (the value) of the property
57+
const initializer = idProp.getFirstDescendantByKind(
58+
SyntaxKind.NumericLiteral
59+
);
60+
if (initializer) {
61+
return initializer.getLiteralValue();
62+
}
63+
}
64+
}
65+
// Fallback: return an empty string if the structure isn’t as expected
66+
return 0;
67+
}
68+
69+
// Sort the elements by the 'id' property
70+
const sortedElements = elements.sort((a, b) => {
71+
const keyA = getSortKey(a);
72+
const keyB = getSortKey(b);
73+
const sortValue = keyA > keyB ? 1 : keyA < keyB ? -1 : 0;
74+
return sortValue;
75+
});
76+
77+
// Replace the array literal's elements with the sorted ones.
78+
// We map each element back to its source text.
79+
80+
const sortedArrayText = `[${sortedElements
81+
.map((el) => el.getText())
82+
.join(", ")}]`;
83+
84+
// Replace the entire initializer text with the sorted array
85+
arrayLiteral.replaceWithText(sortedArrayText);
86+
87+
// Save changes back to the source file
88+
sourceFile.saveSync();
89+
90+
console.log("Array sorted and file updated successfully!");

src/components/HomepageFeatures/index.tsx

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,62 @@ import clsx from "clsx";
33
import styles from "./styles.module.css";
44

55
type FeatureItem = {
6-
title: string;
7-
Svg?: React.ComponentType<React.ComponentProps<"svg">>;
8-
description: JSX.Element;
6+
title: string;
7+
Svg?: React.ComponentType<React.ComponentProps<"svg">>;
8+
description: JSX.Element;
99
};
1010

1111
const FeatureList: FeatureItem[] = [
12-
{
13-
title: "We have The Tools",
14-
Svg: require("@site/static/img/undraw_docusaurus_mountain.svg").default,
15-
description: (
16-
<>
17-
Find all the <a href="/tools">CurseForge community tools</a> here, and
18-
if something is missing, don't be afraid to tell us about it, so that we
19-
can add it to this site.
20-
</>
21-
),
22-
},
23-
{
24-
title: "CurseForge Games Support Information",
25-
description: (
26-
<>
27-
Find information about the games supported by CurseForge, and the
28-
platforms that they are available on.<br />
29-
<a href="/games">CurseForge Games</a>
30-
</>
31-
),
32-
}
12+
{
13+
title: "We have The Tools",
14+
Svg: require("@site/static/img/undraw_docusaurus_mountain.svg").default,
15+
description: (
16+
<>
17+
Find all the <a href="/tools">CurseForge community tools</a>{" "}
18+
here, and if something is missing, don't be afraid to tell us
19+
about it, so that we can add it to this site.
20+
</>
21+
),
22+
},
23+
{
24+
title: "CurseForge Games Support Information",
25+
description: (
26+
<>
27+
Find information about the games supported by CurseForge, and
28+
the platforms that they are available on.
29+
<br />
30+
<a href="/games">CurseForge Games</a>
31+
</>
32+
),
33+
},
3334
];
3435

3536
function Feature({ title, Svg, description }: FeatureItem) {
36-
return (
37-
<div className={clsx("col col--12")}>
38-
{Svg ?
39-
<div className="text--center">
40-
<Svg className={styles.featureSvg} role="img" />
37+
return (
38+
<div className={clsx("col col--12")}>
39+
{Svg ? (
40+
<div className="text--center">
41+
<Svg className={styles.featureSvg} role="img" />
42+
</div>
43+
) : null}
44+
<div className="text--center padding-horiz--md">
45+
<h3>{title}</h3>
46+
<p>{description}</p>
47+
</div>
4148
</div>
42-
: null}
43-
<div className="text--center padding-horiz--md">
44-
<h3>{title}</h3>
45-
<p>{description}</p>
46-
</div>
47-
</div>
48-
);
49+
);
4950
}
5051

5152
export default function HomepageFeatures(): JSX.Element {
52-
return (
53-
<section className={styles.features}>
54-
<div className="container">
55-
<div className="row">
56-
{FeatureList.map((props, idx) => (
57-
<Feature key={idx} {...props} />
58-
))}
59-
</div>
60-
</div>
61-
</section>
62-
);
53+
return (
54+
<section className={styles.features}>
55+
<div className="container">
56+
<div className="row">
57+
{FeatureList.map((props, idx) => (
58+
<Feature key={idx} {...props} />
59+
))}
60+
</div>
61+
</div>
62+
</section>
63+
);
6364
}

0 commit comments

Comments
 (0)