Skip to content

Commit

Permalink
fix(cli): await Posthog API calls (#4581)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Sep 8, 2024
1 parent b2ff926 commit a75f278
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 40 deletions.
10 changes: 3 additions & 7 deletions packages/cli/cli/src/cli-context/CliContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,7 @@ export class CliContext {
return result;
}

public instrumentPostHogEvent(event: PosthogEvent): void {
void this.instrumentPostHogEventAsync(event);
}

private async instrumentPostHogEventAsync(event: PosthogEvent): Promise<void> {
public async instrumentPostHogEvent(event: PosthogEvent): Promise<void> {
(await getPosthogManager()).sendEvent(event);
}

Expand Down Expand Up @@ -225,8 +221,8 @@ export class CliContext {
this.didSucceed = false;
}
},
instrumentPostHogEvent: (event) => {
this.instrumentPostHogEvent(event);
instrumentPostHogEvent: async (event) => {
await this.instrumentPostHogEvent(event);
},
shouldBufferLogs: false
};
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/cli/src/cli-context/TaskContextImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export declare namespace TaskContextImpl {
*/
onResult?: (result: TaskResult) => void;
shouldBufferLogs: boolean;
instrumentPostHogEvent: (event: PosthogEvent) => void;
instrumentPostHogEvent: (event: PosthogEvent) => Promise<void>;
}
}

Expand All @@ -37,7 +37,7 @@ export class TaskContextImpl implements Startable<TaskContext>, 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<void>;
public constructor({
logImmediately,
logPrefix,
Expand Down Expand Up @@ -90,8 +90,8 @@ export class TaskContextImpl implements Startable<TaskContext>, Finishable, Task
return this.result;
}

public instrumentPostHogEvent(event: PosthogEvent): void {
this.instrumentPostHogEventImpl(event);
public async instrumentPostHogEvent(event: PosthogEvent): Promise<void> {
await this.instrumentPostHogEventImpl(event);
}

protected logAtLevel(level: LogLevel, ...parts: string[]): void {
Expand Down Expand Up @@ -133,7 +133,7 @@ export class TaskContextImpl implements Startable<TaskContext>, 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;
Expand Down
16 changes: 8 additions & 8 deletions packages/cli/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async function runCli() {
});
}
} catch (error) {
cliContext.instrumentPostHogEvent({
await cliContext.instrumentPostHogEvent({
command: process.argv.join(" "),
properties: {
failed: true,
Expand Down Expand Up @@ -676,7 +676,7 @@ function addUpdateApiSpecCommand(cli: Argv<GlobalCliOptions>, 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({
Expand All @@ -702,7 +702,7 @@ function addLoginCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
}),
async (argv) => {
await cliContext.runTask(async (context) => {
cliContext.instrumentPostHogEvent({
await cliContext.instrumentPostHogEvent({
command: "fern login"
});
await login(context, { useDeviceCodeFlow: argv.deviceCode });
Expand All @@ -727,7 +727,7 @@ function addFormatCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
description: "Only run the command on the provided API"
}),
async (argv) => {
cliContext.instrumentPostHogEvent({
await cliContext.instrumentPostHogEvent({
command: "fern format"
});
await formatWorkspaces({
Expand Down Expand Up @@ -761,7 +761,7 @@ function addTestCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
description: "Run the tests configured to a specific language"
}),
async (argv) => {
cliContext.instrumentPostHogEvent({
await cliContext.instrumentPostHogEvent({
command: "fern test"
});
await testOutput({
Expand Down Expand Up @@ -794,7 +794,7 @@ function addMockCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
description: "The API to mock."
}),
async (argv) => {
cliContext.instrumentPostHogEvent({
await cliContext.instrumentPostHogEvent({
command: "fern mock"
});
await mockServer({
Expand Down Expand Up @@ -826,7 +826,7 @@ function addWriteOverridesCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCo
})
],
async (argv) => {
cliContext.instrumentPostHogEvent({
await cliContext.instrumentPostHogEvent({
command: "fern generate-overrides"
});
await writeOverridesForWorkspaces({
Expand Down Expand Up @@ -856,7 +856,7 @@ function addWriteDefinitionCommand(cli: Argv<GlobalCliOptions>, cliContext: CliC
description: "Write the definition for a particular SDK language"
}),
async (argv) => {
cliContext.instrumentPostHogEvent({
await cliContext.instrumentPostHogEvent({
command: "fern write-definition"
});
await writeDefinitionForWorkspaces({
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/cli/src/cliV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function addGetOrganizationCommand(cli: Argv<GlobalCliOptions>, 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
Expand Down Expand Up @@ -78,7 +78,7 @@ export function addGeneratorCommands(cli: Argv<GlobalCliOptions>, 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
Expand Down Expand Up @@ -128,7 +128,7 @@ export function addGeneratorCommands(cli: Argv<GlobalCliOptions>, cliContext: Cl
choices: Object.values(FernRegistry.generators.ReleaseType)
}),
async (argv) => {
cliContext.instrumentPostHogEvent({
await cliContext.instrumentPostHogEvent({
command: "fern generator upgrade",
properties: {
generator: argv.generator,
Expand Down Expand Up @@ -178,7 +178,7 @@ export function addGeneratorCommands(cli: Argv<GlobalCliOptions>, 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,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/cli/src/commands/docs-dev/devDocsWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function previewDocsWorkspace({
return;
}

cliContext.instrumentPostHogEvent({
await cliContext.instrumentPostHogEvent({
orgId: project.config.organization,
command: "fern docs dev"
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function generateAPIWorkspaces({
token = currentToken;
}

cliContext.instrumentPostHogEvent({
await cliContext.instrumentPostHogEvent({
orgId: project.config.organization,
command: "fern generate",
properties: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function generateDocsWorkspace({
});
}

cliContext.instrumentPostHogEvent({
await cliContext.instrumentPostHogEvent({
orgId: project.config.organization,
command: "fern generate --docs"
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/cli/src/commands/mock/mockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function mockServer({
project: Project;
port: number | undefined;
}): Promise<void> {
cliContext.instrumentPostHogEvent({
await cliContext.instrumentPostHogEvent({
orgId: project.config.organization,
command: "fern mock"
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/cli/src/commands/test/testOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function testOutput({
testCommand: string | undefined;
generationLanguage: generatorsYml.GenerationLanguage | undefined;
}): Promise<void> {
cliContext.instrumentPostHogEvent({
await cliContext.instrumentPostHogEvent({
orgId: project.config.organization,
command: "fern test"
});
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/cli/versions.yml
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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<never> {
await context.instrumentPostHogEvent({
command: "docs-generation",
properties: {
error: JSON.stringify(error)
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/login/src/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function login(
context: TaskContext,
{ useDeviceCodeFlow = false }: { useDeviceCodeFlow?: boolean } = {}
): Promise<FernUserToken> {
context.instrumentPostHogEvent({
await context.instrumentPostHogEvent({
command: "Login initiated"
});

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/task-context/src/TaskContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface TaskContext {
params: CreateInteractiveTaskParams,
run: (context: InteractiveTaskContext) => void | Promise<void>
) => Promise<boolean>;
instrumentPostHogEvent: (event: PosthogEvent) => void;
instrumentPostHogEvent: (event: PosthogEvent) => Promise<void>;
}

export interface InteractiveTaskContext extends TaskContext {
Expand Down
8 changes: 4 additions & 4 deletions packages/seed/src/TaskContextImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export declare namespace TaskContextImpl {
*/
onResult?: (result: TaskResult) => void;
shouldBufferLogs: boolean;
instrumentPostHogEvent: (event: PosthogEvent) => void;
instrumentPostHogEvent: (event: PosthogEvent) => Promise<void>;
}
}

Expand All @@ -39,7 +39,7 @@ export class TaskContextImpl implements Startable<TaskContext>, 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<void>;
public constructor({
logImmediately,
logPrefix,
Expand Down Expand Up @@ -92,7 +92,7 @@ export class TaskContextImpl implements Startable<TaskContext>, Finishable, Task
return this.result;
}

public instrumentPostHogEvent(event: PosthogEvent): void {
public async instrumentPostHogEvent(event: PosthogEvent): Promise<void> {
this.instrumentPostHogEventImpl(event);
}

Expand Down Expand Up @@ -135,7 +135,7 @@ export class TaskContextImpl implements Startable<TaskContext>, 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;
Expand Down
2 changes: 1 addition & 1 deletion packages/seed/src/commands/test/TaskContextFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class TaskContextFactory {
return;
},
shouldBufferLogs: false,
instrumentPostHogEvent: () => {
instrumentPostHogEvent: async () => {
return;
},
logPrefix: prefixWithColor
Expand Down

0 comments on commit a75f278

Please sign in to comment.