Skip to content

Commit 47cb12c

Browse files
committed
refactor(cli): make instrumentPostHogEvent synchronous
Made-with: Cursor
1 parent 62c9885 commit 47cb12c

21 files changed

Lines changed: 82 additions & 70 deletions

File tree

packages/cli/cli-v2/src/context/adapter/TaskContextAdapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ export class TaskContextAdapter implements TaskContext {
115115
}
116116
}
117117

118-
public async instrumentPostHogEvent(_event: PosthogEvent): Promise<void> {
119-
// no-op for now
118+
public instrumentPostHogEvent(_event: PosthogEvent): void {
119+
// no-op — v2 uses TelemetryClient.sendEvent directly
120120
}
121121

122122
private formatError(error: unknown): string | undefined {

packages/cli/cli/src/cli-context/CliContext.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Log, logErrorMessage, TtyAwareLogger } from "@fern-api/cli-logger";
22
import { createLogger, LOG_LEVELS, LogLevel } from "@fern-api/logger";
3-
import { getPosthogManager } from "@fern-api/posthog-manager";
3+
import { getPosthogManager, PosthogManager } from "@fern-api/posthog-manager";
44
import { Project } from "@fern-api/project-loader";
55
import { isVersionAhead } from "@fern-api/semver-utils";
66
import { Finishable, PosthogEvent, Startable, TaskAbortSignal, TaskContext, TaskResult } from "@fern-api/task-context";
@@ -31,6 +31,7 @@ export interface FernUpgradeInfo {
3131
export class CliContext {
3232
public readonly environment: CliEnvironment;
3333
private readonly sentryClient: SentryClient;
34+
private readonly posthogManager: PosthogManager;
3435

3536
private didSucceed = true;
3637

@@ -42,9 +43,23 @@ export class CliContext {
4243
private readonly stdoutRedirector = new StdoutRedirector();
4344
private jsonMode = false;
4445

45-
constructor(stdout: NodeJS.WriteStream, stderr: NodeJS.WriteStream, { isLocal }: { isLocal?: boolean }) {
46+
public static async create(
47+
stdout: NodeJS.WriteStream,
48+
stderr: NodeJS.WriteStream,
49+
{ isLocal }: { isLocal?: boolean }
50+
): Promise<CliContext> {
51+
const posthogManager = await getPosthogManager();
52+
return new CliContext(stdout, stderr, { isLocal, posthogManager });
53+
}
54+
55+
private constructor(
56+
stdout: NodeJS.WriteStream,
57+
stderr: NodeJS.WriteStream,
58+
{ isLocal, posthogManager }: { isLocal?: boolean; posthogManager: PosthogManager }
59+
) {
4660
this.ttyAwareLogger = new TtyAwareLogger(stdout, stderr);
4761
this.isLocal = isLocal ?? false;
62+
this.posthogManager = posthogManager;
4863

4964
const packageName = this.getPackageName();
5065
const packageVersion = this.getPackageVersion();
@@ -138,8 +153,7 @@ export class CliContext {
138153
await this.nudgeUpgradeIfAvailable();
139154
}
140155
this.ttyAwareLogger.finish();
141-
const posthogManager = await getPosthogManager();
142-
await posthogManager.flush();
156+
await this.posthogManager.flush();
143157
await this.sentryClient.flush();
144158
this.exitProgram({ code });
145159
}
@@ -236,9 +250,9 @@ export class CliContext {
236250
return result;
237251
}
238252

239-
public async instrumentPostHogEvent(event: PosthogEvent): Promise<void> {
253+
public instrumentPostHogEvent(event: PosthogEvent): void {
240254
if (!this.isLocal) {
241-
(await getPosthogManager()).sendEvent(event);
255+
this.posthogManager.sendEvent(event);
242256
}
243257
}
244258

@@ -283,8 +297,8 @@ export class CliContext {
283297
this.didSucceed = false;
284298
}
285299
},
286-
instrumentPostHogEvent: async (event) => {
287-
await this.instrumentPostHogEvent(event);
300+
instrumentPostHogEvent: (event) => {
301+
this.instrumentPostHogEvent(event);
288302
},
289303
shouldBufferLogs: false
290304
};

packages/cli/cli/src/cli-context/TaskContextImpl.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export declare namespace TaskContextImpl {
2323
*/
2424
onResult?: (result: TaskResult) => void;
2525
shouldBufferLogs: boolean;
26-
instrumentPostHogEvent: (event: PosthogEvent) => Promise<void>;
26+
instrumentPostHogEvent: (event: PosthogEvent) => void;
2727
}
2828
}
2929

@@ -36,7 +36,7 @@ export class TaskContextImpl implements Startable<TaskContext>, Finishable, Task
3636
private bufferedLogs: Log[] = [];
3737
protected status: "notStarted" | "running" | "finished" = "notStarted";
3838
private onResult: ((result: TaskResult) => void) | undefined;
39-
private instrumentPostHogEventImpl: (event: PosthogEvent) => Promise<void>;
39+
private instrumentPostHogEventImpl: (event: PosthogEvent) => void;
4040
public constructor({
4141
logImmediately,
4242
logPrefix,
@@ -89,8 +89,8 @@ export class TaskContextImpl implements Startable<TaskContext>, Finishable, Task
8989
return this.result;
9090
}
9191

92-
public async instrumentPostHogEvent(event: PosthogEvent): Promise<void> {
93-
await this.instrumentPostHogEventImpl(event);
92+
public instrumentPostHogEvent(event: PosthogEvent): void {
93+
this.instrumentPostHogEventImpl(event);
9494
}
9595

9696
protected logAtLevel(level: LogLevel, ...parts: string[]): void {
@@ -132,7 +132,7 @@ export class TaskContextImpl implements Startable<TaskContext>, Finishable, Task
132132
takeOverTerminal: this.takeOverTerminal,
133133
onResult: this.onResult,
134134
shouldBufferLogs: this.shouldBufferLogs,
135-
instrumentPostHogEvent: async (event) => await this.instrumentPostHogEventImpl(event)
135+
instrumentPostHogEvent: (event) => this.instrumentPostHogEventImpl(event)
136136
});
137137
this.subtasks.push(subtask);
138138
return subtask;

packages/cli/cli/src/cli.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const USE_NODE_18_OR_ABOVE_MESSAGE = "The Fern CLI requires Node 18+ or above.";
9191

9292
async function runCli() {
9393
const isLocal = process.argv.includes("--local");
94-
const cliContext = new CliContext(process.stdout, process.stderr, { isLocal });
94+
const cliContext = await CliContext.create(process.stdout, process.stderr, { isLocal });
9595

9696
const exit = async () => {
9797
await cliContext.exit();
@@ -131,7 +131,7 @@ async function runCli() {
131131
});
132132
}
133133
} catch (error) {
134-
await cliContext.instrumentPostHogEvent({
134+
cliContext.instrumentPostHogEvent({
135135
command: process.argv.join(" "),
136136
properties: {
137137
failed: true,
@@ -532,7 +532,7 @@ function addSdkDiffCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext)
532532
description: "Output result as JSON"
533533
}),
534534
async (argv) => {
535-
await cliContext.instrumentPostHogEvent({
535+
cliContext.instrumentPostHogEvent({
536536
command: "fern sdk-diff"
537537
});
538538

@@ -1347,7 +1347,7 @@ function addUpdateApiSpecCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCon
13471347
default: 2
13481348
}),
13491349
async (argv) => {
1350-
await cliContext.instrumentPostHogEvent({
1350+
cliContext.instrumentPostHogEvent({
13511351
command: "fern api update"
13521352
});
13531353
await updateApiSpec({
@@ -1378,7 +1378,7 @@ function addSelfUpdateCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContex
13781378
default: false
13791379
}),
13801380
async (argv) => {
1381-
await cliContext.instrumentPostHogEvent({
1381+
cliContext.instrumentPostHogEvent({
13821382
command: "fern self-update"
13831383
});
13841384
await selfUpdate({
@@ -1402,7 +1402,7 @@ function addLoginCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14021402
}),
14031403
async (argv) => {
14041404
await cliContext.runTask(async (context) => {
1405-
await cliContext.instrumentPostHogEvent({
1405+
cliContext.instrumentPostHogEvent({
14061406
command: "fern login"
14071407
});
14081408
await login(context, { useDeviceCodeFlow: argv.deviceCode });
@@ -1418,7 +1418,7 @@ function addLogoutCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14181418
(yargs) => yargs,
14191419
async () => {
14201420
await cliContext.runTask(async (context) => {
1421-
await cliContext.instrumentPostHogEvent({
1421+
cliContext.instrumentPostHogEvent({
14221422
command: "fern logout"
14231423
});
14241424
await logout(context);
@@ -1443,7 +1443,7 @@ function addFormatCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14431443
description: "Only run the command on the provided API"
14441444
}),
14451445
async (argv) => {
1446-
await cliContext.instrumentPostHogEvent({
1446+
cliContext.instrumentPostHogEvent({
14471447
command: "fern format"
14481448
});
14491449
await formatWorkspaces({
@@ -1477,7 +1477,7 @@ function addTestCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14771477
description: "Run the tests configured to a specific language"
14781478
}),
14791479
async (argv) => {
1480-
await cliContext.instrumentPostHogEvent({
1480+
cliContext.instrumentPostHogEvent({
14811481
command: "fern test"
14821482
});
14831483
await testOutput({
@@ -1510,7 +1510,7 @@ function addMockCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
15101510
description: "The API to mock."
15111511
}),
15121512
async (argv) => {
1513-
await cliContext.instrumentPostHogEvent({
1513+
cliContext.instrumentPostHogEvent({
15141514
command: "fern mock"
15151515
});
15161516
await mockServer({
@@ -1555,7 +1555,7 @@ function addOverridesCompareCommand(cli: Argv<GlobalCliOptions>, cliContext: Cli
15551555
description: "Path to write the overrides file (defaults to <original>-overrides.yml)"
15561556
}),
15571557
async (argv) => {
1558-
await cliContext.instrumentPostHogEvent({
1558+
cliContext.instrumentPostHogEvent({
15591559
command: "fern overrides compare"
15601560
});
15611561
const originalPath = resolve(cwd(), argv.original as string);
@@ -1588,7 +1588,7 @@ function addOverridesWriteCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCo
15881588
})
15891589
],
15901590
async (argv) => {
1591-
await cliContext.instrumentPostHogEvent({
1591+
cliContext.instrumentPostHogEvent({
15921592
command: "fern overrides write"
15931593
});
15941594
await writeOverridesForWorkspaces({
@@ -1624,7 +1624,7 @@ function addWriteOverridesCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCo
16241624
],
16251625
async (argv) => {
16261626
cliContext.logger.warn("The 'write-overrides' command is deprecated. Use 'fern overrides write' instead.");
1627-
await cliContext.instrumentPostHogEvent({
1627+
cliContext.instrumentPostHogEvent({
16281628
command: "fern write-overrides"
16291629
});
16301630
await writeOverridesForWorkspaces({
@@ -1659,7 +1659,7 @@ function addWriteDefinitionCommand(cli: Argv<GlobalCliOptions>, cliContext: CliC
16591659
}),
16601660
async (argv) => {
16611661
const preserveSchemaIds = argv.preserveSchemas != null;
1662-
await cliContext.instrumentPostHogEvent({
1662+
cliContext.instrumentPostHogEvent({
16631663
command: "fern write-definition"
16641664
});
16651665
await writeDefinitionForWorkspaces({
@@ -1708,7 +1708,7 @@ function addDocsMdGenerateCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCo
17081708
description: "Name of a specific library defined in docs.yml to generate docs for"
17091709
}),
17101710
async (argv) => {
1711-
await cliContext.instrumentPostHogEvent({
1711+
cliContext.instrumentPostHogEvent({
17121712
command: "fern docs md generate"
17131713
});
17141714

@@ -1749,7 +1749,7 @@ function addDocsDiffCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext)
17491749
description: "Output directory for diff images"
17501750
}),
17511751
async (argv) => {
1752-
await cliContext.instrumentPostHogEvent({
1752+
cliContext.instrumentPostHogEvent({
17531753
command: "fern docs diff"
17541754
});
17551755

@@ -1794,7 +1794,7 @@ function addDocsPreviewListCommand(cli: Argv<GlobalCliOptions>, cliContext: CliC
17941794
description: "Page number for pagination (starts at 1)"
17951795
}),
17961796
async (argv) => {
1797-
await cliContext.instrumentPostHogEvent({
1797+
cliContext.instrumentPostHogEvent({
17981798
command: "fern docs preview list"
17991799
});
18001800
await listDocsPreview({
@@ -1818,7 +1818,7 @@ function addDocsPreviewDeleteCommand(cli: Argv<GlobalCliOptions>, cliContext: Cl
18181818
demandOption: true
18191819
}),
18201820
async (argv) => {
1821-
await cliContext.instrumentPostHogEvent({
1821+
cliContext.instrumentPostHogEvent({
18221822
command: "fern docs preview delete"
18231823
});
18241824
await deleteDocsPreview({
@@ -1937,7 +1937,7 @@ function addDocsMdCheckCommand(cli: Argv<GlobalCliOptions>, cliContext: CliConte
19371937
// No additional options for this command
19381938
},
19391939
async () => {
1940-
await cliContext.instrumentPostHogEvent({
1940+
cliContext.instrumentPostHogEvent({
19411941
command: "fern docs md check"
19421942
});
19431943

@@ -1993,7 +1993,7 @@ function addGenerateJsonschemaCommand(cli: Argv<GlobalCliOptions>, cliContext: C
19931993
description: "The type to generate JSON Schema for (e.g. 'MySchema' or 'mypackage.MySchema')"
19941994
}),
19951995
async (argv) => {
1996-
await cliContext.instrumentPostHogEvent({
1996+
cliContext.instrumentPostHogEvent({
19971997
command: "fern jsonschema",
19981998
properties: {
19991999
output: argv.output
@@ -2023,7 +2023,7 @@ function addWriteDocsDefinitionCommand(cli: Argv<GlobalCliOptions>, cliContext:
20232023
demandOption: true
20242024
}),
20252025
async (argv) => {
2026-
await cliContext.instrumentPostHogEvent({
2026+
cliContext.instrumentPostHogEvent({
20272027
command: "fern write-docs-definition",
20282028
properties: {
20292029
outputPath: argv.outputPath
@@ -2054,7 +2054,7 @@ function addWriteTranslationCommand(cli: Argv<GlobalCliOptions>, cliContext: Cli
20542054
description: "Return content as-is without calling the translation service"
20552055
}),
20562056
async (argv) => {
2057-
await cliContext.instrumentPostHogEvent({
2057+
cliContext.instrumentPostHogEvent({
20582058
command: "fern write-translation"
20592059
});
20602060

@@ -2091,7 +2091,7 @@ function addExportCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
20912091
default: 2
20922092
}),
20932093
async (argv) => {
2094-
await cliContext.instrumentPostHogEvent({
2094+
cliContext.instrumentPostHogEvent({
20952095
command: "fern export",
20962096
properties: {
20972097
outputPath: argv.outputPath
@@ -2135,7 +2135,7 @@ function addEnrichCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
21352135
demandOption: true
21362136
}),
21372137
async (argv) => {
2138-
await cliContext.instrumentPostHogEvent({
2138+
cliContext.instrumentPostHogEvent({
21392139
command: "fern api enrich"
21402140
});
21412141
const openapiPath = resolve(cwd(), argv.openapi as string);
@@ -2182,7 +2182,7 @@ function addSdkPreviewCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContex
21822182
description: "Output result as JSON"
21832183
}),
21842184
async (argv) => {
2185-
await cliContext.instrumentPostHogEvent({
2185+
cliContext.instrumentPostHogEvent({
21862186
command: "fern sdk preview"
21872187
});
21882188
const generatorFilter =
@@ -2339,7 +2339,7 @@ function addReplayInitCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContex
23392339
description: "Overwrite existing lockfile if Replay is already initialized"
23402340
}),
23412341
async (argv) => {
2342-
await cliContext.instrumentPostHogEvent({
2342+
cliContext.instrumentPostHogEvent({
23432343
command: "fern replay init"
23442344
});
23452345

@@ -2457,7 +2457,7 @@ function addReplayResolveCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCon
24572457
description: "Skip checking for remaining conflict markers before committing"
24582458
}),
24592459
async (argv) => {
2460-
await cliContext.instrumentPostHogEvent({
2460+
cliContext.instrumentPostHogEvent({
24612461
command: "fern replay resolve"
24622462
});
24632463

0 commit comments

Comments
 (0)