Skip to content

Commit 7880617

Browse files
committed
refactor: replace is_ci/json_mode with interactive flag
Single boolean: isInteractive() && !opts.json Covers TTY, CI, piped, and --json/--quiet cases.
1 parent ccd29da commit 7880617

2 files changed

Lines changed: 69 additions & 7 deletions

File tree

src/lib/telemetry.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
22
import { join } from 'node:path';
33
import { getConfigDir } from './config';
4+
import { isInteractive } from './tty';
45
import { detectInstallMethodName } from './update-check';
56
import { VERSION } from './version';
67

@@ -68,11 +69,7 @@ export function trackCommand(command: string, opts: { json?: boolean }): void {
6869
os: process.platform,
6970
arch: process.arch,
7071
node_version: process.version,
71-
is_ci:
72-
process.env.CI === 'true' ||
73-
process.env.CI === '1' ||
74-
!!process.env.GITHUB_ACTIONS,
75-
json_mode: !!opts.json,
72+
interactive: isInteractive() && !opts.json,
7673
install_method: detectInstallMethodName(),
7774
},
7875
};

tests/lib/telemetry.test.ts

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ describe('trackCommand', () => {
107107
}
108108
});
109109

110+
function parsePayload() {
111+
const [, options] = fetchSpy.mock.calls[0] as [string, RequestInit];
112+
return JSON.parse(options.body as string);
113+
}
114+
110115
test('sends correct payload to PostHog endpoint', () => {
111116
trackCommand('emails send', { json: true });
112117

@@ -115,10 +120,9 @@ describe('trackCommand', () => {
115120
expect(url).toBe('https://us.i.posthog.com/capture/');
116121
expect(options.method).toBe('POST');
117122

118-
const body = JSON.parse(options.body as string);
123+
const body = parsePayload();
119124
expect(body.event).toBe('cli.used');
120125
expect(body.properties.command).toBe('emails send');
121-
expect(body.properties.json_mode).toBe(true);
122126
expect(body.properties.os).toBe(process.platform);
123127
expect(body.properties.arch).toBe(process.arch);
124128
expect(body.properties.node_version).toBe(process.version);
@@ -128,6 +132,67 @@ describe('trackCommand', () => {
128132
);
129133
});
130134

135+
test('interactive is false when --json flag is set', () => {
136+
Object.defineProperty(process.stdin, 'isTTY', {
137+
value: true,
138+
writable: true,
139+
});
140+
Object.defineProperty(process.stdout, 'isTTY', {
141+
value: true,
142+
writable: true,
143+
});
144+
delete process.env.CI;
145+
delete process.env.GITHUB_ACTIONS;
146+
147+
trackCommand('emails send', { json: true });
148+
expect(parsePayload().properties.interactive).toBe(false);
149+
});
150+
151+
test('interactive is false when not a TTY', () => {
152+
Object.defineProperty(process.stdin, 'isTTY', {
153+
value: undefined,
154+
writable: true,
155+
});
156+
Object.defineProperty(process.stdout, 'isTTY', {
157+
value: undefined,
158+
writable: true,
159+
});
160+
161+
trackCommand('emails list', {});
162+
expect(parsePayload().properties.interactive).toBe(false);
163+
});
164+
165+
test('interactive is false in CI even with TTY', () => {
166+
Object.defineProperty(process.stdin, 'isTTY', {
167+
value: true,
168+
writable: true,
169+
});
170+
Object.defineProperty(process.stdout, 'isTTY', {
171+
value: true,
172+
writable: true,
173+
});
174+
process.env.CI = 'true';
175+
176+
trackCommand('emails list', {});
177+
expect(parsePayload().properties.interactive).toBe(false);
178+
});
179+
180+
test('interactive is true with TTY and no --json', () => {
181+
Object.defineProperty(process.stdin, 'isTTY', {
182+
value: true,
183+
writable: true,
184+
});
185+
Object.defineProperty(process.stdout, 'isTTY', {
186+
value: true,
187+
writable: true,
188+
});
189+
delete process.env.CI;
190+
delete process.env.GITHUB_ACTIONS;
191+
192+
trackCommand('emails list', {});
193+
expect(parsePayload().properties.interactive).toBe(true);
194+
});
195+
131196
test('does nothing when disabled', () => {
132197
process.env.RESEND_TELEMETRY_DISABLED = '1';
133198
trackCommand('emails send', {});

0 commit comments

Comments
 (0)