@@ -42,24 +42,55 @@ export function getScraperErrorMessage(validScraperNames: string[], scraperName:
4242}
4343
4444
45- /**
46- * Replaces require statements with a specified JSON object in the given JavaScript code.
47- *
48- * @param {string } code - The JavaScript code as a string.
49- * @returns {string } - The modified JavaScript code.
50- */
51- function replaceRequireWithJSON ( code :string ) {
52- // Define the JSON object to replace the require statement
53- const replacement = JSON . stringify ( { "FileTypes" :FileTypes } ) ;
5445
55- // Replace require statements with the specified JSON object
56- return code . replace ( / r e q u i r e \s * \( \s * [ ' " ` ] b o t a s a u r u s - c o n t r o l s [ ' " ` ] \s * \) \s * ; ? / g, replacement ) ;
57- }
5846type WhatsAppSupportOptions = {
5947 number : string ; // 10-digit phone number (without country code)
6048 countryCallingCode : string ; // Country calling code (e.g., 81 for Japan, 1 for the US)
6149 message : string ; // Default message for WhatsApp
6250} ;
51+ /**
52+ * Replaces FileTypes constants (e.g., FileTypes.IMAGE) in a string of code
53+ * with their corresponding JSON array values.
54+ *
55+ * @param {string } code - The JavaScript/TypeScript code as a string.
56+ * @returns {string } - The modified code with FileTypes replaced, or the original code if no replacements are needed.
57+ */
58+ function replaceFileTypesInCode ( code : string ) : string {
59+ // 1. Gatekeeper: If the code doesn't mention 'FileTypes', do nothing.
60+ // This is a quick exit for efficiency.
61+ if ( ! code . includes ( 'FileTypes' ) ) {
62+ return code ;
63+ }
64+
65+ let modifiedCode = code ;
66+
67+ // 2. Iterate over each key in our FileTypes object (e.g., "IMAGE", "EXCEL").
68+ for ( const key of Object . keys ( FileTypes ) ) {
69+ // TypeScript needs this assertion to know 'key' is a valid key of FileTypes
70+ const value = FileTypes [ key as keyof typeof FileTypes ] ;
71+
72+ // Convert the array to its JSON string representation.
73+ // e.g., for 'IMAGE', this becomes "['jpeg','jpg','png',...]"
74+ const replacementString = JSON . stringify ( value ) ;
75+
76+ // 3. Create robust regular expressions to find all occurrences.
77+ // We create two regexes to handle both common ways of accessing object properties.
78+
79+ // Regex for dot notation: FileTypes.IMAGE
80+ // \b is a word boundary to prevent matching something like "MyFileTypes.IMAGE"
81+ const dotNotationRegex = new RegExp ( `\\bFileTypes\\.${ key } \\b` , 'g' ) ;
82+
83+ // Regex for bracket notation: FileTypes['IMAGE'], FileTypes["IMAGE"], FileTypes[`IMAGE`]
84+ // It handles optional whitespace around the brackets and quotes.
85+ const bracketNotationRegex = new RegExp ( `\\bFileTypes\\[\\s*['"\`]${ key } ['"\`]\\s*\\]` , 'g' ) ;
86+
87+ // 4. Perform the replacements.
88+ modifiedCode = modifiedCode . replace ( dotNotationRegex , replacementString ) ;
89+ modifiedCode = modifiedCode . replace ( bracketNotationRegex , replacementString ) ;
90+ }
91+
92+ return modifiedCode ;
93+ }
6394
6495type EmailSupportOptions = {
6596 email : string ; // Support email address
@@ -351,7 +382,7 @@ class _Server {
351382 let inputJs : string | null = null ;
352383
353384 if ( fs . existsSync ( inputJsPath ) ) {
354- inputJs = replaceRequireWithJSON ( fs . readFileSync ( inputJsPath , 'utf-8' ) ) ;
385+ inputJs = replaceFileTypesInCode ( fs . readFileSync ( inputJsPath , 'utf-8' ) ) ;
355386 } else {
356387 const scraperFilePath = getInputFilePath ( scraperName )
357388 throw new Error ( `Input js file not found for ${ scraperName } , at path ${ scraperFilePath } . Kindly create it.` ) ;
0 commit comments