Don't send boot event when cliOptions.disableTelemetry is passed

This commit is contained in:
Tom Coleman 2022-12-08 10:22:02 +11:00
parent d672182dc8
commit 1dc35cd8f6
4 changed files with 32 additions and 9 deletions

View File

@ -15,7 +15,9 @@ export const build = async (cliOptions: any) => {
cache,
packageJson: readUpSync({ cwd: __dirname }).packageJson,
};
await withTelemetry('build', { presetOptions: options }, () => buildStaticStandalone(options));
await withTelemetry('build', { cliOptions, presetOptions: options }, () =>
buildStaticStandalone(options)
);
} catch (err) {
logger.error(err);
process.exit(1);

View File

@ -17,7 +17,9 @@ export const dev = async (cliOptions: any) => {
cache,
packageJson: readUpSync({ cwd: __dirname }).packageJson,
};
await withTelemetry('dev', { presetOptions: options }, () => buildDevStandalone(options));
await withTelemetry('dev', { cliOptions, presetOptions: options }, () =>
buildDevStandalone(options)
);
} catch (error) {
// this is a weird bugfix, somehow 'node-pre-gyp' is polluting the npmLog header
npmLog.heading = '';

View File

@ -10,15 +10,25 @@ jest.mock('prompts');
jest.mock('@storybook/core-common');
jest.mock('@storybook/telemetry');
const cliOptions = {};
it('works in happy path', async () => {
const run = jest.fn();
await withTelemetry('dev', {}, run);
await withTelemetry('dev', { cliOptions }, run);
expect(telemetry).toHaveBeenCalledTimes(1);
expect(telemetry).toHaveBeenCalledWith('boot', { eventType: 'dev' }, { stripMetadata: true });
});
it('does not send boot when cli option is passed', async () => {
const run = jest.fn();
await withTelemetry('dev', { cliOptions: { disableTelemetry: true } }, run);
expect(telemetry).toHaveBeenCalledTimes(0);
});
describe('when command fails', () => {
const error = new Error('An Error!');
const run = jest.fn(async () => {
@ -26,13 +36,21 @@ describe('when command fails', () => {
});
it('sends boot message', async () => {
await expect(async () => withTelemetry('dev', {}, run)).rejects.toThrow(error);
await expect(async () => withTelemetry('dev', { cliOptions }, run)).rejects.toThrow(error);
expect(telemetry).toHaveBeenCalledWith('boot', { eventType: 'dev' }, { stripMetadata: true });
});
it('does not send boot when cli option is passed', async () => {
await expect(async () =>
withTelemetry('dev', { cliOptions: { disableTelemetry: true } }, run)
).rejects.toThrow(error);
expect(telemetry).toHaveBeenCalledTimes(0);
});
it('sends error message when no options are passed', async () => {
await expect(async () => withTelemetry('dev', {}, run)).rejects.toThrow(error);
await expect(async () => withTelemetry('dev', { cliOptions }, run)).rejects.toThrow(error);
expect(telemetry).toHaveBeenCalledTimes(2);
expect(telemetry).toHaveBeenCalledWith(

View File

@ -5,7 +5,7 @@ import { telemetry, getPrecedingUpgrade } from '@storybook/telemetry';
import type { EventType } from '@storybook/telemetry';
type TelemetryOptions = {
cliOptions?: CLIOptions;
cliOptions: CLIOptions;
presetOptions?: Parameters<typeof loadAllPresets>[0];
};
@ -29,7 +29,7 @@ const promptCrashReports = async () => {
type ErrorLevel = 'none' | 'error' | 'full';
async function getErrorLevel({ cliOptions, presetOptions }: TelemetryOptions): Promise<ErrorLevel> {
if (cliOptions?.disableTelemetry) return 'none';
if (cliOptions.disableTelemetry) return 'none';
// If we are running init or similar, we just have to go with true here
if (!presetOptions) return 'full';
@ -63,7 +63,8 @@ export async function withTelemetry(
options: TelemetryOptions,
run: () => Promise<void>
) {
telemetry('boot', { eventType }, { stripMetadata: true });
if (!options.cliOptions.disableTelemetry)
telemetry('boot', { eventType }, { stripMetadata: true });
try {
await run();
@ -78,7 +79,7 @@ export async function withTelemetry(
{ eventType, precedingUpgrade, error: errorLevel === 'full' ? error : undefined },
{
immediate: true,
configDir: options.cliOptions?.configDir || options.presetOptions?.configDir,
configDir: options.cliOptions.configDir || options.presetOptions?.configDir,
enableCrashReports: errorLevel === 'full',
}
);