@@ -25,6 +25,20 @@ export async function startPowerShellSession(this: NovaWindowsDriver): Promise<v
2525
2626 this . powerShell = powerShell ;
2727
28+ if ( this . caps . appWorkingDir ) {
29+ const envVarsSet : Set < string > = new Set ( ) ;
30+ const matches = this . caps . appWorkingDir . matchAll ( / % ( [ ^ % ] + ) % / g) ;
31+
32+ for ( const match of matches ) {
33+ envVarsSet . add ( match [ 1 ] ) ;
34+ }
35+ const envVars = Array . from ( envVarsSet ) ;
36+ for ( const envVar of envVars ) {
37+ this . caps . appWorkingDir = this . caps . appWorkingDir . replaceAll ( `%${ envVar } %` , process . env [ envVar . toUpperCase ( ) ] ?? '' ) ;
38+ }
39+ this . sendPowerShellCommand ( `Set-Location -Path '${ this . caps . appWorkingDir } '` ) ;
40+ }
41+
2842 await this . sendPowerShellCommand ( SET_UTF8_ENCODING ) ;
2943 await this . sendPowerShellCommand ( ADD_NECESSARY_ASSEMBLIES ) ;
3044 await this . sendPowerShellCommand ( USE_UI_AUTOMATION_CLIENT ) ;
@@ -68,6 +82,75 @@ export async function startPowerShellSession(this: NovaWindowsDriver): Promise<v
6882 }
6983}
7084
85+ export async function sendIsolatedPowerShellCommand ( this : NovaWindowsDriver , command : string ) : Promise < string > {
86+ const magicNumber = 0xF2EE ;
87+
88+ const powerShell = spawn ( 'powershell.exe' , [ '-NoExit' , '-Command' , '-' ] ) ;
89+ try {
90+ powerShell . stdout . setEncoding ( 'utf8' ) ;
91+ powerShell . stdout . setEncoding ( 'utf8' ) ;
92+
93+ powerShell . stdout . on ( 'data' , ( chunk : any ) => {
94+ this . powerShellStdOut += chunk . toString ( ) ;
95+ } ) ;
96+
97+ powerShell . stderr . on ( 'data' , ( chunk : any ) => {
98+ this . powerShellStdErr += chunk . toString ( ) ;
99+ } ) ;
100+
101+ const result = await new Promise < string > ( ( resolve , reject ) => {
102+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
103+ const powerShell = this . powerShell ! ;
104+
105+ this . powerShellStdOut = '' ;
106+ this . powerShellStdErr = '' ;
107+
108+ powerShell . stdin . write ( `${ SET_UTF8_ENCODING } \n` ) ;
109+ if ( this . caps . appWorkingDir ) {
110+ const envVarsSet : Set < string > = new Set ( ) ;
111+ const matches = this . caps . appWorkingDir . matchAll ( / % ( [ ^ % ] + ) % / g) ;
112+
113+ for ( const match of matches ) {
114+ envVarsSet . add ( match [ 1 ] ) ;
115+ }
116+ const envVars = Array . from ( envVarsSet ) ;
117+ for ( const envVar of envVars ) {
118+ this . caps . appWorkingDir = this . caps . appWorkingDir . replaceAll ( `%${ envVar } %` , process . env [ envVar . toUpperCase ( ) ] ?? '' ) ;
119+ }
120+ powerShell . stdin . write ( `Set-Location -Path '${ this . caps . appWorkingDir } '\n` ) ;
121+ }
122+ powerShell . stdin . write ( `${ command } \n` ) ;
123+ powerShell . stdin . write ( /* ps1 */ `Write-Output $([char]0x${ magicNumber . toString ( 16 ) } )\n` ) ;
124+
125+ const onData : Parameters < typeof powerShell . stdout . on > [ 1 ] = ( ( chunk : any ) => {
126+ const magicChar = String . fromCharCode ( magicNumber ) ;
127+ if ( chunk . toString ( ) . includes ( magicChar ) ) {
128+ powerShell . stdout . off ( 'data' , onData ) ;
129+ if ( this . powerShellStdErr ) {
130+ reject ( new errors . UnknownError ( this . powerShellStdErr ) ) ;
131+ } else {
132+ resolve ( this . powerShellStdOut . replace ( `${ magicChar } ` , '' ) . trim ( ) ) ;
133+ }
134+ }
135+ } ) . bind ( this ) ;
136+
137+ powerShell . stdout . on ( 'data' , onData ) ;
138+ } ) ;
139+
140+ // commented out for now to avoid cluttering the logs with long command outputs
141+ // this.log.debug(`PowerShell command executed:\n${command}\n\nCommand output below:\n${result}\n --------`);
142+
143+ return result ;
144+ } finally {
145+ // Ensure the isolated PowerShell process is terminated
146+ try {
147+ powerShell . kill ( ) ;
148+ } catch ( e ) {
149+ this . log . warn ( `Failed to terminate isolated PowerShell process: ${ e } ` ) ;
150+ }
151+ }
152+ }
153+
71154export async function sendPowerShellCommand ( this : NovaWindowsDriver , command : string ) : Promise < string > {
72155 const magicNumber = 0xF2EE ;
73156
0 commit comments