Skip to content

Commit e31a4c0

Browse files
committed
Overwrite page routes when building
1 parent 1b80e07 commit e31a4c0

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

randomize.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from "node:fs";
33
import path from "node:path";
44

55
const RenamedFiles: { [key: string]: string } = {};
6+
const PageRoutes: { [key: string]: string } = {};
67

78
export function randomizeName(filePath: string): string {
89
const extname = path.extname(filePath);
@@ -46,9 +47,16 @@ export function RandomizeNames() {
4647
const newPath = path.resolve(path.dirname(file), newName);
4748
RenamedFiles[oldPath] = newPath;
4849
fs.renameSync(oldPath, newPath);
50+
51+
if (file.startsWith(path.join(process.cwd(), "src", "pages"))) {
52+
const oldRoute = oldPath.replace(process.cwd() + "/src/pages", "").replace(/\\/g, "/");
53+
const newRoute = newPath.replace(process.cwd() + "/src/pages", "").replace(/\\/g, "/");
54+
PageRoutes[oldRoute] = newRoute;
55+
}
4956
}
5057

5158
updateImports();
59+
updatePageRoutes();
5260
}
5361

5462
export function updateImports() {
@@ -92,6 +100,26 @@ export function updateImports() {
92100
}
93101
}
94102

103+
export function updatePageRoutes() {
104+
const allFiles = [
105+
...findFiles(path.join(process.cwd(), "src"), /\.astro$/),
106+
...findFiles(path.join(process.cwd(), "src"), /\.ts$/),
107+
];
108+
109+
for (const file of allFiles) {
110+
let fileContent = fs.readFileSync(file, "utf-8");
111+
112+
for (const [oldRoute, newRoute] of Object.entries(PageRoutes)) {
113+
fileContent = fileContent.replace(
114+
new RegExp(`['"]${oldRoute.replace(".astro", "")}['"]`, "g"),
115+
`'${newRoute.replace(".astro", "")}'`
116+
);
117+
}
118+
119+
fs.writeFileSync(file, fileContent, "utf-8");
120+
}
121+
}
122+
95123
export function renameEDirectory() {
96124
const eDirPath = path.join(process.cwd(), "src", "pages", "e");
97125
if (fs.existsSync(eDirPath)) {
@@ -105,7 +133,7 @@ export async function Revert() {
105133
try {
106134
console.log("Reverting Changes.");
107135
execSync("git restore src/", { cwd: process.cwd(), stdio: "inherit" });
108-
execSync("git clean -fdx src/", { cwd: process.cwd(), stdio: "inherit" });
136+
execSync("git clean -fdx src/", { cwd: process.cwd(), stdio: "inherit" });
109137

110138
await new Promise((resolve) => setTimeout(resolve, 2000));
111139
console.log("Revert completed.");

0 commit comments

Comments
 (0)