From a75f2782694d749a8da8b5fc0e442faa37f2b661 Mon Sep 17 00:00:00 2001 From: Deep Singhvi Date: Sun, 8 Sep 2024 16:26:33 -0400 Subject: [PATCH] fix(cli): await Posthog API calls (#4581) --- packages/cli/cli/src/cli-context/CliContext.ts | 10 +++------- .../cli/cli/src/cli-context/TaskContextImpl.ts | 10 +++++----- packages/cli/cli/src/cli.ts | 16 ++++++++-------- packages/cli/cli/src/cliV2.ts | 8 ++++---- .../src/commands/docs-dev/devDocsWorkspace.ts | 2 +- .../commands/generate/generateAPIWorkspaces.ts | 2 +- .../commands/generate/generateDocsWorkspace.ts | 2 +- packages/cli/cli/src/commands/mock/mockServer.ts | 2 +- packages/cli/cli/src/commands/test/testOutput.ts | 2 +- packages/cli/cli/versions.yml | 9 +++++++++ .../remote-workspace-runner/src/publishDocs.ts | 8 ++++---- packages/cli/login/src/login.ts | 2 +- packages/cli/task-context/src/TaskContext.ts | 2 +- packages/seed/src/TaskContextImpl.ts | 8 ++++---- .../seed/src/commands/test/TaskContextFactory.ts | 2 +- 15 files changed, 45 insertions(+), 40 deletions(-) diff --git a/packages/cli/cli/src/cli-context/CliContext.ts b/packages/cli/cli/src/cli-context/CliContext.ts index d2eeb56c2bc..ded7f0b7499 100644 --- a/packages/cli/cli/src/cli-context/CliContext.ts +++ b/packages/cli/cli/src/cli-context/CliContext.ts @@ -181,11 +181,7 @@ export class CliContext { return result; } - public instrumentPostHogEvent(event: PosthogEvent): void { - void this.instrumentPostHogEventAsync(event); - } - - private async instrumentPostHogEventAsync(event: PosthogEvent): Promise { + public async instrumentPostHogEvent(event: PosthogEvent): Promise { (await getPosthogManager()).sendEvent(event); } @@ -225,8 +221,8 @@ export class CliContext { this.didSucceed = false; } }, - instrumentPostHogEvent: (event) => { - this.instrumentPostHogEvent(event); + instrumentPostHogEvent: async (event) => { + await this.instrumentPostHogEvent(event); }, shouldBufferLogs: false }; diff --git a/packages/cli/cli/src/cli-context/TaskContextImpl.ts b/packages/cli/cli/src/cli-context/TaskContextImpl.ts index 8fab9d3e2c6..d5fba34575b 100644 --- a/packages/cli/cli/src/cli-context/TaskContextImpl.ts +++ b/packages/cli/cli/src/cli-context/TaskContextImpl.ts @@ -24,7 +24,7 @@ export declare namespace TaskContextImpl { */ onResult?: (result: TaskResult) => void; shouldBufferLogs: boolean; - instrumentPostHogEvent: (event: PosthogEvent) => void; + instrumentPostHogEvent: (event: PosthogEvent) => Promise; } } @@ -37,7 +37,7 @@ export class TaskContextImpl implements Startable, Finishable, Task private bufferedLogs: Log[] = []; protected status: "notStarted" | "running" | "finished" = "notStarted"; private onResult: ((result: TaskResult) => void) | undefined; - private instrumentPostHogEventImpl: (event: PosthogEvent) => void; + private instrumentPostHogEventImpl: (event: PosthogEvent) => Promise; public constructor({ logImmediately, logPrefix, @@ -90,8 +90,8 @@ export class TaskContextImpl implements Startable, Finishable, Task return this.result; } - public instrumentPostHogEvent(event: PosthogEvent): void { - this.instrumentPostHogEventImpl(event); + public async instrumentPostHogEvent(event: PosthogEvent): Promise { + await this.instrumentPostHogEventImpl(event); } protected logAtLevel(level: LogLevel, ...parts: string[]): void { @@ -133,7 +133,7 @@ export class TaskContextImpl implements Startable, Finishable, Task takeOverTerminal: this.takeOverTerminal, onResult: this.onResult, shouldBufferLogs: this.shouldBufferLogs, - instrumentPostHogEvent: (event) => this.instrumentPostHogEventImpl(event) + instrumentPostHogEvent: async (event) => await this.instrumentPostHogEventImpl(event) }); this.subtasks.push(subtask); return subtask; diff --git a/packages/cli/cli/src/cli.ts b/packages/cli/cli/src/cli.ts index 78594baad2e..7c230212c25 100644 --- a/packages/cli/cli/src/cli.ts +++ b/packages/cli/cli/src/cli.ts @@ -71,7 +71,7 @@ async function runCli() { }); } } catch (error) { - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ command: process.argv.join(" "), properties: { failed: true, @@ -676,7 +676,7 @@ function addUpdateApiSpecCommand(cli: Argv, cliContext: CliCon "The API to update the spec for. If not specified, all APIs with a declared origin will be updated." }), async (argv) => { - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ command: "fern api update" }); await updateApiSpec({ @@ -702,7 +702,7 @@ function addLoginCommand(cli: Argv, cliContext: CliContext) { }), async (argv) => { await cliContext.runTask(async (context) => { - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ command: "fern login" }); await login(context, { useDeviceCodeFlow: argv.deviceCode }); @@ -727,7 +727,7 @@ function addFormatCommand(cli: Argv, cliContext: CliContext) { description: "Only run the command on the provided API" }), async (argv) => { - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ command: "fern format" }); await formatWorkspaces({ @@ -761,7 +761,7 @@ function addTestCommand(cli: Argv, cliContext: CliContext) { description: "Run the tests configured to a specific language" }), async (argv) => { - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ command: "fern test" }); await testOutput({ @@ -794,7 +794,7 @@ function addMockCommand(cli: Argv, cliContext: CliContext) { description: "The API to mock." }), async (argv) => { - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ command: "fern mock" }); await mockServer({ @@ -826,7 +826,7 @@ function addWriteOverridesCommand(cli: Argv, cliContext: CliCo }) ], async (argv) => { - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ command: "fern generate-overrides" }); await writeOverridesForWorkspaces({ @@ -856,7 +856,7 @@ function addWriteDefinitionCommand(cli: Argv, cliContext: CliC description: "Write the definition for a particular SDK language" }), async (argv) => { - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ command: "fern write-definition" }); await writeDefinitionForWorkspaces({ diff --git a/packages/cli/cli/src/cliV2.ts b/packages/cli/cli/src/cliV2.ts index 4f28a36a185..99708fcab54 100644 --- a/packages/cli/cli/src/cliV2.ts +++ b/packages/cli/cli/src/cliV2.ts @@ -20,7 +20,7 @@ export function addGetOrganizationCommand(cli: Argv, cliContex description: "The location to output the organization name as a text file, defaults to standard out." }), async (argv) => { - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ command: "fern organization", properties: { outputLocation: argv.output @@ -78,7 +78,7 @@ export function addGeneratorCommands(cli: Argv, cliContext: Cl "The APIs to list the generators for. If not specified, the generator will be upgraded for all APIs." }), async (argv) => { - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ command: "fern generator list", properties: { outputLocation: argv.output @@ -128,7 +128,7 @@ export function addGeneratorCommands(cli: Argv, cliContext: Cl choices: Object.values(FernRegistry.generators.ReleaseType) }), async (argv) => { - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ command: "fern generator upgrade", properties: { generator: argv.generator, @@ -178,7 +178,7 @@ export function addGeneratorCommands(cli: Argv, cliContext: Cl description: "Get the version of the specified generator." }), async (argv) => { - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ command: "fern generator get", properties: { generator: argv.generator, diff --git a/packages/cli/cli/src/commands/docs-dev/devDocsWorkspace.ts b/packages/cli/cli/src/commands/docs-dev/devDocsWorkspace.ts index 26154bee5fa..8566ea730cd 100644 --- a/packages/cli/cli/src/commands/docs-dev/devDocsWorkspace.ts +++ b/packages/cli/cli/src/commands/docs-dev/devDocsWorkspace.ts @@ -19,7 +19,7 @@ export async function previewDocsWorkspace({ return; } - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ orgId: project.config.organization, command: "fern docs dev" }); diff --git a/packages/cli/cli/src/commands/generate/generateAPIWorkspaces.ts b/packages/cli/cli/src/commands/generate/generateAPIWorkspaces.ts index f7894b46e7b..af39b012963 100644 --- a/packages/cli/cli/src/commands/generate/generateAPIWorkspaces.ts +++ b/packages/cli/cli/src/commands/generate/generateAPIWorkspaces.ts @@ -52,7 +52,7 @@ export async function generateAPIWorkspaces({ token = currentToken; } - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ orgId: project.config.organization, command: "fern generate", properties: { diff --git a/packages/cli/cli/src/commands/generate/generateDocsWorkspace.ts b/packages/cli/cli/src/commands/generate/generateDocsWorkspace.ts index b16c7e419e9..e7a66447d1b 100644 --- a/packages/cli/cli/src/commands/generate/generateDocsWorkspace.ts +++ b/packages/cli/cli/src/commands/generate/generateDocsWorkspace.ts @@ -35,7 +35,7 @@ export async function generateDocsWorkspace({ }); } - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ orgId: project.config.organization, command: "fern generate --docs" }); diff --git a/packages/cli/cli/src/commands/mock/mockServer.ts b/packages/cli/cli/src/commands/mock/mockServer.ts index 7dfb88bc3a8..8a367a4f485 100644 --- a/packages/cli/cli/src/commands/mock/mockServer.ts +++ b/packages/cli/cli/src/commands/mock/mockServer.ts @@ -15,7 +15,7 @@ export async function mockServer({ project: Project; port: number | undefined; }): Promise { - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ orgId: project.config.organization, command: "fern mock" }); diff --git a/packages/cli/cli/src/commands/test/testOutput.ts b/packages/cli/cli/src/commands/test/testOutput.ts index 6213e411f25..ffa158de1ba 100644 --- a/packages/cli/cli/src/commands/test/testOutput.ts +++ b/packages/cli/cli/src/commands/test/testOutput.ts @@ -19,7 +19,7 @@ export async function testOutput({ testCommand: string | undefined; generationLanguage: generatorsYml.GenerationLanguage | undefined; }): Promise { - cliContext.instrumentPostHogEvent({ + await cliContext.instrumentPostHogEvent({ orgId: project.config.organization, command: "fern test" }); diff --git a/packages/cli/cli/versions.yml b/packages/cli/cli/versions.yml index 050064b20a2..fce15d9df56 100644 --- a/packages/cli/cli/versions.yml +++ b/packages/cli/cli/versions.yml @@ -1,3 +1,12 @@ +- changelogEntry: + - summary: | + Previously we weren't always awaiting PostHog API calls directly. Now the CLI + awaits these calls so that we can ensure that events are sent. + type: fix + createdAt: '2024-09-08' + irVersion: 53 + version: 0.41.7 + - changelogEntry: - summary: | The Fern Docs CLI now supports OAuth 2.0 Client Credentials injection in API playgrounds. diff --git a/packages/cli/generation/remote-generation/remote-workspace-runner/src/publishDocs.ts b/packages/cli/generation/remote-generation/remote-workspace-runner/src/publishDocs.ts index 20bf964e6bc..9f6afd2b314 100644 --- a/packages/cli/generation/remote-generation/remote-workspace-runner/src/publishDocs.ts +++ b/packages/cli/generation/remote-generation/remote-workspace-runner/src/publishDocs.ts @@ -121,7 +121,7 @@ export async function publishDocs({ docsWorkspace.absoluteFilepath ); } else { - return startDocsRegisterFailed(startDocsRegisterResponse.error, context); + return await startDocsRegisterFailed(startDocsRegisterResponse.error, context); } } else { const startDocsRegisterResponse = await fdr.docs.v2.write.startDocsRegister({ @@ -262,11 +262,11 @@ function convertToFilePathPairs( return toRet; } -function startDocsRegisterFailed( +async function startDocsRegisterFailed( error: CjsFdrSdk.docs.v2.write.startDocsPreviewRegister.Error | CjsFdrSdk.docs.v2.write.startDocsRegister.Error, context: TaskContext -): never { - context.instrumentPostHogEvent({ +): Promise { + await context.instrumentPostHogEvent({ command: "docs-generation", properties: { error: JSON.stringify(error) diff --git a/packages/cli/login/src/login.ts b/packages/cli/login/src/login.ts index 71f2d49d1fc..9216acc4a6c 100644 --- a/packages/cli/login/src/login.ts +++ b/packages/cli/login/src/login.ts @@ -14,7 +14,7 @@ export async function login( context: TaskContext, { useDeviceCodeFlow = false }: { useDeviceCodeFlow?: boolean } = {} ): Promise { - context.instrumentPostHogEvent({ + await context.instrumentPostHogEvent({ command: "Login initiated" }); diff --git a/packages/cli/task-context/src/TaskContext.ts b/packages/cli/task-context/src/TaskContext.ts index 79825f13ff7..ca4939bdc55 100644 --- a/packages/cli/task-context/src/TaskContext.ts +++ b/packages/cli/task-context/src/TaskContext.ts @@ -11,7 +11,7 @@ export interface TaskContext { params: CreateInteractiveTaskParams, run: (context: InteractiveTaskContext) => void | Promise ) => Promise; - instrumentPostHogEvent: (event: PosthogEvent) => void; + instrumentPostHogEvent: (event: PosthogEvent) => Promise; } export interface InteractiveTaskContext extends TaskContext { diff --git a/packages/seed/src/TaskContextImpl.ts b/packages/seed/src/TaskContextImpl.ts index e377251c64b..1d8696a7147 100644 --- a/packages/seed/src/TaskContextImpl.ts +++ b/packages/seed/src/TaskContextImpl.ts @@ -26,7 +26,7 @@ export declare namespace TaskContextImpl { */ onResult?: (result: TaskResult) => void; shouldBufferLogs: boolean; - instrumentPostHogEvent: (event: PosthogEvent) => void; + instrumentPostHogEvent: (event: PosthogEvent) => Promise; } } @@ -39,7 +39,7 @@ export class TaskContextImpl implements Startable, Finishable, Task private bufferedLogs: Log[] = []; protected status: "notStarted" | "running" | "finished" = "notStarted"; private onResult: ((result: TaskResult) => void) | undefined; - private instrumentPostHogEventImpl: (event: PosthogEvent) => void; + private instrumentPostHogEventImpl: (event: PosthogEvent) => Promise; public constructor({ logImmediately, logPrefix, @@ -92,7 +92,7 @@ export class TaskContextImpl implements Startable, Finishable, Task return this.result; } - public instrumentPostHogEvent(event: PosthogEvent): void { + public async instrumentPostHogEvent(event: PosthogEvent): Promise { this.instrumentPostHogEventImpl(event); } @@ -135,7 +135,7 @@ export class TaskContextImpl implements Startable, Finishable, Task takeOverTerminal: this.takeOverTerminal, onResult: this.onResult, shouldBufferLogs: this.shouldBufferLogs, - instrumentPostHogEvent: (event) => this.instrumentPostHogEventImpl(event) + instrumentPostHogEvent: async (event) => await this.instrumentPostHogEventImpl(event) }); this.subtasks.push(subtask); return subtask; diff --git a/packages/seed/src/commands/test/TaskContextFactory.ts b/packages/seed/src/commands/test/TaskContextFactory.ts index b88d04927b6..4a316f57bea 100644 --- a/packages/seed/src/commands/test/TaskContextFactory.ts +++ b/packages/seed/src/commands/test/TaskContextFactory.ts @@ -46,7 +46,7 @@ export class TaskContextFactory { return; }, shouldBufferLogs: false, - instrumentPostHogEvent: () => { + instrumentPostHogEvent: async () => { return; }, logPrefix: prefixWithColor