Skip to content

Commit 0f6924a

Browse files
committed
refactor(cli): make instrumentPostHogEvent synchronous
Made-with: Cursor
1 parent 06590df commit 0f6924a

File tree

21 files changed

+82
-70
lines changed

21 files changed

+82
-70
lines changed

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({
@@ -1407,7 +1407,7 @@ function addLoginCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14071407
}),
14081408
async (argv) => {
14091409
await cliContext.runTask(async (context) => {
1410-
await cliContext.instrumentPostHogEvent({
1410+
cliContext.instrumentPostHogEvent({
14111411
command: "fern login"
14121412
});
14131413
await login(context, { useDeviceCodeFlow: argv.deviceCode, email: argv.email });
@@ -1423,7 +1423,7 @@ function addLogoutCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14231423
(yargs) => yargs,
14241424
async () => {
14251425
await cliContext.runTask(async (context) => {
1426-
await cliContext.instrumentPostHogEvent({
1426+
cliContext.instrumentPostHogEvent({
14271427
command: "fern logout"
14281428
});
14291429
await logout(context);
@@ -1448,7 +1448,7 @@ function addFormatCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14481448
description: "Only run the command on the provided API"
14491449
}),
14501450
async (argv) => {
1451-
await cliContext.instrumentPostHogEvent({
1451+
cliContext.instrumentPostHogEvent({
14521452
command: "fern format"
14531453
});
14541454
await formatWorkspaces({
@@ -1482,7 +1482,7 @@ function addTestCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14821482
description: "Run the tests configured to a specific language"
14831483
}),
14841484
async (argv) => {
1485-
await cliContext.instrumentPostHogEvent({
1485+
cliContext.instrumentPostHogEvent({
14861486
command: "fern test"
14871487
});
14881488
await testOutput({
@@ -1515,7 +1515,7 @@ function addMockCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
15151515
description: "The API to mock."
15161516
}),
15171517
async (argv) => {
1518-
await cliContext.instrumentPostHogEvent({
1518+
cliContext.instrumentPostHogEvent({
15191519
command: "fern mock"
15201520
});
15211521
await mockServer({
@@ -1560,7 +1560,7 @@ function addOverridesCompareCommand(cli: Argv<GlobalCliOptions>, cliContext: Cli
15601560
description: "Path to write the overrides file (defaults to <original>-overrides.yml)"
15611561
}),
15621562
async (argv) => {
1563-
await cliContext.instrumentPostHogEvent({
1563+
cliContext.instrumentPostHogEvent({
15641564
command: "fern overrides compare"
15651565
});
15661566
const originalPath = resolve(cwd(), argv.original as string);
@@ -1593,7 +1593,7 @@ function addOverridesWriteCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCo
15931593
})
15941594
],
15951595
async (argv) => {
1596-
await cliContext.instrumentPostHogEvent({
1596+
cliContext.instrumentPostHogEvent({
15971597
command: "fern overrides write"
15981598
});
15991599
await writeOverridesForWorkspaces({
@@ -1629,7 +1629,7 @@ function addWriteOverridesCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCo
16291629
],
16301630
async (argv) => {
16311631
cliContext.logger.warn("The 'write-overrides' command is deprecated. Use 'fern overrides write' instead.");
1632-
await cliContext.instrumentPostHogEvent({
1632+
cliContext.instrumentPostHogEvent({
16331633
command: "fern write-overrides"
16341634
});
16351635
await writeOverridesForWorkspaces({
@@ -1664,7 +1664,7 @@ function addWriteDefinitionCommand(cli: Argv<GlobalCliOptions>, cliContext: CliC
16641664
}),
16651665
async (argv) => {
16661666
const preserveSchemaIds = argv.preserveSchemas != null;
1667-
await cliContext.instrumentPostHogEvent({
1667+
cliContext.instrumentPostHogEvent({
16681668
command: "fern write-definition"
16691669
});
16701670
await writeDefinitionForWorkspaces({
@@ -1713,7 +1713,7 @@ function addDocsMdGenerateCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCo
17131713
description: "Name of a specific library defined in docs.yml to generate docs for"
17141714
}),
17151715
async (argv) => {
1716-
await cliContext.instrumentPostHogEvent({
1716+
cliContext.instrumentPostHogEvent({
17171717
command: "fern docs md generate"
17181718
});
17191719

@@ -1754,7 +1754,7 @@ function addDocsDiffCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext)
17541754
description: "Output directory for diff images"
17551755
}),
17561756
async (argv) => {
1757-
await cliContext.instrumentPostHogEvent({
1757+
cliContext.instrumentPostHogEvent({
17581758
command: "fern docs diff"
17591759
});
17601760

@@ -1799,7 +1799,7 @@ function addDocsPreviewListCommand(cli: Argv<GlobalCliOptions>, cliContext: CliC
17991799
description: "Page number for pagination (starts at 1)"
18001800
}),
18011801
async (argv) => {
1802-
await cliContext.instrumentPostHogEvent({
1802+
cliContext.instrumentPostHogEvent({
18031803
command: "fern docs preview list"
18041804
});
18051805
await listDocsPreview({
@@ -1823,7 +1823,7 @@ function addDocsPreviewDeleteCommand(cli: Argv<GlobalCliOptions>, cliContext: Cl
18231823
demandOption: true
18241824
}),
18251825
async (argv) => {
1826-
await cliContext.instrumentPostHogEvent({
1826+
cliContext.instrumentPostHogEvent({
18271827
command: "fern docs preview delete"
18281828
});
18291829
await deleteDocsPreview({
@@ -1942,7 +1942,7 @@ function addDocsMdCheckCommand(cli: Argv<GlobalCliOptions>, cliContext: CliConte
19421942
// No additional options for this command
19431943
},
19441944
async () => {
1945-
await cliContext.instrumentPostHogEvent({
1945+
cliContext.instrumentPostHogEvent({
19461946
command: "fern docs md check"
19471947
});
19481948

@@ -1998,7 +1998,7 @@ function addGenerateJsonschemaCommand(cli: Argv<GlobalCliOptions>, cliContext: C
19981998
description: "The type to generate JSON Schema for (e.g. 'MySchema' or 'mypackage.MySchema')"
19991999
}),
20002000
async (argv) => {
2001-
await cliContext.instrumentPostHogEvent({
2001+
cliContext.instrumentPostHogEvent({
20022002
command: "fern jsonschema",
20032003
properties: {
20042004
output: argv.output
@@ -2028,7 +2028,7 @@ function addWriteDocsDefinitionCommand(cli: Argv<GlobalCliOptions>, cliContext:
20282028
demandOption: true
20292029
}),
20302030
async (argv) => {
2031-
await cliContext.instrumentPostHogEvent({
2031+
cliContext.instrumentPostHogEvent({
20322032
command: "fern write-docs-definition",
20332033
properties: {
20342034
outputPath: argv.outputPath
@@ -2059,7 +2059,7 @@ function addWriteTranslationCommand(cli: Argv<GlobalCliOptions>, cliContext: Cli
20592059
description: "Return content as-is without calling the translation service"
20602060
}),
20612061
async (argv) => {
2062-
await cliContext.instrumentPostHogEvent({
2062+
cliContext.instrumentPostHogEvent({
20632063
command: "fern write-translation"
20642064
});
20652065

@@ -2096,7 +2096,7 @@ function addExportCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
20962096
default: 2
20972097
}),
20982098
async (argv) => {
2099-
await cliContext.instrumentPostHogEvent({
2099+
cliContext.instrumentPostHogEvent({
21002100
command: "fern export",
21012101
properties: {
21022102
outputPath: argv.outputPath
@@ -2140,7 +2140,7 @@ function addEnrichCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
21402140
demandOption: true
21412141
}),
21422142
async (argv) => {
2143-
await cliContext.instrumentPostHogEvent({
2143+
cliContext.instrumentPostHogEvent({
21442144
command: "fern api enrich"
21452145
});
21462146
const openapiPath = resolve(cwd(), argv.openapi as string);
@@ -2187,7 +2187,7 @@ function addSdkPreviewCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContex
21872187
description: "Output result as JSON"
21882188
}),
21892189
async (argv) => {
2190-
await cliContext.instrumentPostHogEvent({
2190+
cliContext.instrumentPostHogEvent({
21912191
command: "fern sdk preview"
21922192
});
21932193
const generatorFilter =
@@ -2346,7 +2346,7 @@ function addReplayInitCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContex
23462346
description: "Overwrite existing lockfile if Replay is already initialized"
23472347
}),
23482348
async (argv) => {
2349-
await cliContext.instrumentPostHogEvent({
2349+
cliContext.instrumentPostHogEvent({
23502350
command: "fern replay init"
23512351
});
23522352

@@ -2468,7 +2468,7 @@ function addReplayResolveCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCon
24682468
description: "Skip checking for remaining conflict markers before committing"
24692469
}),
24702470
async (argv) => {
2471-
await cliContext.instrumentPostHogEvent({
2471+
cliContext.instrumentPostHogEvent({
24722472
command: "fern replay resolve"
24732473
});
24742474

0 commit comments

Comments
 (0)