Skip to content

Commit 1b80e07

Browse files
committed
Randomize all file names, and then revert changes after building
1 parent 583d7e6 commit 1b80e07

File tree

2 files changed

+96
-46
lines changed

2 files changed

+96
-46
lines changed

index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ async function Start() {
1818
if (!fs.existsSync("dist")) {
1919
RandomizeNames();
2020
console.log("Interstellar's not built yet! Building now...");
21+
2122
await build({}).catch((err) => {
2223
console.error(err);
2324
process.exit(1);
2425
});
25-
Revert();
26+
27+
await Revert();
2628

2729
if (FirstRun) {
2830
console.log("Restarting Server...");

randomize.ts

Lines changed: 93 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,120 @@
1+
import { execSync } from "node:child_process";
12
import fs from "node:fs";
23
import path from "node:path";
34

4-
export const RenamedFiles: { [key: string]: string } = {};
5+
const RenamedFiles: { [key: string]: string } = {};
56

6-
export function RandomizeNames() {
7-
const pagesDir = path.join(process.cwd(), "src", "pages");
8-
const files = fs.readdirSync(pagesDir);
9-
10-
for (const file of files) {
11-
if (file !== "index.astro" && file.endsWith(".astro")) {
12-
const randomName = `${Math.random().toString(36).slice(2, 11)}.astro`;
13-
const oldPath = path.join(pagesDir, file);
14-
const newPath = path.join(pagesDir, randomName);
15-
RenamedFiles[`/${file.replace(".astro", "")}`] =
16-
`/${randomName.replace(".astro", "")}`;
17-
fs.renameSync(oldPath, newPath);
18-
}
19-
}
20-
21-
const AstroFiles = FindAstroFiles(process.cwd());
22-
for (const astroFile of AstroFiles) {
23-
let fileContent = fs.readFileSync(astroFile, "utf-8");
24-
25-
for (const [oldName, newName] of Object.entries(RenamedFiles)) {
26-
const regex = new RegExp(`"${oldName}"`, "g");
27-
fileContent = fileContent.replace(regex, `"${newName}"`);
28-
}
29-
30-
fs.writeFileSync(astroFile, fileContent, "utf-8");
31-
}
7+
export function randomizeName(filePath: string): string {
8+
const extname = path.extname(filePath);
9+
return `${Math.random().toString(36).slice(2, 11)}${extname}`;
3210
}
3311

34-
export function FindAstroFiles(dir: string): string[] {
12+
export function findFiles(dir: string, filePattern: RegExp): string[] {
3513
let results: string[] = [];
3614
const list = fs.readdirSync(dir);
3715

3816
for (const file of list) {
3917
const resolvedFile = path.resolve(dir, file);
4018
const stat = fs.statSync(resolvedFile);
4119

42-
if (stat?.isDirectory()) {
43-
results = results.concat(FindAstroFiles(resolvedFile));
44-
} else if (file.endsWith(".astro")) {
20+
if (stat.isDirectory()) {
21+
results = results.concat(findFiles(resolvedFile, filePattern));
22+
} else if (filePattern.test(file)) {
4523
results.push(resolvedFile);
4624
}
4725
}
26+
4827
return results;
4928
}
5029

51-
export function Revert() {
52-
const pagesDir = path.join(process.cwd(), "src", "pages");
30+
export function RandomizeNames() {
31+
const filesToRename = [
32+
...findFiles(path.join(process.cwd(), "src", "components"), /\.astro$/),
33+
...findFiles(path.join(process.cwd(), "src", "layouts"), /\.astro$/),
34+
...findFiles(path.join(process.cwd(), "src", "lib"), /\.ts$/),
35+
...findFiles(path.join(process.cwd(), "src", "pages"), /\.astro$/),
36+
...findFiles(path.join(process.cwd(), "src", "pages", "e"), /\.ts$/),
37+
];
5338

54-
for (const [oldPath, newPath] of Object.entries(RenamedFiles)) {
55-
fs.renameSync(
56-
path.join(pagesDir, `${newPath.replace("/", "")}.astro`),
57-
path.join(pagesDir, `${oldPath.replace("/", "")}.astro`),
58-
);
39+
for (const file of filesToRename) {
40+
if (path.basename(file) === "index.astro") {
41+
continue;
42+
}
43+
44+
const newName = randomizeName(file);
45+
const oldPath = path.resolve(file);
46+
const newPath = path.resolve(path.dirname(file), newName);
47+
RenamedFiles[oldPath] = newPath;
48+
fs.renameSync(oldPath, newPath);
5949
}
6050

61-
const AstroFiles = FindAstroFiles(process.cwd());
62-
for (const astroFile of AstroFiles) {
63-
let fileContent = fs.readFileSync(astroFile, "utf-8");
51+
updateImports();
52+
}
53+
54+
export function updateImports() {
55+
const allFiles = [
56+
...findFiles(path.join(process.cwd(), "src"), /\.astro$/),
57+
...findFiles(path.join(process.cwd(), "src"), /\.ts$/),
58+
];
6459

65-
for (const [oldName, newName] of Object.entries(RenamedFiles)) {
66-
const regex = new RegExp(`"${newName}"`, "g");
67-
fileContent = fileContent.replace(regex, `"${oldName}"`);
60+
for (const file of allFiles) {
61+
let fileContent = fs.readFileSync(file, "utf-8");
62+
const rootPath = process.cwd();
63+
64+
for (const [oldPath, newPath] of Object.entries(RenamedFiles)) {
65+
const oldImportPathAlias = oldPath
66+
.replace(`${rootPath}/src/components`, "@/components")
67+
.replace(`${rootPath}/src/layouts`, "@/layouts")
68+
.replace(`${rootPath}/src/lib`, "@/lib")
69+
.replace(/\\/g, "/");
70+
71+
const newImportPathAlias = newPath
72+
.replace(`${rootPath}/src/components`, "@/components")
73+
.replace(`${rootPath}/src/layouts`, "@/layouts")
74+
.replace(`${rootPath}/src/lib`, "@/lib")
75+
.replace(/\\/g, "/");
76+
77+
const oldImportPathAbs = oldPath.replace(rootPath, "").replace(/\\/g, "/");
78+
79+
const newImportPathAbs = newPath.replace(rootPath, "").replace(/\\/g, "/");
80+
81+
fileContent = fileContent.replace(
82+
new RegExp(`['"]${oldImportPathAlias}['"]`, "g"),
83+
`'${newImportPathAlias}'`,
84+
);
85+
fileContent = fileContent.replace(
86+
new RegExp(`['"]${oldImportPathAbs}['"]`, "g"),
87+
`'${newImportPathAbs}'`,
88+
);
6889
}
6990

70-
fs.writeFileSync(astroFile, fileContent, "utf-8");
91+
fs.writeFileSync(file, fileContent, "utf-8");
92+
}
93+
}
94+
95+
export function renameEDirectory() {
96+
const eDirPath = path.join(process.cwd(), "src", "pages", "e");
97+
if (fs.existsSync(eDirPath)) {
98+
const newEDirName = `/${Math.random().toString(36).slice(2, 6)}`;
99+
const newEDirPath = path.join(process.cwd(), "src", "pages", newEDirName);
100+
fs.renameSync(eDirPath, newEDirPath);
71101
}
72102
}
103+
104+
export async function Revert() {
105+
try {
106+
console.log("Reverting Changes.");
107+
execSync("git restore src/", { cwd: process.cwd(), stdio: "inherit" });
108+
execSync("git clean -fdx src/", { cwd: process.cwd(), stdio: "inherit" });
109+
110+
await new Promise((resolve) => setTimeout(resolve, 2000));
111+
console.log("Revert completed.");
112+
} catch (error) {
113+
console.error(
114+
"Error during revert:",
115+
error instanceof Error ? error.message : error,
116+
);
117+
}
118+
}
119+
120+
updateImports();

0 commit comments

Comments
 (0)