Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telemetry: Use a wrapper around all CLI commands to send boot and error events #19566

Merged
merged 5 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions code/lib/cli/src/build.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { sync as readUpSync } from 'read-pkg-up';
import { logger } from '@storybook/node-logger';
import { buildStaticStandalone } from '@storybook/core-server';
import { buildStaticStandalone, withTelemetry } from '@storybook/core-server';
import { cache } from '@storybook/core-common';

export const build = async (cliOptions: any) => {
try {
await buildStaticStandalone({
const options = {
...cliOptions,
configDir: cliOptions.configDir || './.storybook',
outputDir: cliOptions.outputDir || './storybook-static',
Expand All @@ -14,9 +14,10 @@ export const build = async (cliOptions: any) => {
configType: 'PRODUCTION',
cache,
packageJson: readUpSync({ cwd: __dirname }).packageJson,
});
} catch (e) {
logger.error(e);
};
await withTelemetry('build', { presetOptions: options }, () => buildStaticStandalone(options));
} catch (err) {
logger.error(err);
process.exit(1);
}
};
7 changes: 4 additions & 3 deletions code/lib/cli/src/dev.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { dedent } from 'ts-dedent';
import { sync as readUpSync } from 'read-pkg-up';
import { logger, instance as npmLog } from '@storybook/node-logger';
import { buildDevStandalone } from '@storybook/core-server';
import { buildDevStandalone, withTelemetry } from '@storybook/core-server';
import { cache } from '@storybook/core-common';

export const dev = async (cliOptions: any) => {
process.env.NODE_ENV = process.env.NODE_ENV || 'development';

try {
await buildDevStandalone({
const options = {
...cliOptions,
configDir: cliOptions.configDir || './.storybook',
configType: 'DEVELOPMENT',
ignorePreview: !!cliOptions.previewUrl && !cliOptions.forceBuildPreview,
docsMode: !!cliOptions.docs,
cache,
packageJson: readUpSync({ cwd: __dirname }).packageJson,
});
};
await withTelemetry('dev', { presetOptions: options }, () => buildDevStandalone(options));
} catch (error) {
// this is a weird bugfix, somehow 'node-pre-gyp' is polluting the npmLog header
npmLog.heading = '';
Expand Down
10 changes: 8 additions & 2 deletions code/lib/cli/src/initiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { UpdateNotifier, Package } from 'update-notifier';
import chalk from 'chalk';
import prompts from 'prompts';
import { telemetry } from '@storybook/telemetry';
import { withTelemetry } from '@storybook/core-server';

import { installableProjectTypes, ProjectType } from './project_types';
import { detect, isStorybookInstalled, detectLanguage, detectBuilder } from './detect';
import { commandLog, codeLog, paddedLog } from './helpers';
Expand Down Expand Up @@ -250,7 +252,7 @@ const projectTypeInquirer = async (
return Promise.resolve();
};

export async function initiate(options: CommandOptions, pkg: Package): Promise<void> {
async function doInitiate(options: CommandOptions, pkg: Package): Promise<void> {
const { useNpm, packageManager: pkgMgr } = options;
if (useNpm) {
useNpmWarning();
Expand All @@ -260,7 +262,7 @@ export async function initiate(options: CommandOptions, pkg: Package): Promise<v
logger.log(chalk.inverse(`\n ${welcomeMessage} \n`));

if (!options.disableTelemetry) {
telemetry('init');
telemetry('init', {}, { stripMetadata: true });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was failing trying to find the metadata. I'm not really sure what changed, but we don't need it / can't find it anyway.

}

// Update notify code.
Expand Down Expand Up @@ -332,3 +334,7 @@ export async function initiate(options: CommandOptions, pkg: Package): Promise<v
// Add a new line for the clear visibility.
logger.log();
}

export async function initiate(options: CommandOptions, pkg: Package): Promise<void> {
await withTelemetry('init', { cliOptions: options }, () => doInitiate(options, pkg));
}
8 changes: 7 additions & 1 deletion code/lib/cli/src/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { sync as spawnSync } from 'cross-spawn';
import { telemetry } from '@storybook/telemetry';
import semver from 'semver';
import { logger } from '@storybook/node-logger';
import { withTelemetry } from '@storybook/core-server';

import {
getPackageDetails,
JsPackageManagerFactory,
Expand Down Expand Up @@ -148,7 +150,7 @@ export interface UpgradeOptions {
disableTelemetry: boolean;
}

export const upgrade = async ({
export const doUpgrade = async ({
prerelease,
skipCheck,
useNpm,
Expand Down Expand Up @@ -188,3 +190,7 @@ export const upgrade = async ({
await automigrate({ dryRun, yes, useNpm, force: pkgMgr });
}
};

export async function upgrade(options: UpgradeOptions): Promise<void> {
await withTelemetry('upgrade', { cliOptions: options }, () => doUpgrade(options));
}
62 changes: 26 additions & 36 deletions code/lib/core-server/src/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,33 @@ export async function storybookDevServer(options: Options) {
// try get index generator, if failed, send telemetry without storyCount, then rethrow the error
let initializedStoryIndexGenerator: Promise<StoryIndexGenerator> = Promise.resolve(undefined);
if (features?.buildStoriesJson || features?.storyStoreV7) {
try {
const workingDir = process.cwd();
const directories = {
configDir: options.configDir,
workingDir,
};
const normalizedStories = normalizeStories(
await options.presets.apply('stories'),
directories
);
const storyIndexers = await options.presets.apply('storyIndexers', []);
const docsOptions = await options.presets.apply<DocsOptions>('docs', {});

const generator = new StoryIndexGenerator(normalizedStories, {
...directories,
storyIndexers,
docs: docsOptions,
workingDir,
storiesV2Compatibility: !features?.breakingChangesV7 && !features?.storyStoreV7,
storyStoreV7: features?.storyStoreV7,
});
const workingDir = process.cwd();
const directories = {
configDir: options.configDir,
workingDir,
};
const normalizedStories = normalizeStories(await options.presets.apply('stories'), directories);
const storyIndexers = await options.presets.apply('storyIndexers', []);
const docsOptions = await options.presets.apply<DocsOptions>('docs', {});

const generator = new StoryIndexGenerator(normalizedStories, {
...directories,
storyIndexers,
docs: docsOptions,
workingDir,
storiesV2Compatibility: !features?.breakingChangesV7 && !features?.storyStoreV7,
storyStoreV7: features?.storyStoreV7,
});

initializedStoryIndexGenerator = generator.initialize().then(() => generator);
initializedStoryIndexGenerator = generator.initialize().then(() => generator);

useStoriesJson({
router,
initializedStoryIndexGenerator,
normalizedStories,
serverChannel,
workingDir,
});
} catch (err) {
if (!core?.disableTelemetry) {
telemetry('start');
}
throw err;
}
useStoriesJson({
router,
initializedStoryIndexGenerator,
normalizedStories,
serverChannel,
workingDir,
});
}

if (!core?.disableTelemetry) {
Expand All @@ -89,7 +79,7 @@ export async function storybookDevServer(options: Options) {
},
}
: undefined;
telemetry('start', payload, { configDir: options.configDir });
telemetry('dev', payload, { configDir: options.configDir });
});
}

Expand Down
1 change: 1 addition & 0 deletions code/lib/core-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export {

export * from './build-static';
export * from './build-dev';
export * from './withTelemetry';
Loading