@@ -26,49 +26,107 @@ var debugAdapterLogWriter =
26
26
// debug server
27
27
process . stdin . pause ( ) ;
28
28
29
- // Read the details of the current session to learn
30
- // the connection details for the debug service
31
- let sessionDetails = utils . readSessionFile ( ) ;
32
-
33
- // Establish connection before setting up the session
34
- debugAdapterLogWriter . write ( "Connecting to port: " + sessionDetails . debugServicePort + "\r\n" ) ;
35
- let debugServiceSocket = net . connect ( sessionDetails . debugServicePort , '127.0.0.1' ) ;
36
-
37
- // Write any errors to the log file
38
- debugServiceSocket . on (
39
- 'error' ,
40
- ( e ) => debugAdapterLogWriter . write ( "Socket connect ERROR: " + e + "\r\n" ) ) ;
41
-
42
- // Route any output from the socket through stdout
43
- debugServiceSocket . on (
44
- 'data' ,
45
- ( data : Buffer ) => process . stdout . write ( data ) ) ;
46
-
47
- // Wait for the connection to complete
48
- debugServiceSocket . on (
49
- 'connect' ,
50
- ( ) => {
51
- debugAdapterLogWriter . write ( "Connected to socket!\r\n\r\n" ) ;
52
-
53
- // When data comes on stdin, route it through the socket
54
- process . stdin . on (
55
- 'data' ,
56
- ( data : Buffer ) => debugServiceSocket . write ( data ) ) ;
57
-
58
- // Resume the stdin stream
59
- process . stdin . resume ( ) ;
60
- } ) ;
61
-
62
- // When the socket closes, end the session
63
- debugServiceSocket . on (
64
- 'close' ,
65
- ( ) => {
66
- debugAdapterLogWriter . write ( "Socket closed, shutting down." ) ;
67
-
68
- // Close after a short delay to give the client time
69
- // to finish up
70
- setTimeout ( ( ) => {
29
+ function startDebugging ( ) {
30
+ // Read the details of the current session to learn
31
+ // the connection details for the debug service
32
+ let sessionDetails = utils . readSessionFile ( ) ;
33
+
34
+ // Establish connection before setting up the session
35
+ debugAdapterLogWriter . write ( "Connecting to port: " + sessionDetails . debugServicePort + "\r\n" ) ;
36
+
37
+ let isConnected = false ;
38
+ let debugServiceSocket = net . connect ( sessionDetails . debugServicePort , '127.0.0.1' ) ;
39
+
40
+ // Write any errors to the log file
41
+ debugServiceSocket . on (
42
+ 'error' ,
43
+ ( e ) => {
44
+ debugAdapterLogWriter . write ( "Socket ERROR: " + e + "\r\n" )
45
+ debugAdapterLogWriter . close ( ) ;
46
+ debugServiceSocket . destroy ( ) ;
71
47
process . exit ( 0 ) ;
72
- } , 1000 ) ;
48
+ } ) ;
49
+
50
+ // Route any output from the socket through stdout
51
+ debugServiceSocket . on (
52
+ 'data' ,
53
+ ( data : Buffer ) => process . stdout . write ( data ) ) ;
54
+
55
+ // Wait for the connection to complete
56
+ debugServiceSocket . on (
57
+ 'connect' ,
58
+ ( ) => {
59
+ isConnected = true ;
60
+ debugAdapterLogWriter . write ( "Connected to socket!\r\n\r\n" ) ;
61
+
62
+ // When data comes on stdin, route it through the socket
63
+ process . stdin . on (
64
+ 'data' ,
65
+ ( data : Buffer ) => debugServiceSocket . write ( data ) ) ;
66
+
67
+ // Resume the stdin stream
68
+ process . stdin . resume ( ) ;
69
+ } ) ;
70
+
71
+ // When the socket closes, end the session
72
+ debugServiceSocket . on (
73
+ 'close' ,
74
+ ( ) => {
75
+ debugAdapterLogWriter . write ( "Socket closed, shutting down." ) ;
76
+ debugAdapterLogWriter . close ( ) ;
77
+ isConnected = false ;
78
+
79
+ // Close after a short delay to give the client time
80
+ // to finish up
81
+ setTimeout ( ( ) => {
82
+ process . exit ( 0 ) ;
83
+ } , 2000 ) ;
84
+ }
85
+ )
86
+
87
+ process . on (
88
+ 'exit' ,
89
+ ( e ) => {
90
+ if ( debugAdapterLogWriter ) {
91
+ debugAdapterLogWriter . write ( "Debug adapter process is exiting..." ) ;
92
+ }
93
+ }
94
+ )
95
+ }
96
+
97
+ var sessionFilePath = utils . getSessionFilePath ( ) ;
98
+ function waitForSessionFile ( triesRemaining : number ) {
99
+
100
+ debugAdapterLogWriter . write ( `Waiting for session file, tries remaining: ${ triesRemaining } ...\r\n` ) ;
101
+
102
+ if ( triesRemaining > 0 ) {
103
+ if ( utils . checkIfFileExists ( sessionFilePath ) ) {
104
+ debugAdapterLogWriter . write ( `Session file present, connecting to debug adapter...\r\n\r\n` ) ;
105
+ startDebugging ( ) ;
106
+ }
107
+ else {
108
+ // Wait for a second and try again
109
+ setTimeout (
110
+ ( ) => waitForSessionFile ( triesRemaining - 1 ) ,
111
+ 1000 ) ;
112
+ }
113
+ }
114
+ else {
115
+ debugAdapterLogWriter . write ( `Timed out waiting for session file!\r\n` ) ;
116
+ var errorJson =
117
+ JSON . stringify ( {
118
+ type : "response" ,
119
+ request_seq : 1 ,
120
+ command : "initialize" ,
121
+ success : false ,
122
+ message : "Timed out waiting for the PowerShell extension to start."
123
+ } ) ;
124
+
125
+ process . stdout . write (
126
+ `Content-Length: ${ Buffer . byteLength ( errorJson , 'utf8' ) } \r\n\r\n${ errorJson } ` ,
127
+ 'utf8' ) ;
73
128
}
74
- )
129
+ }
130
+
131
+ // Wait for the session file to appear
132
+ waitForSessionFile ( 30 ) ;
0 commit comments