@@ -9,15 +9,23 @@ namespace ts.server {
99 gzipSync ( buf : Buffer ) : Buffer
1010 } = require ( "zlib" ) ;
1111
12+ const net : {
13+ connect ( options : { port : number } , onConnect ?: ( ) => void ) : NodeSocket
14+ } = require ( "net" ) ;
15+
16+ const childProcess : {
17+ fork ( modulePath : string , args : string [ ] , options ?: { execArgv : string [ ] , env ?: MapLike < string > } ) : NodeChildProcess ;
18+ } = require ( "child_process" ) ;
19+
1220 interface NodeChildProcess {
1321 send ( message : any , sendHandle ?: any ) : void ;
1422 on ( message : "message" , f : ( m : any ) => void ) : void ;
1523 kill ( ) : void ;
1624 }
1725
18- const childProcess : {
19- fork ( modulePath : string ) : NodeChildProcess ;
20- } = require ( "child_process" ) ;
26+ interface NodeSocket {
27+ write ( data : string , encoding : string ) : boolean ;
28+ }
2129
2230 interface ReadLineOptions {
2331 input : NodeJS . ReadableStream ;
@@ -106,6 +114,10 @@ namespace ts.server {
106114 }
107115 }
108116
117+ getLogFileName ( ) {
118+ return this . logFilename ;
119+ }
120+
109121 perftrc ( s : string ) {
110122 this . msg ( s , Msg . Perf ) ;
111123 }
@@ -163,9 +175,15 @@ namespace ts.server {
163175
164176 class NodeTypingsInstaller implements ITypingsInstaller {
165177 private installer : NodeChildProcess ;
178+ private socket : NodeSocket ;
166179 private projectService : ProjectService ;
167180
168- constructor ( private readonly logger : server . Logger ) {
181+ constructor ( private readonly logger : server . Logger , private readonly eventPort : number ) {
182+ if ( eventPort ) {
183+ const s = net . connect ( { port : eventPort } , ( ) => {
184+ this . socket = s ;
185+ } ) ;
186+ }
169187 }
170188
171189 attach ( projectService : ProjectService ) {
@@ -174,7 +192,11 @@ namespace ts.server {
174192 this . logger . info ( "Binding..." ) ;
175193 }
176194
177- this . installer = childProcess . fork ( combinePaths ( __dirname , "typingsInstaller.js" ) ) ;
195+ let args : string [ ] = [ ] ;
196+ if ( this . logger . loggingEnabled ( ) && this . logger . getLogFileName ( ) ) {
197+ args = [ "--logFile" , combinePaths ( getDirectoryPath ( normalizeSlashes ( this . logger . getLogFileName ( ) ) ) , `ti-${ process . pid } .log` ) ] ;
198+ }
199+ this . installer = childProcess . fork ( combinePaths ( __dirname , "typingsInstaller.js" ) , args ) ;
178200 this . installer . on ( "message" , m => this . handleMessage ( m ) ) ;
179201 process . on ( "exit" , ( ) => {
180202 this . installer . kill ( ) ;
@@ -198,12 +220,15 @@ namespace ts.server {
198220 this . logger . info ( `Received response: ${ JSON . stringify ( response ) } ` ) ;
199221 }
200222 this . projectService . updateTypingsForProject ( response ) ;
223+ if ( response . kind == "set" && this . socket ) {
224+ this . socket . write ( JSON . stringify ( { kind : "updateTypings" , message : response } ) + "\r\n" , "utf8" ) ;
225+ }
201226 }
202227 }
203228
204229 class IOSession extends Session {
205- constructor ( host : ServerHost , cancellationToken : HostCancellationToken , useSingleInferredProject : boolean , logger : server . Logger ) {
206- super ( host , cancellationToken , useSingleInferredProject , new NodeTypingsInstaller ( logger ) , Buffer . byteLength , maxUncompressedMessageSize , compress , process . hrtime , logger ) ;
230+ constructor ( host : ServerHost , cancellationToken : HostCancellationToken , eventPort : number , useSingleInferredProject : boolean , logger : server . Logger ) {
231+ super ( host , cancellationToken , useSingleInferredProject , new NodeTypingsInstaller ( logger , eventPort ) , Buffer . byteLength , maxUncompressedMessageSize , compress , process . hrtime , logger ) ;
207232 }
208233
209234 exit ( ) {
@@ -435,8 +460,19 @@ namespace ts.server {
435460 } ;
436461 } ;
437462
438- const useSingleInferredProject = sys . args . some ( arg => arg === "--useSingleInferredProject" ) ;
439- const ioSession = new IOSession ( sys , cancellationToken , useSingleInferredProject , logger ) ;
463+ let eventPort : number ;
464+ {
465+ const index = sys . args . indexOf ( "--eventPort" ) ;
466+ if ( index >= 0 && index < sys . args . length - 1 ) {
467+ const v = parseInt ( sys . args [ index + 1 ] ) ;
468+ if ( ! isNaN ( v ) ) {
469+ eventPort = v ;
470+ }
471+ }
472+ }
473+
474+ const useSingleInferredProject = sys . args . indexOf ( "--useSingleInferredProject" ) >= 0 ;
475+ const ioSession = new IOSession ( sys , cancellationToken , eventPort , useSingleInferredProject , logger ) ;
440476 process . on ( "uncaughtException" , function ( err : Error ) {
441477 ioSession . logError ( err , "unknown" ) ;
442478 } ) ;
0 commit comments