Skip to content

Commit 42bf390

Browse files
committed
refactor(cli): make instrumentPostHogEvent synchronous
Made-with: Cursor
1 parent 452b99d commit 42bf390

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

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

9999
const exit = async () => {
100100
await cliContext.exit();
@@ -134,7 +134,7 @@ async function runCli() {
134134
});
135135
}
136136
} catch (error) {
137-
await cliContext.instrumentPostHogEvent({
137+
cliContext.instrumentPostHogEvent({
138138
command: process.argv.join(" "),
139139
properties: {
140140
failed: true,
@@ -535,7 +535,7 @@ function addSdkDiffCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext)
535535
description: "Output result as JSON"
536536
}),
537537
async (argv) => {
538-
await cliContext.instrumentPostHogEvent({
538+
cliContext.instrumentPostHogEvent({
539539
command: "fern sdk-diff"
540540
});
541541

@@ -1350,7 +1350,7 @@ function addUpdateApiSpecCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCon
13501350
default: 2
13511351
}),
13521352
async (argv) => {
1353-
await cliContext.instrumentPostHogEvent({
1353+
cliContext.instrumentPostHogEvent({
13541354
command: "fern api update"
13551355
});
13561356
await updateApiSpec({
@@ -1381,7 +1381,7 @@ function addSelfUpdateCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContex
13811381
default: false
13821382
}),
13831383
async (argv) => {
1384-
await cliContext.instrumentPostHogEvent({
1384+
cliContext.instrumentPostHogEvent({
13851385
command: "fern self-update"
13861386
});
13871387
await selfUpdate({
@@ -1410,7 +1410,7 @@ function addLoginCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14101410
}),
14111411
async (argv) => {
14121412
await cliContext.runTask(async (context) => {
1413-
await cliContext.instrumentPostHogEvent({
1413+
cliContext.instrumentPostHogEvent({
14141414
command: "fern login"
14151415
});
14161416
await login(context, { useDeviceCodeFlow: argv.deviceCode, email: argv.email });
@@ -1426,7 +1426,7 @@ function addLogoutCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14261426
(yargs) => yargs,
14271427
async () => {
14281428
await cliContext.runTask(async (context) => {
1429-
await cliContext.instrumentPostHogEvent({
1429+
cliContext.instrumentPostHogEvent({
14301430
command: "fern logout"
14311431
});
14321432
await logout(context);
@@ -1451,7 +1451,7 @@ function addFormatCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14511451
description: "Only run the command on the provided API"
14521452
}),
14531453
async (argv) => {
1454-
await cliContext.instrumentPostHogEvent({
1454+
cliContext.instrumentPostHogEvent({
14551455
command: "fern format"
14561456
});
14571457
await formatWorkspaces({
@@ -1485,7 +1485,7 @@ function addTestCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
14851485
description: "Run the tests configured to a specific language"
14861486
}),
14871487
async (argv) => {
1488-
await cliContext.instrumentPostHogEvent({
1488+
cliContext.instrumentPostHogEvent({
14891489
command: "fern test"
14901490
});
14911491
await testOutput({
@@ -1518,7 +1518,7 @@ function addMockCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
15181518
description: "The API to mock."
15191519
}),
15201520
async (argv) => {
1521-
await cliContext.instrumentPostHogEvent({
1521+
cliContext.instrumentPostHogEvent({
15221522
command: "fern mock"
15231523
});
15241524
await mockServer({
@@ -1563,7 +1563,7 @@ function addOverridesCompareCommand(cli: Argv<GlobalCliOptions>, cliContext: Cli
15631563
description: "Path to write the overrides file (defaults to <original>-overrides.yml)"
15641564
}),
15651565
async (argv) => {
1566-
await cliContext.instrumentPostHogEvent({
1566+
cliContext.instrumentPostHogEvent({
15671567
command: "fern overrides compare"
15681568
});
15691569
const originalPath = resolve(cwd(), argv.original as string);
@@ -1596,7 +1596,7 @@ function addOverridesWriteCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCo
15961596
})
15971597
],
15981598
async (argv) => {
1599-
await cliContext.instrumentPostHogEvent({
1599+
cliContext.instrumentPostHogEvent({
16001600
command: "fern overrides write"
16011601
});
16021602
await writeOverridesForWorkspaces({
@@ -1632,7 +1632,7 @@ function addWriteOverridesCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCo
16321632
],
16331633
async (argv) => {
16341634
cliContext.logger.warn("The 'write-overrides' command is deprecated. Use 'fern overrides write' instead.");
1635-
await cliContext.instrumentPostHogEvent({
1635+
cliContext.instrumentPostHogEvent({
16361636
command: "fern write-overrides"
16371637
});
16381638
await writeOverridesForWorkspaces({
@@ -1667,7 +1667,7 @@ function addWriteDefinitionCommand(cli: Argv<GlobalCliOptions>, cliContext: CliC
16671667
}),
16681668
async (argv) => {
16691669
const preserveSchemaIds = argv.preserveSchemas != null;
1670-
await cliContext.instrumentPostHogEvent({
1670+
cliContext.instrumentPostHogEvent({
16711671
command: "fern write-definition"
16721672
});
16731673
await writeDefinitionForWorkspaces({
@@ -1716,7 +1716,7 @@ function addDocsMdGenerateCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCo
17161716
description: "Name of a specific library defined in docs.yml to generate docs for"
17171717
}),
17181718
async (argv) => {
1719-
await cliContext.instrumentPostHogEvent({
1719+
cliContext.instrumentPostHogEvent({
17201720
command: "fern docs md generate"
17211721
});
17221722

@@ -1757,7 +1757,7 @@ function addDocsDiffCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext)
17571757
description: "Output directory for diff images"
17581758
}),
17591759
async (argv) => {
1760-
await cliContext.instrumentPostHogEvent({
1760+
cliContext.instrumentPostHogEvent({
17611761
command: "fern docs diff"
17621762
});
17631763

@@ -1802,7 +1802,7 @@ function addDocsPreviewListCommand(cli: Argv<GlobalCliOptions>, cliContext: CliC
18021802
description: "Page number for pagination (starts at 1)"
18031803
}),
18041804
async (argv) => {
1805-
await cliContext.instrumentPostHogEvent({
1805+
cliContext.instrumentPostHogEvent({
18061806
command: "fern docs preview list"
18071807
});
18081808
await listDocsPreview({
@@ -1826,7 +1826,7 @@ function addDocsPreviewDeleteCommand(cli: Argv<GlobalCliOptions>, cliContext: Cl
18261826
demandOption: true
18271827
}),
18281828
async (argv) => {
1829-
await cliContext.instrumentPostHogEvent({
1829+
cliContext.instrumentPostHogEvent({
18301830
command: "fern docs preview delete"
18311831
});
18321832
await deleteDocsPreview({
@@ -1945,7 +1945,7 @@ function addDocsMdCheckCommand(cli: Argv<GlobalCliOptions>, cliContext: CliConte
19451945
// No additional options for this command
19461946
},
19471947
async () => {
1948-
await cliContext.instrumentPostHogEvent({
1948+
cliContext.instrumentPostHogEvent({
19491949
command: "fern docs md check"
19501950
});
19511951

@@ -2001,7 +2001,7 @@ function addGenerateJsonschemaCommand(cli: Argv<GlobalCliOptions>, cliContext: C
20012001
description: "The type to generate JSON Schema for (e.g. 'MySchema' or 'mypackage.MySchema')"
20022002
}),
20032003
async (argv) => {
2004-
await cliContext.instrumentPostHogEvent({
2004+
cliContext.instrumentPostHogEvent({
20052005
command: "fern jsonschema",
20062006
properties: {
20072007
output: argv.output
@@ -2031,7 +2031,7 @@ function addWriteDocsDefinitionCommand(cli: Argv<GlobalCliOptions>, cliContext:
20312031
demandOption: true
20322032
}),
20332033
async (argv) => {
2034-
await cliContext.instrumentPostHogEvent({
2034+
cliContext.instrumentPostHogEvent({
20352035
command: "fern write-docs-definition",
20362036
properties: {
20372037
outputPath: argv.outputPath
@@ -2062,7 +2062,7 @@ function addWriteTranslationCommand(cli: Argv<GlobalCliOptions>, cliContext: Cli
20622062
description: "Return content as-is without calling the translation service"
20632063
}),
20642064
async (argv) => {
2065-
await cliContext.instrumentPostHogEvent({
2065+
cliContext.instrumentPostHogEvent({
20662066
command: "fern write-translation"
20672067
});
20682068

@@ -2099,7 +2099,7 @@ function addExportCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
20992099
default: 2
21002100
}),
21012101
async (argv) => {
2102-
await cliContext.instrumentPostHogEvent({
2102+
cliContext.instrumentPostHogEvent({
21032103
command: "fern export",
21042104
properties: {
21052105
outputPath: argv.outputPath
@@ -2143,7 +2143,7 @@ function addEnrichCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
21432143
demandOption: true
21442144
}),
21452145
async (argv) => {
2146-
await cliContext.instrumentPostHogEvent({
2146+
cliContext.instrumentPostHogEvent({
21472147
command: "fern api enrich"
21482148
});
21492149
const openapiPath = resolve(cwd(), argv.openapi as string);
@@ -2197,7 +2197,7 @@ function addSdkPreviewCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContex
21972197
"Omit to publish to the default preview registry and write to a temp directory."
21982198
}),
21992199
async (argv) => {
2200-
await cliContext.instrumentPostHogEvent({
2200+
cliContext.instrumentPostHogEvent({
22012201
command: "fern sdk preview"
22022202
});
22032203
const generatorFilter =
@@ -2357,7 +2357,7 @@ function addReplayInitCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContex
23572357
description: "Overwrite existing lockfile if Replay is already initialized"
23582358
}),
23592359
async (argv) => {
2360-
await cliContext.instrumentPostHogEvent({
2360+
cliContext.instrumentPostHogEvent({
23612361
command: "fern replay init"
23622362
});
23632363

@@ -2479,7 +2479,7 @@ function addReplayResolveCommand(cli: Argv<GlobalCliOptions>, cliContext: CliCon
24792479
description: "Skip checking for remaining conflict markers before committing"
24802480
}),
24812481
async (argv) => {
2482-
await cliContext.instrumentPostHogEvent({
2482+
cliContext.instrumentPostHogEvent({
24832483
command: "fern replay resolve"
24842484
});
24852485

0 commit comments

Comments
 (0)