Skip to content

Commit

Permalink
Print errors in withTelemetry, before prompting
Browse files Browse the repository at this point in the history
Also make exiting more consistent across commands. For #21373
  • Loading branch information
tmeasday committed Mar 6, 2023
1 parent 0b04540 commit 7de718e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 64 deletions.
30 changes: 12 additions & 18 deletions code/lib/cli/src/build.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { sync as readUpSync } from 'read-pkg-up';
import { logger } from '@storybook/node-logger';
import { buildStaticStandalone, withTelemetry } from '@storybook/core-server';
import { cache } from '@storybook/core-common';

export const build = async (cliOptions: any) => {
try {
const options = {
...cliOptions,
configDir: cliOptions.configDir || './.storybook',
outputDir: cliOptions.outputDir || './storybook-static',
ignorePreview: !!cliOptions.previewUrl && !cliOptions.forceBuildPreview,
configType: 'PRODUCTION',
cache,
packageJson: readUpSync({ cwd: __dirname }).packageJson,
};
await withTelemetry('build', { cliOptions, presetOptions: options }, () =>
buildStaticStandalone(options)
);
} catch (err) {
logger.error(err);
process.exit(1);
}
const options = {
...cliOptions,
configDir: cliOptions.configDir || './.storybook',
outputDir: cliOptions.outputDir || './storybook-static',
ignorePreview: !!cliOptions.previewUrl && !cliOptions.forceBuildPreview,
configType: 'PRODUCTION',
cache,
packageJson: readUpSync({ cwd: __dirname }).packageJson,
};
await withTelemetry('build', { cliOptions, presetOptions: options }, () =>
buildStaticStandalone(options)
);
};
72 changes: 35 additions & 37 deletions code/lib/cli/src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,49 @@ import { logger, instance as npmLog } from '@storybook/node-logger';
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';
function printError(error: any) {
// this is a weird bugfix, somehow 'node-pre-gyp' is polluting the npmLog header
npmLog.heading = '';

try {
const options = {
...cliOptions,
configDir: cliOptions.configDir || './.storybook',
configType: 'DEVELOPMENT',
ignorePreview: !!cliOptions.previewUrl && !cliOptions.forceBuildPreview,
cache,
packageJson: readUpSync({ cwd: __dirname }).packageJson,
};
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 = '';

if (error instanceof Error) {
if ((error as any).error) {
logger.error((error as any).error);
} else if ((error as any).stats && (error as any).stats.compilation.errors) {
(error as any).stats.compilation.errors.forEach((e: any) => logger.plain(e));
} else {
logger.error(error as any);
}
} else if (error.compilation?.errors) {
error.compilation.errors.forEach((e: any) => logger.plain(e));
if (error instanceof Error) {
if ((error as any).error) {
logger.error((error as any).error);
} else if ((error as any).stats && (error as any).stats.compilation.errors) {
(error as any).stats.compilation.errors.forEach((e: any) => logger.plain(e));
} else {
logger.error(error as any);
}
} else if (error.compilation?.errors) {
error.compilation.errors.forEach((e: any) => logger.plain(e));
}

logger.line();
logger.warn(
error.close
? dedent`
logger.line();
logger.warn(
error.close
? dedent`
FATAL broken build!, will close the process,
Fix the error below and restart storybook.
`
: dedent`
: dedent`
Broken build, fix the error above.
You may need to refresh the browser.
`
);
logger.line();
);
logger.line();
}

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

const options = {
...cliOptions,
configDir: cliOptions.configDir || './.storybook',
configType: 'DEVELOPMENT',
ignorePreview: !!cliOptions.previewUrl && !cliOptions.forceBuildPreview,
cache,
packageJson: readUpSync({ cwd: __dirname }).packageJson,
};
await withTelemetry('dev', { cliOptions, presetOptions: options, printError }, () =>
buildDevStandalone(options)
);
};
15 changes: 6 additions & 9 deletions code/lib/cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ command('init')
.option('-b --builder <webpack5 | vite>', 'Builder library')
.option('-l --linkable', 'Prepare installation for link (contributor helper)')
.action((options: CommandOptions) => {
initiate(options, pkg).catch((err) => {
logger.error(err);
process.exit(1);
});
initiate(options, pkg).catch(() => process.exit(1));
});

command('add <addon>')
Expand Down Expand Up @@ -82,7 +79,7 @@ command('upgrade')
.option('-p --prerelease', 'Upgrade to the pre-release packages')
.option('-s --skip-check', 'Skip postinstall version and automigration checks')
.option('-c, --config-dir <dir-name>', 'Directory where to load Storybook configurations from')
.action((options: UpgradeOptions) => upgrade(options));
.action(async (options: UpgradeOptions) => upgrade(options).catch(() => process.exit(1)));

command('info')
.description('Prints debugging information about the local environment')
Expand Down Expand Up @@ -215,7 +212,7 @@ command('dev')
)
.option('--force-build-preview', 'Build the preview iframe even if you are using --preview-url')
.option('--docs', 'Build a documentation-only site using addon-docs')
.action((options) => {
.action(async (options) => {
logger.setLevel(program.loglevel);
consoleLogger.log(chalk.bold(`${pkg.name} v${pkg.version}`) + chalk.reset('\n'));

Expand All @@ -233,7 +230,7 @@ command('dev')
program.port = parseInt(program.port, 10);
}

dev({ ...options, packageJson: pkg });
await dev({ ...options, packageJson: pkg }).catch(() => process.exit(1));
});

command('build')
Expand All @@ -250,7 +247,7 @@ command('build')
)
.option('--force-build-preview', 'Build the preview iframe even if you are using --preview-url')
.option('--docs', 'Build a documentation-only site using addon-docs')
.action((options) => {
.action(async (options) => {
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
logger.setLevel(program.loglevel);
consoleLogger.log(chalk.bold(`${pkg.name} v${pkg.version}\n`));
Expand All @@ -263,7 +260,7 @@ command('build')
configDir: 'SBCONFIG_CONFIG_DIR',
});

build({ ...options, packageJson: pkg });
await build({ ...options, packageJson: pkg }).catch(() => process.exit(1));
});

program.on('command:*', ([invalidCmd]) => {
Expand Down
5 changes: 5 additions & 0 deletions code/lib/core-server/src/withTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import type { CLIOptions, CoreConfig } from '@storybook/types';
import { loadAllPresets, cache } from '@storybook/core-common';
import { telemetry, getPrecedingUpgrade, oneWayHash } from '@storybook/telemetry';
import type { EventType } from '@storybook/telemetry';
import { logger } from '@storybook/node-logger';

type TelemetryOptions = {
cliOptions: CLIOptions;
presetOptions?: Parameters<typeof loadAllPresets>[0];
printError?: (err: any) => void;
};

const promptCrashReports = async () => {
Expand Down Expand Up @@ -99,6 +101,9 @@ export async function withTelemetry(
try {
await run();
} catch (error) {
const { printError = logger.error } = options;
printError(error);

await sendTelemetryError(error, eventType, options);
throw error;
}
Expand Down

0 comments on commit 7de718e

Please sign in to comment.