@@ -7,6 +7,7 @@ import compression from '@polka/compression';
7
7
import polka from 'polka' ;
8
8
import invariant from 'tiny-invariant' ;
9
9
10
+ import { telemetry } from '../telemetry' ;
10
11
import type { StoryIndexGenerator } from './utils/StoryIndexGenerator' ;
11
12
import { doTelemetry } from './utils/doTelemetry' ;
12
13
import { getManagerBuilder , getPreviewBuilder } from './utils/get-builders' ;
@@ -19,6 +20,7 @@ import { openInBrowser } from './utils/open-in-browser';
19
20
import { getServerAddresses } from './utils/server-address' ;
20
21
import { getServer } from './utils/server-init' ;
21
22
import { useStatics } from './utils/server-statics' ;
23
+ import { summarizeIndex } from './utils/summarizeIndex' ;
22
24
23
25
export async function storybookDevServer ( options : Options ) {
24
26
const [ server , core ] = await Promise . all ( [ getServer ( options ) , options . presets . apply ( 'core' ) ] ) ;
@@ -130,5 +132,26 @@ export async function storybookDevServer(options: Options) {
130
132
// Now the preview has successfully started, we can count this as a 'dev' event.
131
133
doTelemetry ( app , core , initializedStoryIndexGenerator , options ) ;
132
134
135
+ async function cancelTelemetry ( ) {
136
+ const payload = { eventType : 'dev' } ;
137
+ try {
138
+ const generator = await initializedStoryIndexGenerator ;
139
+ const indexAndStats = await generator ?. getIndexAndStats ( ) ;
140
+ // compute stats so we can get more accurate story counts
141
+ if ( indexAndStats ) {
142
+ Object . assign ( payload , {
143
+ storyIndex : summarizeIndex ( indexAndStats . storyIndex ) ,
144
+ storyStats : indexAndStats . stats ,
145
+ } ) ;
146
+ }
147
+ } catch ( err ) { }
148
+ await telemetry ( 'canceled' , payload , { immediate : true } ) ;
149
+ process . exit ( 0 ) ;
150
+ }
151
+
152
+ if ( ! core ?. disableTelemetry ) {
153
+ process . on ( 'SIGINT' , cancelTelemetry ) ;
154
+ }
155
+
133
156
return { previewResult, managerResult, address, networkAddress } ;
134
157
}
0 commit comments