11/// <reference types="node" />
2+ /// <reference path="shared.ts" />
23/// <reference path="session.ts" />
34// used in fs.writeSync
45/* tslint:disable:no-null-keyword */
@@ -196,8 +197,10 @@ namespace ts.server {
196197 private socket : NodeSocket ;
197198 private projectService : ProjectService ;
198199 private throttledOperations : ThrottledOperations ;
200+ private telemetrySender : EventSender ;
199201
200202 constructor (
203+ private readonly telemetryEnabled : boolean ,
201204 private readonly logger : server . Logger ,
202205 host : ServerHost ,
203206 eventPort : number ,
@@ -226,15 +229,22 @@ namespace ts.server {
226229 this . socket . write ( formatMessage ( { seq, type : "event" , event, body } , this . logger , Buffer . byteLength , this . newLine ) , "utf8" ) ;
227230 }
228231
232+ setTelemetrySender ( telemetrySender : EventSender ) {
233+ this . telemetrySender = telemetrySender ;
234+ }
235+
229236 attach ( projectService : ProjectService ) {
230237 this . projectService = projectService ;
231238 if ( this . logger . hasLevel ( LogLevel . requestTime ) ) {
232239 this . logger . info ( "Binding..." ) ;
233240 }
234241
235- const args : string [ ] = [ "--globalTypingsCacheLocation" , this . globalTypingsCacheLocation ] ;
242+ const args : string [ ] = [ Arguments . GlobalCacheLocation , this . globalTypingsCacheLocation ] ;
243+ if ( this . telemetryEnabled ) {
244+ args . push ( Arguments . EnableTelemetry ) ;
245+ }
236246 if ( this . logger . loggingEnabled ( ) && this . logger . getLogFileName ( ) ) {
237- args . push ( "--logFile" , combinePaths ( getDirectoryPath ( normalizeSlashes ( this . logger . getLogFileName ( ) ) ) , `ti-${ process . pid } .log` ) ) ;
247+ args . push ( Arguments . LogFile , combinePaths ( getDirectoryPath ( normalizeSlashes ( this . logger . getLogFileName ( ) ) ) , `ti-${ process . pid } .log` ) ) ;
238248 }
239249 const execArgv : string [ ] = [ ] ;
240250 {
@@ -280,12 +290,25 @@ namespace ts.server {
280290 } ) ;
281291 }
282292
283- private handleMessage ( response : SetTypings | InvalidateCachedTypings ) {
293+ private handleMessage ( response : SetTypings | InvalidateCachedTypings | TypingsInstallEvent ) {
284294 if ( this . logger . hasLevel ( LogLevel . verbose ) ) {
285295 this . logger . info ( `Received response: ${ JSON . stringify ( response ) } ` ) ;
286296 }
297+ if ( response . kind === EventInstall ) {
298+ if ( this . telemetrySender ) {
299+ const body : protocol . TypingsInstalledTelemetryEventBody = {
300+ telemetryEventName : "typingsInstalled" ,
301+ payload : {
302+ installedPackages : response . packagesToInstall . join ( "," )
303+ }
304+ } ;
305+ const eventName : protocol . TelemetryEventName = "telemetry" ;
306+ this . telemetrySender . event ( body , eventName ) ;
307+ }
308+ return ;
309+ }
287310 this . projectService . updateTypingsForProject ( response ) ;
288- if ( response . kind == "set" && this . socket ) {
311+ if ( response . kind == ActionSet && this . socket ) {
289312 this . sendEvent ( 0 , "setTypings" , response ) ;
290313 }
291314 }
@@ -300,18 +323,25 @@ namespace ts.server {
300323 useSingleInferredProject : boolean ,
301324 disableAutomaticTypingAcquisition : boolean ,
302325 globalTypingsCacheLocation : string ,
326+ telemetryEnabled : boolean ,
303327 logger : server . Logger ) {
304- super (
305- host ,
306- cancellationToken ,
307- useSingleInferredProject ,
308- disableAutomaticTypingAcquisition
309- ? nullTypingsInstaller
310- : new NodeTypingsInstaller ( logger , host , installerEventPort , globalTypingsCacheLocation , host . newLine ) ,
311- Buffer . byteLength ,
312- process . hrtime ,
313- logger ,
314- canUseEvents ) ;
328+ const typingsInstaller = disableAutomaticTypingAcquisition
329+ ? undefined
330+ : new NodeTypingsInstaller ( telemetryEnabled , logger , host , installerEventPort , globalTypingsCacheLocation , host . newLine ) ;
331+
332+ super (
333+ host ,
334+ cancellationToken ,
335+ useSingleInferredProject ,
336+ typingsInstaller || nullTypingsInstaller ,
337+ Buffer . byteLength ,
338+ process . hrtime ,
339+ logger ,
340+ canUseEvents ) ;
341+
342+ if ( telemetryEnabled && typingsInstaller ) {
343+ typingsInstaller . setTelemetrySender ( this ) ;
344+ }
315345 }
316346
317347 exit ( ) {
@@ -538,17 +568,17 @@ namespace ts.server {
538568
539569 let eventPort : number ;
540570 {
541- const index = sys . args . indexOf ( "--eventPort" ) ;
542- if ( index >= 0 && index < sys . args . length - 1 ) {
543- const v = parseInt ( sys . args [ index + 1 ] ) ;
544- if ( ! isNaN ( v ) ) {
545- eventPort = v ;
546- }
571+ const str = findArgument ( "--eventPort" ) ;
572+ const v = str && parseInt ( str ) ;
573+ if ( ! isNaN ( v ) ) {
574+ eventPort = v ;
547575 }
548576 }
549577
550- const useSingleInferredProject = sys . args . indexOf ( "--useSingleInferredProject" ) >= 0 ;
551- const disableAutomaticTypingAcquisition = sys . args . indexOf ( "--disableAutomaticTypingAcquisition" ) >= 0 ;
578+ const useSingleInferredProject = hasArgument ( "--useSingleInferredProject" ) ;
579+ const disableAutomaticTypingAcquisition = hasArgument ( "--disableAutomaticTypingAcquisition" ) ;
580+ const telemetryEnabled = hasArgument ( Arguments . EnableTelemetry ) ;
581+
552582 const ioSession = new IOSession (
553583 sys ,
554584 cancellationToken ,
@@ -557,6 +587,7 @@ namespace ts.server {
557587 useSingleInferredProject ,
558588 disableAutomaticTypingAcquisition ,
559589 getGlobalTypingsCacheLocation ( ) ,
590+ telemetryEnabled ,
560591 logger ) ;
561592 process . on ( "uncaughtException" , function ( err : Error ) {
562593 ioSession . logError ( err , "unknown" ) ;
0 commit comments