Skip to content

Commit 5126081

Browse files
Added a script to update ui library variables
1 parent baea361 commit 5126081

File tree

2 files changed

+158
-13
lines changed

2 files changed

+158
-13
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
}

ui-library-filesharing-chat-composite/app/src/App.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,35 @@ function App(): JSX.Element {
1818
initializeFileTypeIcons();
1919

2020
// Common variables
21-
const endpointUrl = "<Azure Communication Services Resource Endpoint>";
22-
const token = "<Azure Communication Services Resource Access Token>";
23-
const userId = "<User Id associated to the token>";
24-
const threadId = "<Get thread id from chat service>";
25-
const displayName = "<Display Name>";
21+
const ENDPOINT_URL = "<Azure Communication Services Resource Endpoint>";
22+
const TOKEN = "<Azure Communication Services Resource Access Token>";
23+
const USER_ID = "<User Id associated to the token>";
24+
const THREAD_ID = "<Get thread id from chat service>";
25+
const DISPLAY_NAME = "<Display Name>";
2626

2727
// We can't even initialize the Chat and Call adapters without a well-formed token.
2828
const credential = useMemo(() => {
2929
try {
30-
return new AzureCommunicationTokenCredential(token);
30+
return new AzureCommunicationTokenCredential(TOKEN);
3131
} catch {
3232
console.error("Failed to construct token credential");
3333
return undefined;
3434
}
35-
}, [token]);
35+
}, [TOKEN]);
3636

3737
// Memoize arguments to `useAzureCommunicationChatAdapter` so that
3838
// a new adapter is only created when an argument changes.
3939
const chatAdapterArgs = useMemo(
4040
() => ({
41-
endpoint: endpointUrl,
41+
endpoint: ENDPOINT_URL,
4242
userId: fromFlatCommunicationIdentifier(
43-
userId
43+
USER_ID
4444
) as CommunicationUserIdentifier,
45-
displayName,
45+
DISPLAY_NAME,
4646
credential,
47-
threadId,
47+
THREAD_ID,
4848
}),
49-
[userId, displayName, credential, threadId]
49+
[USER_ID, DISPLAY_NAME, credential, THREAD_ID]
5050
);
5151
const chatAdapter = useAzureCommunicationChatAdapter(chatAdapterArgs);
5252

@@ -59,7 +59,7 @@ function App(): JSX.Element {
5959
attachmentOptions: {
6060
uploadOptions: uploadOptions,
6161
downloadOptions: downloadOptions,
62-
}
62+
},
6363
}}
6464
/>
6565
</div>

0 commit comments

Comments
 (0)