Skip to content

Commit 71c0d55

Browse files
committed
fixes
1 parent 4822dfa commit 71c0d55

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

docs/docs/botasaurus-desktop/pre-launch-checklist.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ Use your IDE's global search feature (<ShortCut/>+Shift+F) to replace all placeh
2525

2626
![Find and Replace Example](https://raw.githubusercontent.com/omkarcloud/botasaurus/master/images/pre-launch-checklist/find-replace-example.png)
2727
:::
28-
### 2. Replace Icon Assets
28+
29+
### 2. Upload to S3
30+
31+
If you haven't already, [upload installers to S3](./packaging-publishing.md)
32+
33+
### 3. Replace Icon Assets
2934

3035
Replace default icons with your own brand icons.
3136

@@ -109,7 +114,7 @@ If you don't have a brand icon yet and don't want to invest time in creating one
109114
Once you have users, you can invest time in creating a custom icon.
110115
:::
111116

112-
### 3. Add Customer Support
117+
### 4. Add Customer Support
113118

114119
Giving great customer support is your duty. Give it your best shot.
115120

js/botasaurus-server-js/src/server.ts

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/require\s*\(\s*['"`]botasaurus-controls['"`]\s*\)\s*;?/g, replacement);
57-
}
5846
type 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

6495
type 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

Comments
 (0)