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 */
@@ -17,7 +18,6 @@ namespace ts.server {
1718 homedir ( ) : string
1819 } = require ( "os" ) ;
1920
20-
2121 function getGlobalTypingsCacheLocation ( ) {
2222 let basePath : string ;
2323 switch ( process . platform ) {
@@ -184,8 +184,10 @@ namespace ts.server {
184184 private socket : NodeSocket ;
185185 private projectService : ProjectService ;
186186 private throttledOperations : ThrottledOperations ;
187+ private telemetrySender : EventSender ;
187188
188189 constructor (
190+ private readonly telemetryEnabled : boolean ,
189191 private readonly logger : server . Logger ,
190192 host : ServerHost ,
191193 eventPort : number ,
@@ -214,15 +216,22 @@ namespace ts.server {
214216 this . socket . write ( formatMessage ( { seq, type : "event" , event, body } , this . logger , Buffer . byteLength , this . newLine ) , "utf8" ) ;
215217 }
216218
219+ setTelemetrySender ( telemetrySender : EventSender ) {
220+ this . telemetrySender = telemetrySender ;
221+ }
222+
217223 attach ( projectService : ProjectService ) {
218224 this . projectService = projectService ;
219225 if ( this . logger . hasLevel ( LogLevel . requestTime ) ) {
220226 this . logger . info ( "Binding..." ) ;
221227 }
222228
223- const args : string [ ] = [ "--globalTypingsCacheLocation" , this . globalTypingsCacheLocation ] ;
229+ const args : string [ ] = [ Arguments . GlobalCacheLocation , this . globalTypingsCacheLocation ] ;
230+ if ( this . telemetryEnabled ) {
231+ args . push ( Arguments . EnableTelemetry ) ;
232+ }
224233 if ( this . logger . loggingEnabled ( ) && this . logger . getLogFileName ( ) ) {
225- args . push ( "--logFile" , combinePaths ( getDirectoryPath ( normalizeSlashes ( this . logger . getLogFileName ( ) ) ) , `ti-${ process . pid } .log` ) ) ;
234+ args . push ( Arguments . LogFile , combinePaths ( getDirectoryPath ( normalizeSlashes ( this . logger . getLogFileName ( ) ) ) , `ti-${ process . pid } .log` ) ) ;
226235 }
227236 const execArgv : string [ ] = [ ] ;
228237 {
@@ -268,12 +277,25 @@ namespace ts.server {
268277 } ) ;
269278 }
270279
271- private handleMessage ( response : SetTypings | InvalidateCachedTypings ) {
280+ private handleMessage ( response : SetTypings | InvalidateCachedTypings | TypingsInstallEvent ) {
272281 if ( this . logger . hasLevel ( LogLevel . verbose ) ) {
273282 this . logger . info ( `Received response: ${ JSON . stringify ( response ) } ` ) ;
274283 }
284+ if ( response . kind === EventInstall ) {
285+ if ( this . telemetrySender ) {
286+ const body : protocol . TypingsInstalledTelemetryEventBody = {
287+ telemetryEventName : "typingsInstalled" ,
288+ payload : {
289+ installedPackages : response . packagesToInstall . join ( "," )
290+ }
291+ } ;
292+ const eventName : protocol . TelemetryEventName = "telemetry" ;
293+ this . telemetrySender . event ( body , eventName ) ;
294+ }
295+ return ;
296+ }
275297 this . projectService . updateTypingsForProject ( response ) ;
276- if ( response . kind == "set" && this . socket ) {
298+ if ( response . kind == ActionSet && this . socket ) {
277299 this . sendEvent ( 0 , "setTypings" , response ) ;
278300 }
279301 }
@@ -288,18 +310,25 @@ namespace ts.server {
288310 useSingleInferredProject : boolean ,
289311 disableAutomaticTypingAcquisition : boolean ,
290312 globalTypingsCacheLocation : string ,
313+ telemetryEnabled : boolean ,
291314 logger : server . Logger ) {
292- super (
293- host ,
294- cancellationToken ,
295- useSingleInferredProject ,
296- disableAutomaticTypingAcquisition
297- ? nullTypingsInstaller
298- : new NodeTypingsInstaller ( logger , host , installerEventPort , globalTypingsCacheLocation , host . newLine ) ,
299- Buffer . byteLength ,
300- process . hrtime ,
301- logger ,
302- canUseEvents ) ;
315+ const typingsInstaller = disableAutomaticTypingAcquisition
316+ ? undefined
317+ : new NodeTypingsInstaller ( telemetryEnabled , logger , host , installerEventPort , globalTypingsCacheLocation , host . newLine ) ;
318+
319+ super (
320+ host ,
321+ cancellationToken ,
322+ useSingleInferredProject ,
323+ typingsInstaller || nullTypingsInstaller ,
324+ Buffer . byteLength ,
325+ process . hrtime ,
326+ logger ,
327+ canUseEvents ) ;
328+
329+ if ( telemetryEnabled && typingsInstaller ) {
330+ typingsInstaller . setTelemetrySender ( this ) ;
331+ }
303332 }
304333
305334 exit ( ) {
@@ -526,17 +555,17 @@ namespace ts.server {
526555
527556 let eventPort : number ;
528557 {
529- const index = sys . args . indexOf ( "--eventPort" ) ;
530- if ( index >= 0 && index < sys . args . length - 1 ) {
531- const v = parseInt ( sys . args [ index + 1 ] ) ;
532- if ( ! isNaN ( v ) ) {
533- eventPort = v ;
534- }
558+ const str = findArgument ( "--eventPort" ) ;
559+ const v = str && parseInt ( str ) ;
560+ if ( ! isNaN ( v ) ) {
561+ eventPort = v ;
535562 }
536563 }
537564
538- const useSingleInferredProject = sys . args . indexOf ( "--useSingleInferredProject" ) >= 0 ;
539- const disableAutomaticTypingAcquisition = sys . args . indexOf ( "--disableAutomaticTypingAcquisition" ) >= 0 ;
565+ const useSingleInferredProject = hasArgument ( "--useSingleInferredProject" ) ;
566+ const disableAutomaticTypingAcquisition = hasArgument ( "--disableAutomaticTypingAcquisition" ) ;
567+ const telemetryEnabled = hasArgument ( Arguments . EnableTelemetry ) ;
568+
540569 const ioSession = new IOSession (
541570 sys ,
542571 cancellationToken ,
@@ -545,6 +574,7 @@ namespace ts.server {
545574 useSingleInferredProject ,
546575 disableAutomaticTypingAcquisition ,
547576 getGlobalTypingsCacheLocation ( ) ,
577+ telemetryEnabled ,
548578 logger ) ;
549579 process . on ( "uncaughtException" , function ( err : Error ) {
550580 ioSession . logError ( err , "unknown" ) ;
0 commit comments