|
// Import required modules using ESM syntax |
|
import fs from "fs/promises"; // Use the promises-based API |
|
import path from "path"; |
|
import process from "process"; // Required for argv and exit |
|
|
|
// --- Configuration --- |
|
|
|
// Get the filename from the command line arguments |
|
// process.argv[0] is 'node' |
|
// process.argv[1] is the script file (e.g., 'replace.js') |
|
// process.argv[2] is the first user-provided argument |
|
const FILENAME = process.argv[2]; |
|
|
|
// --- Validation --- |
|
if (!FILENAME) { |
|
console.error("Error: No filename provided."); |
|
console.log("Usage: node replace.js <your-target-file.js>"); |
|
process.exit(1); // Exit with a non-zero status code |
|
} |
|
|
|
// Resolve the absolute path based on the current working directory |
|
// This makes the script work regardless of where it's called from. |
|
const targetFilePath = path.resolve(FILENAME); |
|
|
|
/** |
|
* Async function to read, replace, and write file content. |
|
*/ |
|
async function runRobustReplace() { |
|
try { |
|
console.log(`[INFO] Reading file: ${targetFilePath}`); |
|
let fileContent = await fs.readFile(targetFilePath, "utf8"); |
|
|
|
// Save original content for comparison |
|
const originalContent = fileContent; |
|
|
|
// We will use a series of replacements to ensure we are operating |
|
// within the correct 'pro_plan_v1' block. |
|
// The 'm' flag allows regex to match across multiple lines. |
|
// [\s\S]*? is a non-greedy way to match any character, including newlines. |
|
|
|
// Replace blobLimit |
|
fileContent = fileContent.replace( |
|
/(pro_plan_v1:[\s\S]*?blobLimit:\s*)100 \* _base__WEBPACK_IMPORTED_MODULE_1__\.OneMB(,\s*)/m, |
|
"$110 * _base__WEBPACK_IMPORTED_MODULE_1__.OneGB$2" |
|
); |
|
|
|
// Replace storageQuota |
|
fileContent = fileContent.replace( |
|
/(pro_plan_v1:[\s\S]*?storageQuota:\s*)100 \* _base__WEBPACK_IMPORTED_MODULE_1__\.OneGB(,\s*)/m, |
|
"$11000 * _base__WEBPACK_IMPORTED_MODULE_1__.OneGB$2" |
|
); |
|
|
|
// Replace historyPeriod |
|
// fileContent = fileContent.replace( |
|
// /(pro_plan_v1:[\s\S]*?historyPeriod:\s*)30 \* _base__WEBPACK_IMPORTED_MODULE_1__\.OneDay(,\s*)/m, |
|
// "$13650 * _base__WEBPACK_IMPORTED_MODULE_1__.OneDay$2" |
|
// ); |
|
|
|
// Replace memberLimit |
|
fileContent = fileContent.replace( |
|
/(pro_plan_v1:[\s\S]*?memberLimit:\s*)10(,\s*)/m, |
|
"$11000$2" |
|
); |
|
|
|
// Check if any content actually changed |
|
if (originalContent === fileContent) { |
|
console.error("[ERROR] Could not find any matching lines to replace."); |
|
console.log("Please check your regex patterns and the file content."); |
|
return; |
|
} |
|
|
|
// Write the new content back to the file |
|
await fs.writeFile(targetFilePath, fileContent, "utf8"); |
|
console.log( |
|
"[SUCCESS] File content was successfully replaced using Regex!" |
|
); |
|
} catch (err) { |
|
// Handle potential errors, like "File Not Found" |
|
if (err.code === "ENOENT") { |
|
console.error(`[ERROR] File not found at path: ${targetFilePath}`); |
|
} else { |
|
console.error("[ERROR] An error occurred during the process:", err); |
|
} |
|
process.exit(1); // Exit with error |
|
} |
|
} |
|
|
|
// Run the main function |
|
runRobustReplace(); |