1- import { matchFile , REWRITE_PATTERN } from '@/rewrite/config'
1+ import { GROUPS } from '@/rewrite/config'
2+ import { analyze } from '@/rewrite/shared'
23import { chromium } from 'playwright-chromium'
3-
44import rules from '../public/rules/figma.json'
55
66const redirectRule = rules . find ( ( rule ) => rule . action . type === 'redirect' )
7-
87const ASSETS_PATTERN = new RegExp ( redirectRule ?. condition ?. regexFilter || / a ^ / )
98const MAX_RETRIES = 3
109
@@ -16,16 +15,16 @@ async function runCheck() {
1615
1716 try {
1817 page . on ( 'response' , async ( response ) => {
19- if ( response . request ( ) . resourceType ( ) === 'script' ) {
20- const url = response . url ( )
21- if ( ! ASSETS_PATTERN . test ( url ) ) {
22- return
23- }
24-
25- const content = await response . text ( )
26- scripts . push ( { url, content } )
27- console . log ( `Captured script: ${ url } ` )
18+ if ( response . request ( ) . resourceType ( ) !== 'script' ) {
19+ return
20+ }
21+ const url = response . url ( )
22+ if ( ! ASSETS_PATTERN . test ( url ) ) {
23+ return
2824 }
25+ const content = await response . text ( )
26+ scripts . push ( { url, content } )
27+ console . log ( `Captured script: ${ url } ` )
2928 } )
3029
3130 try {
@@ -43,7 +42,7 @@ async function runCheck() {
4342 try {
4443 await page . waitForURL ( / ^ (? ! .* l o g i n ) .* $ / , { timeout : 10000 } )
4544 console . log ( 'Logged in successfully.' )
46- } catch ( error ) {
45+ } catch {
4746 console . error ( 'Login failed. Please check your credentials.' )
4847 return false
4948 }
@@ -53,16 +52,20 @@ async function runCheck() {
5352
5453 let matched : string | null = null
5554 let rewritable = false
56- scripts . forEach ( ( { url, content } ) => {
57- if ( matchFile ( url , content ) ) {
58- matched = url
59- console . log ( `Matched script: ${ url } ` )
60- if ( REWRITE_PATTERN . test ( content ) ) {
61- rewritable = true
62- console . log ( `Rewritable script: ${ url } ` )
63- }
55+
56+ for ( const { url, content } of scripts ) {
57+ const { matched : m , rewritable : r } = analyze ( content , GROUPS )
58+ if ( ! m ) {
59+ continue
6460 }
65- } )
61+ matched = url
62+ console . log ( `Matched script: ${ url } ` )
63+ if ( r ) {
64+ rewritable = true
65+ console . log ( `Rewritable script: ${ url } ` )
66+ break
67+ }
68+ }
6669
6770 if ( ! matched ) {
6871 console . log ( '❌ No matched script found.' )
@@ -72,11 +75,11 @@ async function runCheck() {
7275 console . log ( `✅ Matched script: ${ matched } ` )
7376
7477 if ( ! rewritable ) {
75- console . log ( ` ❌ Rewrite pattern not found.` )
78+ console . log ( ' ❌ Rewrite pattern not found.' )
7679 return false
7780 }
7881
79- console . log ( ` ✅ Rewrite pattern found.` )
82+ console . log ( ' ✅ Rewrite pattern found.' )
8083 return true
8184 } finally {
8285 await browser . close ( )
@@ -90,14 +93,12 @@ async function main() {
9093 }
9194
9295 const success = await runCheck ( )
93-
9496 if ( success ) {
9597 process . exit ( 0 )
9698 }
9799
98100 if ( attempt < MAX_RETRIES ) {
99101 console . log ( `Attempt ${ attempt } failed. Waiting before retry...` )
100- // Wait a bit before retrying
101102 await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) )
102103 }
103104 }
0 commit comments