forked from UseInterstellar/Interstellar-Astro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomize.ts
231 lines (188 loc) · 6.42 KB
/
randomize.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { execSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
const FilesMap: { [original: string]: string } = {};
const RouteMap: { [original: string]: string } = {};
const FaviconMap: { [original: string]: string } = {};
export function RandomizeNames(filePath: string): string {
const extension = path.extname(filePath);
return `${Math.random().toString(36).slice(2, 11)}${extension}`;
}
export function FindFiles(directory: string, pattern: RegExp): string[] {
let MatchedFiles: string[] = [];
const files = fs.readdirSync(directory);
for (const file of files) {
const FullPath = path.resolve(directory, file);
const stats = fs.statSync(FullPath);
if (stats.isDirectory()) {
MatchedFiles = MatchedFiles.concat(FindFiles(FullPath, pattern));
} else if (pattern.test(file)) {
MatchedFiles.push(FullPath);
}
}
return MatchedFiles;
}
export function RandomizeFavicons() {
const FaviconDir = path.join(
process.cwd(),
"public",
"assets",
"media",
"favicons",
);
if (!fs.existsSync(FaviconDir)) {
return;
}
const Favicons = fs.readdirSync(FaviconDir);
for (const file of Favicons) {
const OriginalPath = path.join(FaviconDir, file);
const RandomizedName = RandomizeNames(file);
const NewPath = path.join(FaviconDir, RandomizedName);
fs.renameSync(OriginalPath, NewPath);
FaviconMap[file] = RandomizedName;
}
UpdateFaviconRoutes();
}
export function UpdateFaviconRoutes() {
const FilesToUpdate = [
...FindFiles(path.join(process.cwd(), "src"), /\.astro$/),
...FindFiles(path.join(process.cwd(), "src"), /\.ts$/),
];
for (const file of FilesToUpdate) {
let content = fs.readFileSync(file, "utf-8");
for (const [OldName, RandomizedName] of Object.entries(FaviconMap)) {
const patterns = [
`/assets/media/favicons/${OldName}`,
`assets/media/favicons/${OldName}`,
`'${OldName}'`,
`"${OldName}"`,
];
for (const pattern of patterns) {
content = content.replace(
new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g"),
pattern.startsWith("/")
? `/assets/media/favicons/${RandomizedName}`
: pattern.startsWith("assets")
? `assets/media/favicons/${RandomizedName}`
: pattern.startsWith("'")
? `'${RandomizedName}'`
: `"${RandomizedName}"`,
);
}
}
fs.writeFileSync(file, content, "utf-8");
}
}
export function Main() {
RandomizeFavicons();
const FilesToProcess = [
...FindFiles(path.join(process.cwd(), "src", "components"), /\.astro$/),
...FindFiles(path.join(process.cwd(), "src", "layouts"), /\.astro$/),
...FindFiles(path.join(process.cwd(), "src", "lib"), /\.ts$/),
...FindFiles(path.join(process.cwd(), "src", "pages"), /\.astro$/),
...FindFiles(path.join(process.cwd(), "src", "pages", "e"), /\.ts$/),
];
for (const file of FilesToProcess) {
if (path.basename(file) === "index.astro") {
continue;
}
const RandomizedName = RandomizeNames(file);
const OriginalPath = path.resolve(file);
const NewPath = path.resolve(path.dirname(file), RandomizedName);
FilesMap[OriginalPath] = NewPath;
if (file.startsWith(path.join(process.cwd(), "src", "pages"))) {
const OriginalRoute = OriginalPath.replace(
path.join(process.cwd(), "src", "pages"),
"",
)
.replace(/\\/g, "/")
.replace(/\.astro$/, "");
const NewRoute = NewPath.replace(path.join(process.cwd(), "src", "pages"), "")
.replace(/\\/g, "/")
.replace(/\.astro$/, "");
RouteMap[OriginalRoute] = NewRoute;
if (OriginalRoute.startsWith("/")) {
RouteMap[OriginalRoute.substring(1)] = NewRoute.startsWith("/")
? NewRoute.substring(1)
: NewRoute;
} else {
RouteMap[OriginalRoute] = NewRoute;
}
}
}
UpdateImports();
UpdateRoutes();
for (const [OriginalPath, NewPath] of Object.entries(FilesMap)) {
fs.renameSync(OriginalPath, NewPath);
}
console.log("Routes updated:", RouteMap);
}
export function UpdateRoutes() {
const FilesToUpdate = [
...FindFiles(path.join(process.cwd(), "src"), /\.astro$/),
...FindFiles(path.join(process.cwd(), "src"), /\.ts$/),
];
for (const file of FilesToUpdate) {
let content = fs.readFileSync(file, "utf-8");
for (const [OriginalRoute, NewRoute] of Object.entries(RouteMap)) {
const routePattern = new RegExp(`(['"\`])${OriginalRoute}(['"\`])`, "g");
content = content.replace(routePattern, `$1${NewRoute}$2`);
}
fs.writeFileSync(file, content, "utf-8");
}
}
export function UpdateImports() {
const FilesToUpdate = [
...FindFiles(path.join(process.cwd(), "src"), /\.astro$/),
...FindFiles(path.join(process.cwd(), "src"), /\.ts$/),
];
const root = process.cwd();
for (const file of FilesToUpdate) {
let content = fs.readFileSync(file, "utf-8");
for (const [OriginalPath, NewPath] of Object.entries(FilesMap)) {
const OriginalName = path.basename(OriginalPath);
const RandomizedName = path.basename(NewPath);
const OriginalPatterns = [
`@/components/${OriginalName}`,
`@/layouts/${OriginalName}`,
`@/lib/${OriginalName}`,
OriginalPath.replace(root, "").replace(/\\/g, "/"),
OriginalName,
];
const NewPatterns = [
`@/components/${RandomizedName}`,
`@/layouts/${RandomizedName}`,
`@/lib/${RandomizedName}`,
NewPath.replace(root, "").replace(/\\/g, "/"),
RandomizedName,
];
OriginalPatterns.forEach((pattern, index) => {
content = content.replace(
new RegExp(
`['"]${pattern.replace(/\\/g, "\\\\").replace(/\./g, "\\.")}['"]`,
"g",
),
`'${NewPatterns[index]}'`,
);
});
}
fs.writeFileSync(file, content, "utf-8");
}
}
export async function Revert() {
try {
console.log("Reverting Changes.");
execSync("git restore src/ public/", { cwd: process.cwd(), stdio: "inherit" });
execSync("git clean -fdx src/ public/", {
cwd: process.cwd(),
stdio: "inherit",
});
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log("Revert completed.");
} catch (error) {
console.error(
"Error while reverting:",
error instanceof Error ? error.message : error,
);
}
}