11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
4+ import fs = require( "fs" ) ;
45import cp = require( "child_process" ) ;
56import * as semver from "semver" ;
67import path = require( "path" ) ;
78import vscode = require( "vscode" ) ;
89import { Logger } from "./logging" ;
910import Settings = require( "./settings" ) ;
1011import utils = require( "./utils" ) ;
12+ import { IEditorServicesSessionDetails , SessionManager } from "./session" ;
1113
1214export class PowerShellProcess {
13- public static escapeSingleQuotes ( pspath : string ) : string {
14- return pspath . replace ( new RegExp ( "'" , "g" ) , "''" ) ;
15+ public static escapeSingleQuotes ( psPath : string ) : string {
16+ return psPath . replace ( new RegExp ( "'" , "g" ) , "''" ) ;
1517 }
1618
1719 // This is used to warn the user that the extension is taking longer than expected to startup.
@@ -30,13 +32,13 @@ export class PowerShellProcess {
3032 private title : string ,
3133 private log : Logger ,
3234 private startPsesArgs : string ,
33- private sessionFilePath : string ,
35+ private sessionFilePath : vscode . Uri ,
3436 private sessionSettings : Settings . ISettings ) {
3537
3638 this . onExited = this . onExitedEmitter . event ;
3739 }
3840
39- public async start ( logFileName : string ) : Promise < utils . IEditorServicesSessionDetails > {
41+ public async start ( logFileName : string ) : Promise < IEditorServicesSessionDetails > {
4042 const editorServicesLogPath = this . log . getLogFilePath ( logFileName ) ;
4143
4244 const psesModulePath =
@@ -52,7 +54,7 @@ export class PowerShellProcess {
5254
5355 this . startPsesArgs +=
5456 `-LogPath '${ PowerShellProcess . escapeSingleQuotes ( editorServicesLogPath . fsPath ) } ' ` +
55- `-SessionDetailsPath '${ PowerShellProcess . escapeSingleQuotes ( this . sessionFilePath ) } ' ` +
57+ `-SessionDetailsPath '${ PowerShellProcess . escapeSingleQuotes ( this . sessionFilePath . fsPath ) } ' ` +
5658 `-FeatureFlags @(${ featureFlags } ) ` ;
5759
5860 if ( this . sessionSettings . integratedConsole . useLegacyReadLine ) {
@@ -99,7 +101,7 @@ export class PowerShellProcess {
99101 " PowerShell Editor Services args: " + startEditorServices ) ;
100102
101103 // Make sure no old session file exists
102- utils . deleteSessionFile ( this . sessionFilePath ) ;
104+ await PowerShellProcess . deleteSessionFile ( this . sessionFilePath ) ;
103105
104106 // Launch PowerShell in the integrated terminal
105107 const terminalOptions : vscode . TerminalOptions = {
@@ -149,7 +151,7 @@ export class PowerShellProcess {
149151
150152 public dispose ( ) {
151153 // Clean up the session file
152- utils . deleteSessionFile ( this . sessionFilePath ) ;
154+ PowerShellProcess . deleteSessionFile ( this . sessionFilePath ) ;
153155
154156 if ( this . consoleCloseSubscription ) {
155157 this . consoleCloseSubscription . dispose ( ) ;
@@ -189,17 +191,31 @@ export class PowerShellProcess {
189191 return true ;
190192 }
191193
192- private async waitForSessionFile ( ) : Promise < utils . IEditorServicesSessionDetails > {
194+ private static readSessionFile ( sessionFilePath : vscode . Uri ) : IEditorServicesSessionDetails {
195+ // TODO: Use vscode.workspace.fs.readFile instead of fs.readFileSync.
196+ const fileContents = fs . readFileSync ( sessionFilePath . fsPath , "utf-8" ) ;
197+ return JSON . parse ( fileContents ) ;
198+ }
199+
200+ private static async deleteSessionFile ( sessionFilePath : vscode . Uri ) {
201+ try {
202+ await vscode . workspace . fs . delete ( sessionFilePath ) ;
203+ } catch ( e ) {
204+ // TODO: Be more specific about what we're catching
205+ }
206+ }
207+
208+ private async waitForSessionFile ( ) : Promise < IEditorServicesSessionDetails > {
193209 // Determine how many tries by dividing by 2000 thus checking every 2 seconds.
194210 const numOfTries = this . sessionSettings . developer . waitForSessionFileTimeoutSeconds / 2 ;
195211 const warnAt = numOfTries - PowerShellProcess . warnUserThreshold ;
196212
197213 // Check every 2 seconds
198214 for ( let i = numOfTries ; i > 0 ; i -- ) {
199- if ( utils . checkIfFileExists ( this . sessionFilePath ) ) {
215+ if ( utils . checkIfFileExists ( this . sessionFilePath . fsPath ) ) {
200216 this . log . write ( "Session file found" ) ;
201- const sessionDetails = utils . readSessionFile ( this . sessionFilePath ) ;
202- utils . deleteSessionFile ( this . sessionFilePath ) ;
217+ const sessionDetails = PowerShellProcess . readSessionFile ( this . sessionFilePath ) ;
218+ PowerShellProcess . deleteSessionFile ( this . sessionFilePath ) ;
203219 return sessionDetails ;
204220 }
205221
0 commit comments