-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathworker-client.ts
More file actions
62 lines (53 loc) · 1.88 KB
/
worker-client.ts
File metadata and controls
62 lines (53 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* eslint-disable n/no-process-exit */
import {CLIError, spawnTelemetryWorker, TelemetryGlobal} from './telemetry-utils.js'
// Extend global with telemetry property
declare global {
// eslint-disable-next-line no-var
var cliTelemetry: TelemetryGlobal['cliTelemetry']
}
interface SetupTelemetryOptions {
cliStartTime: number
computeDuration: (startTime: number) => number
enableTelemetry: boolean
}
/**
* Setup telemetry handlers for beforeExit and signal handlers
* This centralizes all telemetry worker spawning logic
*/
export function setupTelemetryHandlers(options: SetupTelemetryOptions): void {
const {cliStartTime, computeDuration, enableTelemetry} = options
if (!enableTelemetry) return
process.once('beforeExit', code => {
// capture as successful exit
if (global.cliTelemetry) {
if (global.cliTelemetry.isVersionOrHelp) {
const cmdStartTime = global.cliTelemetry.commandRunDuration
global.cliTelemetry.commandRunDuration = computeDuration(cmdStartTime)
}
global.cliTelemetry.exitCode = code
global.cliTelemetry.cliRunDuration = computeDuration(cliStartTime)
const telemetryData = global.cliTelemetry
// Spawn background process to send telemetry without blocking exit
spawnTelemetryWorker(telemetryData)
}
})
process.on('SIGINT', () => {
// Spawn background process to send telemetry
const error: CLIError = Object.assign(new Error('Received SIGINT'), {
cliRunDuration: computeDuration(cliStartTime),
context: {
isTTY: process.stdin.isTTY,
},
})
spawnTelemetryWorker(error)
process.exit(1)
})
process.on('SIGTERM', () => {
// Spawn background process to send telemetry
const error: CLIError = Object.assign(new Error('Received SIGTERM'), {
cliRunDuration: computeDuration(cliStartTime),
})
spawnTelemetryWorker(error)
process.exit(1)
})
}