1+ #!/usr/bin/env node
2+ // Copyright (c) Microsoft Corporation.
3+ // Licensed under the MIT License.
4+
5+ import { REPO_ROOT } from './lib/index.mjs' ;
6+ import { readFileSync , writeFileSync } from 'fs' ;
7+ import path from 'path' ;
8+
9+
10+ const MATRIX_JSON = path . join ( REPO_ROOT , 'common' , 'config' , 'workflows' , 'matrix.json' ) ;
11+
12+ /**
13+ * This script injects sign-out html logic as part of CI pipeline.
14+ *
15+ * In uri query parameter add '&hideSignout=true' to hide the signout bar/button.
16+ *
17+ * Usage: node inject-signout-html.mjs <Calling/CallWithChat/Chat>
18+ */
19+ function main ( args ) {
20+ const target = args [ 2 ]
21+ if ( target !== 'Calling' && target !== 'CallWithChat' && target !== 'Chat' ) {
22+ throw new Error ( `Usage: ${ args [ 1 ] } ['Calling' | 'CallWithChat' | 'Chat']\n` ) ;
23+ }
24+ const indexHtmlPath = path . join ( REPO_ROOT , 'samples' , target , 'public' , 'index.html' ) ;
25+ injectSignoutToIndexHtmlFile ( indexHtmlPath ) ;
26+ }
27+
28+ function injectSignoutToIndexHtmlFile ( filePath ) {
29+ const bodySearchString = '<div id="root" class="Root"></div>' ;
30+ const headSearchString = '</head>' ;
31+ const styles = ` <style>
32+ .signout-bar {
33+ display: flex;
34+ background-color: #0078d4;
35+ color: white;
36+ padding: 10px;
37+ }
38+
39+ .signout-button {
40+ margin-left: auto;
41+ background-color: white;
42+ color: black;
43+ border: 1px solid;
44+ border-radius: 2px;
45+ cursor: pointer;
46+ font-size: 16px;
47+ padding: 8px 16px;
48+ transition: background-color 0.3s, color 0.3s, border-color 0.3s;
49+ }
50+
51+ .signout-button:hover {
52+ background-color: #f3f2f1;
53+ border-color: #005a9e;
54+ }
55+
56+ .signout-button:focus {
57+ outline: none;
58+ box-shadow: 0 0 0 2px rgba(0, 120, 212, 0.5);
59+ }
60+
61+ .signout-button:active {
62+ background-color: #e0e0e0;
63+ }
64+ </style>
65+ </head>`
66+ const content = `<div id="signout-container">
67+ <div id="signout-bar" class="signout-bar" style="display: flex;">
68+ <button id="signoutButton" class="signout-button">Sign Out</button>
69+ </div>
70+ </div>
71+ <div id="root" class="Root"></div>
72+ <script>
73+ // Ignore: for internal testing only
74+ function handleSignOut() {
75+ console.log("You have signed out!");
76+ window.location.href = window.location.origin + '/.auth/logout';
77+ }
78+ document.getElementById('signoutButton').addEventListener('click', handleSignOut);
79+
80+ const urlQueryParameters = new URLSearchParams(window.location.search);
81+ const display = urlQueryParameters.get('hideSignout') === 'true' ? 'none' : 'flex';
82+ document.getElementById('signout-bar').style.display = display;
83+ </script>` ;
84+ try {
85+ const fileContent = readFileSync ( filePath , 'utf-8' ) ;
86+ const updatedContent = fileContent
87+ . replace ( new RegExp ( bodySearchString , 'g' ) , content )
88+ . replace ( new RegExp ( headSearchString , 'g' ) , styles ) ;
89+ writeFileSync ( filePath , updatedContent , 'utf-8' ) ;
90+ console . log ( `Successfully replaced "${ bodySearchString } " and "${ headSearchString } " with signout logic in ${ filePath } ` ) ;
91+ } catch ( error ) {
92+ console . error ( 'Error processing the file:' , error ) ;
93+ }
94+ }
95+
96+ main ( process . argv ) ;
0 commit comments