|
| 1 | +const fs = require("fs"); |
| 2 | +const readline = require("readline"); |
| 3 | +const path = require("path"); |
| 4 | + |
| 5 | +// Function to update the file content |
| 6 | +function updateFileContent(filePath, replacements) { |
| 7 | + fs.readFile(filePath, "utf8", (err, data) => { |
| 8 | + if (err) { |
| 9 | + console.error(`Error reading file ${filePath}:`, err); |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + let updatedContent = data |
| 14 | + .split("\n") |
| 15 | + .map((line) => { |
| 16 | + for (const [pattern, value] of Object.entries(replacements)) { |
| 17 | + // console.log(`line`, line); |
| 18 | + if (line.includes(pattern)) { |
| 19 | + console.log(`Replacing ${pattern} with ${value}`); |
| 20 | + return updateLine(line, value); |
| 21 | + } |
| 22 | + } |
| 23 | + return line; |
| 24 | + }) |
| 25 | + .join("\n"); |
| 26 | + |
| 27 | + fs.writeFile(filePath, updatedContent, "utf8", (err) => { |
| 28 | + if (err) { |
| 29 | + console.error(`Error writing file ${filePath}:`, err); |
| 30 | + } else { |
| 31 | + console.log(`File ${filePath} updated successfully.`); |
| 32 | + } |
| 33 | + }); |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +// List of files to be updated |
| 38 | +const filesToUpdate = [ |
| 39 | + // ui-library-filesharing-chat-composite |
| 40 | + "../ui-library-filesharing-chat-composite/api/local.settings.json", |
| 41 | + "../ui-library-filesharing-chat-composite/app/src/App.tsx", |
| 42 | + |
| 43 | + |
| 44 | + // Add more file paths as needed |
| 45 | +]; |
| 46 | + |
| 47 | +// Function to prompt the user for input |
| 48 | +function promptUser(query) { |
| 49 | + const rl = readline.createInterface({ |
| 50 | + input: process.stdin, |
| 51 | + output: process.stdout, |
| 52 | + }); |
| 53 | + |
| 54 | + return new Promise((resolve) => |
| 55 | + rl.question(query, (answer) => { |
| 56 | + rl.close(); |
| 57 | + resolve(answer); |
| 58 | + }) |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +// Main function to run the script |
| 63 | +async function main() { |
| 64 | + const endpointUrl = await promptUser( |
| 65 | + "Enter Azure Communication Services Resource Endpoint: " |
| 66 | + ); |
| 67 | + const azureStorageConnectionString = await promptUser( |
| 68 | + "Enter Azure Communication Services Storage Connection String: " |
| 69 | + ); |
| 70 | + const token = await promptUser( |
| 71 | + "Enter Azure Communication Services Resource Access Token: " |
| 72 | + ); |
| 73 | + const userId = await promptUser("Enter User Id associated to the token: "); |
| 74 | + const threadId = await promptUser( |
| 75 | + "Enter Azure Communication Services thread id: " |
| 76 | + ); |
| 77 | + const teamsMeetingLink = await promptUser("Enter Teams Meeting link: "); |
| 78 | + const displayName = await promptUser("Enter Display Name: "); |
| 79 | + |
| 80 | + const replacements = { |
| 81 | + "const ENDPOINT_URL =": endpointUrl, |
| 82 | + "const TOKEN =": token, |
| 83 | + "const USER_ID =": userId, |
| 84 | + "const DISPLAY_NAME =": displayName, |
| 85 | + '"azureStorageConnectionString":': azureStorageConnectionString, |
| 86 | + "const TEAMS_MEETING_LINK =": teamsMeetingLink, |
| 87 | + "const THREAD_ID =": threadId; |
| 88 | + }; |
| 89 | + console.log(replacements); |
| 90 | + filesToUpdate.forEach((filePath) => { |
| 91 | + updateFileContent(filePath, replacements); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +const defaultValues = { |
| 96 | + "const ENDPOINT_URL =": "<Azure Communication Services Resource Endpoint>", |
| 97 | + "const TOKEN =": "<Azure Communication Services Resource Access Token>", |
| 98 | + "const USER_ID =": "<User Id associated to the token>", |
| 99 | + "const DISPLAY_NAME =": "<Display Name>", |
| 100 | + '"azureStorageConnectionString":': "<CONNECTION_STRING>", |
| 101 | + "const TEAMS_MEETING_LINK =": "<Teams Meeting Link>", |
| 102 | + "const THREAD_ID =": "<Get thread id from chat service>", |
| 103 | +}; |
| 104 | + |
| 105 | +const updateLine = (line, value) => { |
| 106 | + const lastChar = line[line.length - 1]; |
| 107 | + const shouldAddLastChar = /[,;]/.test(lastChar); |
| 108 | + const usesEqualSign = line.includes(" = "); |
| 109 | + |
| 110 | + const [key] = usesEqualSign ? line.split("=") : line.split(":"); |
| 111 | + console.log(`key`, key, value); |
| 112 | + return `${key}${usesEqualSign ? "=" : ":"} \"${value}\"${ |
| 113 | + shouldAddLastChar ? lastChar : "" |
| 114 | + }`; |
| 115 | +}; |
| 116 | + |
| 117 | +// Function to restore files from backups |
| 118 | +function restoreBackups() { |
| 119 | + filesToUpdate.forEach((filePath) => { |
| 120 | + const currentContent = fs.readFileSync(filePath, "utf8").split("\n"); |
| 121 | + const patterns = Object.keys(defaultValues); |
| 122 | + |
| 123 | + const restoredContent = currentContent |
| 124 | + .map((line) => { |
| 125 | + const matchingPattern = patterns.find((pattern) => |
| 126 | + line.includes(pattern) |
| 127 | + ); |
| 128 | + if (matchingPattern) { |
| 129 | + return updateLine(line, defaultValues[matchingPattern]); |
| 130 | + } |
| 131 | + return line; |
| 132 | + }) |
| 133 | + .join("\n"); |
| 134 | + |
| 135 | + fs.writeFileSync(filePath, restoredContent, "utf8"); |
| 136 | + console.log(`File ${filePath} restored from backup.`); |
| 137 | + }); |
| 138 | +} |
| 139 | + |
| 140 | +// Check if the script should restore backups |
| 141 | +if (process.argv.includes("--restore")) { |
| 142 | + restoreBackups(); |
| 143 | +} else { |
| 144 | + main(); |
| 145 | +} |
0 commit comments