Skip to content

Commit cbb6b50

Browse files
committed
refactor(cli): make instrumentPostHogEvent synchronous
Made-with: Cursor
1 parent d00b1f5 commit cbb6b50

File tree

22 files changed

+90
-71
lines changed

22 files changed

+90
-71
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/__test__/mockCliContext.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ export class MockCliContext extends CliContext {
88
process.env.CLI_VERSION = "0.0.0";
99
process.env.CLI_NAME = "test-cli";
1010

11-
super(process.stdout, process.stderr, { isLocal: true });
11+
super(process.stdout, process.stderr, {
12+
isLocal: true,
13+
posthogManager: {
14+
sendEvent: () => undefined,
15+
identify: () => undefined,
16+
flush: async () => undefined
17+
}
18+
});
1219
}
1320

1421
// Override exit to prevent process.exit in tests

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+
protected 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
}
@@ -240,9 +254,9 @@ export class CliContext {
240254
return result;
241255
}
242256

243-
public async instrumentPostHogEvent(event: PosthogEvent): Promise<void> {
257+
public instrumentPostHogEvent(event: PosthogEvent): void {
244258
if (!this.isLocal) {
245-
(await getPosthogManager()).sendEvent(event);
259+
this.posthogManager.sendEvent(event);
246260
}
247261
}
248262

@@ -287,8 +301,8 @@ export class CliContext {
287301
this.didSucceed = false;
288302
}
289303
},
290-
instrumentPostHogEvent: async (event) => {
291-
await this.instrumentPostHogEvent(event);
304+
instrumentPostHogEvent: (event) => {
305+
this.instrumentPostHogEvent(event);
292306
},
293307
shouldBufferLogs: false
294308
};

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
@@ -95,7 +95,7 @@ async function runCli() {
9595
getOrCreateFernRunId();
9696

9797
const isLocal = process.argv.includes("--local");
98-
const cliContext = new CliContext(process.stdout, process.stderr, { isLocal });
98+
const cliContext = await CliContext.create(process.stdout, process.stderr, { isLocal });
9999

100100
const exit = async () => {
101101
await cliContext.exit();
@@ -135,7 +135,7 @@ async function runCli() {
135135
});
136136
}
137137
} catch (error) {
138-
await cliContext.instrumentPostHogEvent({
138+
cliContext.instrumentPostHogEvent({
139139
command: process.argv.join(" "),
140140
properties: {
141141
failed: true,
@@ -537,7 +537,7 @@ function addSdkDiffCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext)
537537
description: "Output result as JSON"
538538
}),
539539
async (argv) => {
540-
await cliContext.instrumentPostHogEvent({
540+
cliContext.instrumentPostHogEvent({
541541
command: "fern sdk-diff"
542542
});
543543

@@ -1355,7 +1355,7 @@ function addUpdateApiSpecCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCon
13551355
default: 2
13561356
}),
13571357
async (argv) => {
1358-
await cliContext.instrumentPostHogEvent({
1358+
cliContext.instrumentPostHogEvent({
13591359
command: "fern api update"
13601360
});
13611361
await updateApiSpec({
@@ -1386,7 +1386,7 @@ function addSelfUpdateCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContex
13861386
default: false
13871387
}),
13881388
async (argv) => {
1389-
await cliContext.instrumentPostHogEvent({
1389+
cliContext.instrumentPostHogEvent({
13901390
command: "fern self-update"
13911391
});
13921392
await selfUpdate({
@@ -1415,7 +1415,7 @@ function addLoginCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14151415
}),
14161416
async (argv) => {
14171417
await cliContext.runTask(async (context) => {
1418-
await cliContext.instrumentPostHogEvent({
1418+
cliContext.instrumentPostHogEvent({
14191419
command: "fern login"
14201420
});
14211421
await login(context, { useDeviceCodeFlow: argv.deviceCode, email: argv.email });
@@ -1431,7 +1431,7 @@ function addLogoutCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14311431
(yargs) => yargs,
14321432
async () => {
14331433
await cliContext.runTask(async (context) => {
1434-
await cliContext.instrumentPostHogEvent({
1434+
cliContext.instrumentPostHogEvent({
14351435
command: "fern logout"
14361436
});
14371437
await logout(context);
@@ -1456,7 +1456,7 @@ function addFormatCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14561456
description: "Only run the command on the provided API"
14571457
}),
14581458
async (argv) => {
1459-
await cliContext.instrumentPostHogEvent({
1459+
cliContext.instrumentPostHogEvent({
14601460
command: "fern format"
14611461
});
14621462
await formatWorkspaces({
@@ -1490,7 +1490,7 @@ function addTestCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14901490
description: "Run the tests configured to a specific language"
14911491
}),
14921492
async (argv) => {
1493-
await cliContext.instrumentPostHogEvent({
1493+
cliContext.instrumentPostHogEvent({
14941494
command: "fern test"
14951495
});
14961496
await testOutput({
@@ -1523,7 +1523,7 @@ function addMockCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
15231523
description: "The API to mock."
15241524
}),
15251525
async (argv) => {
1526-
await cliContext.instrumentPostHogEvent({
1526+
cliContext.instrumentPostHogEvent({
15271527
command: "fern mock"
15281528
});
15291529
await mockServer({
@@ -1568,7 +1568,7 @@ function addOverridesCompareCommand(cli: Argv<GlobalCliOptions>, cliContext: Cli
15681568
description: "Path to write the overrides file (defaults to <original>-overrides.yml)"
15691569
}),
15701570
async (argv) => {
1571-
await cliContext.instrumentPostHogEvent({
1571+
cliContext.instrumentPostHogEvent({
15721572
command: "fern overrides compare"
15731573
});
15741574
const originalPath = resolve(cwd(), argv.original as string);
@@ -1601,7 +1601,7 @@ function addOverridesWriteCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCo
16011601
})
16021602
],
16031603
async (argv) => {
1604-
await cliContext.instrumentPostHogEvent({
1604+
cliContext.instrumentPostHogEvent({
16051605
command: "fern overrides write"
16061606
});
16071607
await writeOverridesForWorkspaces({
@@ -1637,7 +1637,7 @@ function addWriteOverridesCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCo
16371637
],
16381638
async (argv) => {
16391639
cliContext.logger.warn("The 'write-overrides' command is deprecated. Use 'fern overrides write' instead.");
1640-
await cliContext.instrumentPostHogEvent({
1640+
cliContext.instrumentPostHogEvent({
16411641
command: "fern write-overrides"
16421642
});
16431643
await writeOverridesForWorkspaces({
@@ -1672,7 +1672,7 @@ function addWriteDefinitionCommand(cli: Argv<GlobalCliOptions>, cliContext: CliC
16721672
}),
16731673
async (argv) => {
16741674
const preserveSchemaIds = argv.preserveSchemas != null;
1675-
await cliContext.instrumentPostHogEvent({
1675+
cliContext.instrumentPostHogEvent({
16761676
command: "fern write-definition"
16771677
});
16781678
await writeDefinitionForWorkspaces({
@@ -1721,7 +1721,7 @@ function addDocsMdGenerateCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCo
17211721
description: "Name of a specific library defined in docs.yml to generate docs for"
17221722
}),
17231723
async (argv) => {
1724-
await cliContext.instrumentPostHogEvent({
1724+
cliContext.instrumentPostHogEvent({
17251725
command: "fern docs md generate"
17261726
});
17271727

@@ -1762,7 +1762,7 @@ function addDocsDiffCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext)
17621762
description: "Output directory for diff images"
17631763
}),
17641764
async (argv) => {
1765-
await cliContext.instrumentPostHogEvent({
1765+
cliContext.instrumentPostHogEvent({
17661766
command: "fern docs diff"
17671767
});
17681768

@@ -1807,7 +1807,7 @@ function addDocsPreviewListCommand(cli: Argv<GlobalCliOptions>, cliContext: CliC
18071807
description: "Page number for pagination (starts at 1)"
18081808
}),
18091809
async (argv) => {
1810-
await cliContext.instrumentPostHogEvent({
1810+
cliContext.instrumentPostHogEvent({
18111811
command: "fern docs preview list"
18121812
});
18131813
await listDocsPreview({
@@ -1831,7 +1831,7 @@ function addDocsPreviewDeleteCommand(cli: Argv<GlobalCliOptions>, cliContext: Cl
18311831
demandOption: true
18321832
}),
18331833
async (argv) => {
1834-
await cliContext.instrumentPostHogEvent({
1834+
cliContext.instrumentPostHogEvent({
18351835
command: "fern docs preview delete"
18361836
});
18371837
await deleteDocsPreview({
@@ -1950,7 +1950,7 @@ function addDocsMdCheckCommand(cli: Argv<GlobalCliOptions>, cliContext: CliConte
19501950
// No additional options for this command
19511951
},
19521952
async () => {
1953-
await cliContext.instrumentPostHogEvent({
1953+
cliContext.instrumentPostHogEvent({
19541954
command: "fern docs md check"
19551955
});
19561956

@@ -2006,7 +2006,7 @@ function addGenerateJsonschemaCommand(cli: Argv<GlobalCliOptions>, cliContext: C
20062006
description: "The type to generate JSON Schema for (e.g. 'MySchema' or 'mypackage.MySchema')"
20072007
}),
20082008
async (argv) => {
2009-
await cliContext.instrumentPostHogEvent({
2009+
cliContext.instrumentPostHogEvent({
20102010
command: "fern jsonschema",
20112011
properties: {
20122012
output: argv.output
@@ -2036,7 +2036,7 @@ function addWriteDocsDefinitionCommand(cli: Argv<GlobalCliOptions>, cliContext:
20362036
demandOption: true
20372037
}),
20382038
async (argv) => {
2039-
await cliContext.instrumentPostHogEvent({
2039+
cliContext.instrumentPostHogEvent({
20402040
command: "fern write-docs-definition",
20412041
properties: {
20422042
outputPath: argv.outputPath
@@ -2067,7 +2067,7 @@ function addWriteTranslationCommand(cli: Argv<GlobalCliOptions>, cliContext: Cli
20672067
description: "Return content as-is without calling the translation service"
20682068
}),
20692069
async (argv) => {
2070-
await cliContext.instrumentPostHogEvent({
2070+
cliContext.instrumentPostHogEvent({
20712071
command: "fern write-translation"
20722072
});
20732073

@@ -2104,7 +2104,7 @@ function addExportCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
21042104
default: 2
21052105
}),
21062106
async (argv) => {
2107-
await cliContext.instrumentPostHogEvent({
2107+
cliContext.instrumentPostHogEvent({
21082108
command: "fern export",
21092109
properties: {
21102110
outputPath: argv.outputPath
@@ -2148,7 +2148,7 @@ function addEnrichCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
21482148
demandOption: true
21492149
}),
21502150
async (argv) => {
2151-
await cliContext.instrumentPostHogEvent({
2151+
cliContext.instrumentPostHogEvent({
21522152
command: "fern api enrich"
21532153
});
21542154
const openapiPath = resolve(cwd(), argv.openapi as string);
@@ -2470,7 +2470,7 @@ function addSdkPreviewCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContex
24702470
"Cannot be combined with --local."
24712471
}),
24722472
async (argv) => {
2473-
await cliContext.instrumentPostHogEvent({
2473+
cliContext.instrumentPostHogEvent({
24742474
command: "fern sdk preview"
24752475
});
24762476
const generatorFilter =
@@ -2632,7 +2632,7 @@ function addReplayInitCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContex
26322632
description: "Overwrite existing lockfile if Replay is already initialized"
26332633
}),
26342634
async (argv) => {
2635-
await cliContext.instrumentPostHogEvent({
2635+
cliContext.instrumentPostHogEvent({
26362636
command: "fern replay init"
26372637
});
26382638

@@ -2754,7 +2754,7 @@ function addReplayResolveCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCon
27542754
description: "Skip checking for remaining conflict markers before committing"
27552755
}),
27562756
async (argv) => {
2757-
await cliContext.instrumentPostHogEvent({
2757+
cliContext.instrumentPostHogEvent({
27582758
command: "fern replay resolve"
27592759
});
27602760

0 commit comments

Comments
 (0)