@@ -2,151 +2,227 @@ import { execSync } from "node:child_process";
22import fs from "node:fs" ;
33import path from "node:path" ;
44
5- const RenamedFiles : { [ key : string ] : string } = { } ;
6- const PageRoutes : { [ key : string ] : string } = { } ;
5+ const FilesMap : { [ original : string ] : string } = { } ;
6+ const RouteMap : { [ original : string ] : string } = { } ;
7+ const FaviconMap : { [ original : string ] : string } = { } ;
78
8- export function randomizeName ( filePath : string ) : string {
9- const extname = path . extname ( filePath ) ;
10- return `${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 11 ) } ${ extname } ` ;
9+ export function RandomizeNames ( filePath : string ) : string {
10+ const extension = path . extname ( filePath ) ;
11+ return `${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 11 ) } ${ extension } ` ;
1112}
1213
13- export function findFiles ( dir : string , filePattern : RegExp ) : string [ ] {
14- let results : string [ ] = [ ] ;
15- const list = fs . readdirSync ( dir ) ;
14+ export function FindFiles ( directory : string , pattern : RegExp ) : string [ ] {
15+ let MatchedFiles : string [ ] = [ ] ;
16+ const files = fs . readdirSync ( directory ) ;
1617
17- for ( const file of list ) {
18- const resolvedFile = path . resolve ( dir , file ) ;
19- const stat = fs . statSync ( resolvedFile ) ;
18+ for ( const file of files ) {
19+ const FullPath = path . resolve ( directory , file ) ;
20+ const stats = fs . statSync ( FullPath ) ;
2021
21- if ( stat . isDirectory ( ) ) {
22- results = results . concat ( findFiles ( resolvedFile , filePattern ) ) ;
23- } else if ( filePattern . test ( file ) ) {
24- results . push ( resolvedFile ) ;
22+ if ( stats . isDirectory ( ) ) {
23+ MatchedFiles = MatchedFiles . concat ( FindFiles ( FullPath , pattern ) ) ;
24+ } else if ( pattern . test ( file ) ) {
25+ MatchedFiles . push ( FullPath ) ;
2526 }
2627 }
2728
28- return results ;
29+ return MatchedFiles ;
2930}
3031
31- export function RandomizeNames ( ) {
32- const filesToRename = [
33- ...findFiles ( path . join ( process . cwd ( ) , "src" , "components" ) , / \. a s t r o $ / ) ,
34- ...findFiles ( path . join ( process . cwd ( ) , "src" , "layouts" ) , / \. a s t r o $ / ) ,
35- ...findFiles ( path . join ( process . cwd ( ) , "src" , "lib" ) , / \. t s $ / ) ,
36- ...findFiles ( path . join ( process . cwd ( ) , "src" , "pages" ) , / \. a s t r o $ / ) ,
37- ...findFiles ( path . join ( process . cwd ( ) , "src" , "pages" , "e" ) , / \. t s $ / ) ,
32+ export function RandomizeFavicons ( ) {
33+ const FaviconDir = path . join (
34+ process . cwd ( ) ,
35+ "public" ,
36+ "assets" ,
37+ "media" ,
38+ "favicons" ,
39+ ) ;
40+
41+ if ( ! fs . existsSync ( FaviconDir ) ) {
42+ return ;
43+ }
44+
45+ const Favicons = fs . readdirSync ( FaviconDir ) ;
46+
47+ for ( const file of Favicons ) {
48+ const OriginalPath = path . join ( FaviconDir , file ) ;
49+ const RandomizedName = RandomizeNames ( file ) ;
50+ const NewPath = path . join ( FaviconDir , RandomizedName ) ;
51+
52+ fs . renameSync ( OriginalPath , NewPath ) ;
53+ FaviconMap [ file ] = RandomizedName ;
54+ }
55+
56+ UpdateFaviconRoutes ( ) ;
57+ }
58+
59+ export function UpdateFaviconRoutes ( ) {
60+ const FilesToUpdate = [
61+ ...FindFiles ( path . join ( process . cwd ( ) , "src" ) , / \. a s t r o $ / ) ,
62+ ...FindFiles ( path . join ( process . cwd ( ) , "src" ) , / \. t s $ / ) ,
63+ ] ;
64+
65+ for ( const file of FilesToUpdate ) {
66+ let content = fs . readFileSync ( file , "utf-8" ) ;
67+
68+ for ( const [ OldName , RandomizedName ] of Object . entries ( FaviconMap ) ) {
69+ const patterns = [
70+ `/assets/media/favicons/${ OldName } ` ,
71+ `assets/media/favicons/${ OldName } ` ,
72+ `'${ OldName } '` ,
73+ `"${ OldName } "` ,
74+ ] ;
75+
76+ for ( const pattern of patterns ) {
77+ content = content . replace (
78+ new RegExp ( pattern . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) , "g" ) ,
79+ pattern . startsWith ( "/" )
80+ ? `/assets/media/favicons/${ RandomizedName } `
81+ : pattern . startsWith ( "assets" )
82+ ? `assets/media/favicons/${ RandomizedName } `
83+ : pattern . startsWith ( "'" )
84+ ? `'${ RandomizedName } '`
85+ : `"${ RandomizedName } "` ,
86+ ) ;
87+ }
88+ }
89+
90+ fs . writeFileSync ( file , content , "utf-8" ) ;
91+ }
92+ }
93+
94+ export function Main ( ) {
95+ RandomizeFavicons ( ) ;
96+
97+ const FilesToProcess = [
98+ ...FindFiles ( path . join ( process . cwd ( ) , "src" , "components" ) , / \. a s t r o $ / ) ,
99+ ...FindFiles ( path . join ( process . cwd ( ) , "src" , "layouts" ) , / \. a s t r o $ / ) ,
100+ ...FindFiles ( path . join ( process . cwd ( ) , "src" , "lib" ) , / \. t s $ / ) ,
101+ ...FindFiles ( path . join ( process . cwd ( ) , "src" , "pages" ) , / \. a s t r o $ / ) ,
102+ ...FindFiles ( path . join ( process . cwd ( ) , "src" , "pages" , "e" ) , / \. t s $ / ) ,
38103 ] ;
39104
40- for ( const file of filesToRename ) {
105+ for ( const file of FilesToProcess ) {
41106 if ( path . basename ( file ) === "index.astro" ) {
42107 continue ;
43108 }
44109
45- const newName = randomizeName ( file ) ;
46- const oldPath = path . resolve ( file ) ;
47- const newPath = path . resolve ( path . dirname ( file ) , newName ) ;
48- RenamedFiles [ oldPath ] = newPath ;
49- fs . renameSync ( oldPath , newPath ) ;
110+ const RandomizedName = RandomizeNames ( file ) ;
111+ const OriginalPath = path . resolve ( file ) ;
112+ const NewPath = path . resolve ( path . dirname ( file ) , RandomizedName ) ;
113+
114+ FilesMap [ OriginalPath ] = NewPath ;
50115
51116 if ( file . startsWith ( path . join ( process . cwd ( ) , "src" , "pages" ) ) ) {
52- const oldRoute = oldPath
53- . replace ( `${ process . cwd ( ) } /src/pages` , "" )
54- . replace ( / \\ / g, "/" ) ;
55- const newRoute = newPath
56- . replace ( `${ process . cwd ( ) } /src/pages` , "" )
57- . replace ( / \\ / g, "/" ) ;
58- PageRoutes [ oldRoute ] = newRoute ;
117+ const OriginalRoute = OriginalPath . replace (
118+ path . join ( process . cwd ( ) , "src" , "pages" ) ,
119+ "" ,
120+ )
121+ . replace ( / \\ / g, "/" )
122+ . replace ( / \. a s t r o $ / , "" ) ;
123+
124+ const NewRoute = NewPath . replace ( path . join ( process . cwd ( ) , "src" , "pages" ) , "" )
125+ . replace ( / \\ / g, "/" )
126+ . replace ( / \. a s t r o $ / , "" ) ;
127+
128+ RouteMap [ OriginalRoute ] = NewRoute ;
129+
130+ if ( OriginalRoute . startsWith ( "/" ) ) {
131+ RouteMap [ OriginalRoute . substring ( 1 ) ] = NewRoute . startsWith ( "/" )
132+ ? NewRoute . substring ( 1 )
133+ : NewRoute ;
134+ } else {
135+ RouteMap [ OriginalRoute ] = NewRoute ;
136+ }
59137 }
60138 }
61139
62- updateImports ( ) ;
63- updatePageRoutes ( ) ;
140+ UpdateImports ( ) ;
141+ UpdateRoutes ( ) ;
142+
143+ for ( const [ OriginalPath , NewPath ] of Object . entries ( FilesMap ) ) {
144+ fs . renameSync ( OriginalPath , NewPath ) ;
145+ }
146+
147+ console . log ( "Routes updated:" , RouteMap ) ;
64148}
65149
66- export function updateImports ( ) {
67- const allFiles = [
68- ...findFiles ( path . join ( process . cwd ( ) , "src" ) , / \. a s t r o $ / ) ,
69- ...findFiles ( path . join ( process . cwd ( ) , "src" ) , / \. t s $ / ) ,
150+ export function UpdateRoutes ( ) {
151+ const FilesToUpdate = [
152+ ...FindFiles ( path . join ( process . cwd ( ) , "src" ) , / \. a s t r o $ / ) ,
153+ ...FindFiles ( path . join ( process . cwd ( ) , "src" ) , / \. t s $ / ) ,
70154 ] ;
71155
72- for ( const file of allFiles ) {
73- let fileContent = fs . readFileSync ( file , "utf-8" ) ;
74- const rootPath = process . cwd ( ) ;
75-
76- for ( const [ oldPath , newPath ] of Object . entries ( RenamedFiles ) ) {
77- const oldImportPathAlias = oldPath
78- . replace ( `${ rootPath } /src/components` , "@/components" )
79- . replace ( `${ rootPath } /src/layouts` , "@/layouts" )
80- . replace ( `${ rootPath } /src/lib` , "@/lib" )
81- . replace ( / \\ / g, "/" ) ;
82-
83- const newImportPathAlias = newPath
84- . replace ( `${ rootPath } /src/components` , "@/components" )
85- . replace ( `${ rootPath } /src/layouts` , "@/layouts" )
86- . replace ( `${ rootPath } /src/lib` , "@/lib" )
87- . replace ( / \\ / g, "/" ) ;
88-
89- const oldImportPathAbs = oldPath . replace ( rootPath , "" ) . replace ( / \\ / g, "/" ) ;
90-
91- const newImportPathAbs = newPath . replace ( rootPath , "" ) . replace ( / \\ / g, "/" ) ;
92-
93- fileContent = fileContent . replace (
94- new RegExp ( `['"]${ oldImportPathAlias } ['"]` , "g" ) ,
95- `'${ newImportPathAlias } '` ,
96- ) ;
97- fileContent = fileContent . replace (
98- new RegExp ( `['"]${ oldImportPathAbs } ['"]` , "g" ) ,
99- `'${ newImportPathAbs } '` ,
100- ) ;
156+ for ( const file of FilesToUpdate ) {
157+ let content = fs . readFileSync ( file , "utf-8" ) ;
158+
159+ for ( const [ OriginalRoute , NewRoute ] of Object . entries ( RouteMap ) ) {
160+ const routePattern = new RegExp ( `(['"\`])${ OriginalRoute } (['"\`])` , "g" ) ;
161+
162+ content = content . replace ( routePattern , `$1${ NewRoute } $2` ) ;
101163 }
102164
103- fs . writeFileSync ( file , fileContent , "utf-8" ) ;
165+ fs . writeFileSync ( file , content , "utf-8" ) ;
104166 }
105167}
106168
107- export function updatePageRoutes ( ) {
108- const allFiles = [
109- ...findFiles ( path . join ( process . cwd ( ) , "src" ) , / \. a s t r o $ / ) ,
110- ...findFiles ( path . join ( process . cwd ( ) , "src" ) , / \. t s $ / ) ,
169+ export function UpdateImports ( ) {
170+ const FilesToUpdate = [
171+ ...FindFiles ( path . join ( process . cwd ( ) , "src" ) , / \. a s t r o $ / ) ,
172+ ...FindFiles ( path . join ( process . cwd ( ) , "src" ) , / \. t s $ / ) ,
111173 ] ;
112174
113- for ( const file of allFiles ) {
114- let fileContent = fs . readFileSync ( file , "utf-8" ) ;
115-
116- for ( const [ oldRoute , newRoute ] of Object . entries ( PageRoutes ) ) {
117- fileContent = fileContent . replace (
118- new RegExp ( `['"]${ oldRoute . replace ( ".astro" , "" ) } ['"]` , "g" ) ,
119- `'${ newRoute . replace ( ".astro" , "" ) } '` ,
120- ) ;
175+ const root = process . cwd ( ) ;
176+
177+ for ( const file of FilesToUpdate ) {
178+ let content = fs . readFileSync ( file , "utf-8" ) ;
179+
180+ for ( const [ OriginalPath , NewPath ] of Object . entries ( FilesMap ) ) {
181+ const OriginalName = path . basename ( OriginalPath ) ;
182+ const RandomizedName = path . basename ( NewPath ) ;
183+
184+ const OriginalPatterns = [
185+ `@/components/${ OriginalName } ` ,
186+ `@/layouts/${ OriginalName } ` ,
187+ `@/lib/${ OriginalName } ` ,
188+ OriginalPath . replace ( root , "" ) . replace ( / \\ / g, "/" ) ,
189+ OriginalName ,
190+ ] ;
191+
192+ const NewPatterns = [
193+ `@/components/${ RandomizedName } ` ,
194+ `@/layouts/${ RandomizedName } ` ,
195+ `@/lib/${ RandomizedName } ` ,
196+ NewPath . replace ( root , "" ) . replace ( / \\ / g, "/" ) ,
197+ RandomizedName ,
198+ ] ;
199+
200+ OriginalPatterns . forEach ( ( pattern , index ) => {
201+ content = content . replace (
202+ new RegExp ( `['"]${ pattern . replace ( / \. / g, "\\." ) } ['"]` , "g" ) ,
203+ `'${ NewPatterns [ index ] } '` ,
204+ ) ;
205+ } ) ;
121206 }
122207
123- fs . writeFileSync ( file , fileContent , "utf-8" ) ;
124- }
125- }
126-
127- export function renameEDirectory ( ) {
128- const eDirPath = path . join ( process . cwd ( ) , "src" , "pages" , "e" ) ;
129- if ( fs . existsSync ( eDirPath ) ) {
130- const newEDirName = `/${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 6 ) } ` ;
131- const newEDirPath = path . join ( process . cwd ( ) , "src" , "pages" , newEDirName ) ;
132- fs . renameSync ( eDirPath , newEDirPath ) ;
208+ fs . writeFileSync ( file , content , "utf-8" ) ;
133209 }
134210}
135211
136212export async function Revert ( ) {
137213 try {
138214 console . log ( "Reverting Changes." ) ;
139- execSync ( "git restore src/" , { cwd : process . cwd ( ) , stdio : "inherit" } ) ;
140- execSync ( "git clean -fdx src/" , { cwd : process . cwd ( ) , stdio : "inherit" } ) ;
141-
142- await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
215+ execSync ( "git restore src/ public/" , { cwd : process . cwd ( ) , stdio : "inherit" } ) ;
216+ execSync ( "git clean -fdx src/ public/" , {
217+ cwd : process . cwd ( ) ,
218+ stdio : "inherit" ,
219+ } ) ;
220+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
143221 console . log ( "Revert completed." ) ;
144222 } catch ( error ) {
145223 console . error (
146- "Error during revert :" ,
224+ "Error while reverting :" ,
147225 error instanceof Error ? error . message : error ,
148226 ) ;
149227 }
150228}
151-
152- updateImports ( ) ;
0 commit comments