Skip to content

Commit

Permalink
Merge pull request #19566 from storybookjs/tom/sb-832-restore-crash-r…
Browse files Browse the repository at this point in the history
…eporting-in-v7-and

Telemetry: Use a wrapper around all CLI commands to send boot and error events
  • Loading branch information
tmeasday authored Oct 24, 2022
2 parents ff46c40 + 090fb35 commit 26324ef
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 75 deletions.
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 type { 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 @@ -256,7 +258,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 @@ -266,7 +268,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 });
}

// Update notify code.
Expand Down Expand Up @@ -339,3 +341,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

0 comments on commit 26324ef

Please sign in to comment.