Skip to content

Commit

Permalink
Start moving gatsby-telemetry to typescript (#25812)
Browse files Browse the repository at this point in the history
* Fix camelCase

* Start moving gatsby-telemetry to ts

* Continue converting base to typescript

* Debug transpilation issues

* Debug transpilation issues

* Fix telemetry tests
  • Loading branch information
jamo committed Jul 20, 2020
1 parent 16ee206 commit 2a72ec1
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 110 deletions.
4 changes: 2 additions & 2 deletions packages/gatsby-core-utils/src/ci.ts
@@ -1,7 +1,7 @@
import ci from "ci-info"

const CI_DEFINITIONS = [
envFromCIandCIName,
envFromCIAndCIName,
getEnvDetect({ key: `NOW_BUILDER_ANNOTATE`, name: `ZEIT Now` }),
getEnvDetect({ key: `NOW_REGION`, name: `ZEIT Now v1` }),
herokuDetect,
Expand Down Expand Up @@ -73,7 +73,7 @@ function herokuDetect(): false | "Heroku" {
)
}

function envFromCIandCIName(): string | null {
function envFromCIAndCIName(): string | null {
if (process.env.CI_NAME && process.env.CI) {
return process.env.CI_NAME
}
Expand Down
@@ -1,11 +1,11 @@
jest.mock(`../event-storage`)
const eventStore = require(`../event-storage`)
const Telemetry = require(`../telemetry`)
import { AnalyticsTracker } from "../telemetry"

let telemetry
beforeEach(() => {
eventStore.mockReset()
telemetry = new Telemetry()
telemetry = new AnalyticsTracker()
})

describe(`Telemetry`, () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/gatsby-telemetry/src/declarations.d.ts
@@ -0,0 +1,8 @@
type UUID = string

declare namespace NodeJS {
interface Process {
gatsbyTelemetrySessionId: UUID;
}
}

42 changes: 0 additions & 42 deletions packages/gatsby-telemetry/src/index.js

This file was deleted.

56 changes: 56 additions & 0 deletions packages/gatsby-telemetry/src/index.ts
@@ -0,0 +1,56 @@
import { AnalyticsTracker, IAggregateStats } from "./telemetry"
import * as express from "express"

const instance = new AnalyticsTracker()

const flush = require(`./flush`)(instance.isTrackingEnabled())

process.on(`exit`, flush)

// For long running commands we want to occasionally flush the data
//
// The data is also sent on exit.

const intervalDuration = process.env.TELEMETRY_BUFFER_INTERVAL
const interval =
intervalDuration && Number.isFinite(+intervalDuration)
? Math.max(Number(intervalDuration), 1000)
: 10 * 60 * 1000 // 10 min

function tick(): void {
flush()
.catch(console.error)
.then(() => setTimeout(tick, interval))
}

module.exports = {
trackCli: (input, tags, opts): void =>
instance.captureEvent(input, tags, opts),
trackError: (input, tags): void => instance.captureError(input, tags),
trackBuildError: (input, tags): void =>
instance.captureBuildError(input, tags),
setDefaultTags: (tags): void => instance.decorateAll(tags),
decorateEvent: (event, tags): void => instance.decorateNextEvent(event, tags),
setTelemetryEnabled: (enabled): void => instance.setTelemetryEnabled(enabled),
startBackgroundUpdate: (): void => {
setTimeout(tick, interval)
},
isTrackingEnabled: (): boolean => instance.isTrackingEnabled(),
aggregateStats: (data): IAggregateStats => instance.aggregateStats(data),
addSiteMeasurement: (event, obj): void =>
instance.addSiteMeasurement(event, obj),
expressMiddleware: function (source: string) {
return function (
_req: express.Request,
_res: express.Response,
next
): void {
try {
instance.trackActivity(`${source}_ACTIVE`)
} catch (e) {
// ignore
}
next()
}
},
}
2 changes: 1 addition & 1 deletion packages/gatsby-telemetry/src/repository-id.ts
Expand Up @@ -15,7 +15,7 @@ interface IRepositoryData {
name?: string
}

interface IRepositoryId {
export interface IRepositoryId {
repositoryId: string
repositoryData?: IRepositoryData | null
}
Expand Down
10 changes: 0 additions & 10 deletions packages/gatsby-telemetry/src/send.js

This file was deleted.

10 changes: 10 additions & 0 deletions packages/gatsby-telemetry/src/send.ts
@@ -0,0 +1,10 @@
import { AnalyticsTracker } from "./telemetry"
const instance = new AnalyticsTracker()

function flush(): void {
instance.sendEvents().catch(_e => {
// ignore
})
}

flush()
@@ -1,10 +1,10 @@
const boxen = require(`boxen`)
import boxen from "boxen"

const defaultConfig = {
padding: 1,
borderColor: `blue`,
borderStyle: `double`,
}
} as boxen.Options

const defaultMessage =
`Gatsby collects anonymous usage analytics\n` +
Expand All @@ -15,14 +15,10 @@ const defaultMessage =

/**
* Analytics notice for the end-user
* @param {Object} config - The configuration that boxen accepts. https://github.com/sindresorhus/boxen#api
* @param {string} message - Message shown to the end-user
*/
const showAnalyticsNotification = (
config = defaultConfig,
message = defaultMessage
) => {
export function showAnalyticsNotification(
config: boxen.Options = defaultConfig,
message: string = defaultMessage
): void {
console.log(boxen(message, config))
}

module.exports = showAnalyticsNotification

0 comments on commit 2a72ec1

Please sign in to comment.