Skip to content

Commit d878bfd

Browse files
authored
chore(gatsby-telemetry): Generate typings (#26779)
* chore(gatsby-telemetry): Generate typings * Add rimraf * Improve telementry typings * Add typescript devdep * Exclude test files etc from typegen
1 parent 9fbac50 commit d878bfd

File tree

6 files changed

+60
-34
lines changed

6 files changed

+60
-34
lines changed

packages/gatsby-cli/src/create-cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ Creating a plugin:
529529
}),
530530

531531
handler: handlerP(({ enable, disable }: yargs.Arguments) => {
532-
const enabled = enable || !disable
532+
const enabled = Boolean(enable) || !disable
533533
setTelemetryEnabled(enabled)
534534
report.log(`Telemetry collection ${enabled ? `enabled` : `disabled`}`)
535535
}),

packages/gatsby-cli/src/reporter/loggers/ink/cli.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ class CLI extends React.Component<ICLIProps, ICLIState> {
3232
componentDidCatch(error: Error, info: React.ErrorInfo): void {
3333
trackBuildError(`INK`, {
3434
error: {
35-
stack: info.componentStack,
35+
error: {
36+
stack: info.componentStack,
37+
},
3638
text: error.message,
37-
context: {},
3839
},
3940
})
4041
}

packages/gatsby-telemetry/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
"cross-env": "^5.2.1",
3232
"jest": "^24.9.0",
3333
"jest-cli": "^24.9.0",
34-
"jest-junit": "^6.4.0"
34+
"jest-junit": "^6.4.0",
35+
"rimraf": "^3.0.2",
36+
"typescript": "^3.9.5"
3537
},
3638
"files": [
3739
"lib/",
@@ -50,9 +52,10 @@
5052
},
5153
"scripts": {
5254
"build": "babel src --out-dir lib --ignore \"**/__tests__\",\"**/__mocks__\" --extensions \".ts,.js\"",
53-
"prepare": "cross-env NODE_ENV=production npm run build",
55+
"prepare": "cross-env NODE_ENV=production npm run build && npm run typegen",
5456
"jest": "jest",
5557
"postinstall": "node src/postinstall.js || true",
58+
"typegen": "rimraf \"lib/**/*.d.ts\" && tsc --emitDeclarationOnly --declaration --declarationDir lib/",
5659
"watch": "babel -w src --out-dir lib --ignore \"**/__tests__\",\"**/__mocks__\" --extensions \".ts,.js\""
5760
},
5861
"yargs": {

packages/gatsby-telemetry/src/index.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import {
33
IAggregateStats,
44
ITelemetryTagsPayload,
55
ITelemetryOptsPayload,
6+
IDefaultTelemetryTagsPayload,
67
} from "./telemetry"
7-
import * as express from "express"
8+
import { Request, Response } from "express"
89
import { createFlush } from "./create-flush"
910

1011
const instance = new AnalyticsTracker()
@@ -40,23 +41,29 @@ export function trackCli(
4041
instance.captureEvent(input, tags, opts)
4142
}
4243

43-
export function trackError(input, tags): void {
44+
export function trackError(input: string, tags?: ITelemetryTagsPayload): void {
4445
instance.captureError(input, tags)
4546
}
4647

47-
export function trackBuildError(input, tags): void {
48+
export function trackBuildError(
49+
input: string,
50+
tags?: ITelemetryTagsPayload
51+
): void {
4852
instance.captureBuildError(input, tags)
4953
}
5054

51-
export function setDefaultTags(tags): void {
55+
export function setDefaultTags(tags: IDefaultTelemetryTagsPayload): void {
5256
instance.decorateAll(tags)
5357
}
5458

55-
export function decorateEvent(event, tags): void {
59+
export function decorateEvent(
60+
event: string,
61+
tags?: Record<string, unknown>
62+
): void {
5663
instance.decorateNextEvent(event, tags)
5764
}
5865

59-
export function setTelemetryEnabled(enabled): void {
66+
export function setTelemetryEnabled(enabled: boolean): void {
6067
instance.setTelemetryEnabled(enabled)
6168
}
6269

@@ -68,16 +75,16 @@ export function isTrackingEnabled(): boolean {
6875
return instance.isTrackingEnabled()
6976
}
7077

71-
export function aggregateStats(data): IAggregateStats {
78+
export function aggregateStats(data: Array<number>): IAggregateStats {
7279
return instance.aggregateStats(data)
7380
}
7481

75-
export function addSiteMeasurement(event, obj): void {
82+
export function addSiteMeasurement(event: string, obj): void {
7683
instance.addSiteMeasurement(event, obj)
7784
}
7885

7986
export function expressMiddleware(source: string) {
80-
return function (_req: express.Request, _res: express.Response, next): void {
87+
return function (_req: Request, _res: Response, next): void {
8188
try {
8289
instance.trackActivity(`${source}_ACTIVE`)
8390
} catch (e) {
@@ -88,11 +95,11 @@ export function expressMiddleware(source: string) {
8895
}
8996

9097
// Internal
91-
export function setDefaultComponentId(componentId): void {
98+
export function setDefaultComponentId(componentId: string): void {
9299
instance.componentId = componentId
93100
}
94101

95-
export function setGatsbyCliVersion(version): void {
102+
export function setGatsbyCliVersion(version: string): void {
96103
instance.gatsbyCliVersion = version
97104
}
98105

packages/gatsby-telemetry/src/telemetry.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ interface IAnalyticsTrackerConstructorParameters {
4848
gatsbyCliVersion?: SemVer
4949
}
5050

51+
export interface IStructuredError {
52+
id?: string
53+
code?: string
54+
text: string
55+
level?: string
56+
type?: string
57+
context?: unknown
58+
error?: {
59+
stack?: string
60+
}
61+
}
62+
63+
export interface IStructuredErrorV2 {
64+
id?: string
65+
text: string
66+
level?: string
67+
type?: string
68+
context?: string
69+
stack?: string
70+
}
71+
5172
export interface ITelemetryTagsPayload {
5273
name?: string
5374
starterName?: string
@@ -58,29 +79,23 @@ export interface ITelemetryTagsPayload {
5879
valid?: boolean
5980
plugins?: string[]
6081
pathname?: string
61-
error?: {
62-
id?: string
63-
code?: string
64-
text: string
65-
level?: string
66-
type?: string
67-
stack?: string
68-
context?: string
69-
error?: {
70-
stack?: string
71-
}
72-
}
82+
error?: IStructuredError | Array<IStructuredError>
7383
cacheStatus?: string
7484
pluginCachePurged?: string
7585
siteMeasurements?: {
7686
pagesCount?: number
7787
clientsCount?: number
78-
paths?: string[]
88+
paths?: Array<string | undefined>
7989
bundleStats?: unknown
8090
pageDataStats?: unknown
8191
queryStats?: unknown
8292
}
83-
errorV2?: unknown
93+
errorV2?: IStructuredErrorV2
94+
}
95+
96+
export interface IDefaultTelemetryTagsPayload extends ITelemetryTagsPayload {
97+
gatsbyCliVersion?: SemVer
98+
installedGatsbyVersion?: SemVer
8499
}
85100

86101
export interface ITelemetryOptsPayload {
@@ -390,7 +405,7 @@ export class AnalyticsTracker {
390405
this.store.updateConfig(`telemetry.enabled`, enabled)
391406
}
392407

393-
aggregateStats(data): IAggregateStats {
408+
aggregateStats(data: Array<number>): IAggregateStats {
394409
const sum = data.reduce((acc, x) => acc + x, 0)
395410
const mean = sum / data.length || 0
396411
const median = data.sort()[Math.floor((data.length - 1) / 2)] || 0
@@ -419,14 +434,13 @@ export class AnalyticsTracker {
419434

420435
async sendEvents(): Promise<boolean> {
421436
if (!this.isTrackingEnabled()) {
422-
return Promise.resolve(true)
437+
return true
423438
}
424439

425440
return this.store.sendEvents()
426441
}
427442

428443
trackFeatureIsUsed(name: string): void {
429-
if (this.features.has(name)) return
430444
this.features.add(name)
431445
}
432446
}

packages/gatsby-telemetry/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
44
"declaration": true
5-
}
5+
},
6+
"exclude": ["node_modules", "src/__tests__", "src/__mocks__", "lib"]
67
}

0 commit comments

Comments
 (0)