|
| 1 | +import { execSync } from "node:child_process"; |
1 | 2 | import fs from "node:fs"; |
2 | 3 | import path from "node:path"; |
3 | 4 |
|
4 | | -export const RenamedFiles: { [key: string]: string } = {}; |
| 5 | +const RenamedFiles: { [key: string]: string } = {}; |
5 | 6 |
|
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}`; |
32 | 10 | } |
33 | 11 |
|
34 | | -export function FindAstroFiles(dir: string): string[] { |
| 12 | +export function findFiles(dir: string, filePattern: RegExp): string[] { |
35 | 13 | let results: string[] = []; |
36 | 14 | const list = fs.readdirSync(dir); |
37 | 15 |
|
38 | 16 | for (const file of list) { |
39 | 17 | const resolvedFile = path.resolve(dir, file); |
40 | 18 | const stat = fs.statSync(resolvedFile); |
41 | 19 |
|
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)) { |
45 | 23 | results.push(resolvedFile); |
46 | 24 | } |
47 | 25 | } |
| 26 | + |
48 | 27 | return results; |
49 | 28 | } |
50 | 29 |
|
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 | + ]; |
53 | 38 |
|
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); |
59 | 49 | } |
60 | 50 |
|
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 | + ]; |
64 | 59 |
|
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 | + ); |
68 | 89 | } |
69 | 90 |
|
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); |
71 | 101 | } |
72 | 102 | } |
| 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