From 3f7451d2b25168ca72f102b97a63ec5c36ce0d72 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Wed, 25 Jan 2023 11:23:29 +0800 Subject: [PATCH 1/3] CLI: Fix global flag handling --- code/lib/cli/src/generate.ts | 62 +++++++++++++++--------------------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/code/lib/cli/src/generate.ts b/code/lib/cli/src/generate.ts index 57d351769ef2..02fc464e3f86 100644 --- a/code/lib/cli/src/generate.ts +++ b/code/lib/cli/src/generate.ts @@ -24,18 +24,19 @@ import { parseList, getEnvConfig } from './utils'; const pkg = readUpSync({ cwd: __dirname }).packageJson; const consoleLogger = console; -program - .option( - '--disable-telemetry', - 'disable sending telemetry data', - // default value is false, but if the user sets STORYBOOK_DISABLE_TELEMETRY, it can be true - process.env.STORYBOOK_DISABLE_TELEMETRY && process.env.STORYBOOK_DISABLE_TELEMETRY !== 'false' - ) - .option('--debug', 'Get more logs in debug mode', false) - .option('--enable-crash-reports', 'enable sending crash reports to telemetry data'); +const command = (name: string) => + program + .command(name) + .option( + '--disable-telemetry', + 'disable sending telemetry data', + // default value is false, but if the user sets STORYBOOK_DISABLE_TELEMETRY, it can be true + process.env.STORYBOOK_DISABLE_TELEMETRY && process.env.STORYBOOK_DISABLE_TELEMETRY !== 'false' + ) + .option('--debug', 'Get more logs in debug mode', false) + .option('--enable-crash-reports', 'enable sending crash reports to telemetry data'); -program - .command('init') +command('init') .description('Initialize Storybook into your project.') .option('-f --force', 'Force add Storybook') .option('-s --skip-install', 'Skip installing deps') @@ -47,15 +48,14 @@ program .option('-y --yes', 'Answer yes to all prompts') .option('-b --builder ', 'Builder library') .option('-l --linkable', 'Prepare installation for link (contributor helper)') - .action((options: CommandOptions) => + .action((options: CommandOptions) => { initiate(options, pkg).catch((err) => { logger.error(err); process.exit(1); - }) - ); + }); + }); -program - .command('add ') +command('add ') .description('Add an addon to your Storybook') .option( '--package-manager ', @@ -65,13 +65,11 @@ program .option('-s --skip-postinstall', 'Skip package specific postinstall config modifications') .action((addonName: string, options: any) => add(addonName, options)); -program - .command('babelrc') +command('babelrc') .description('generate the default storybook babel config into your current working directory') .action(() => generateStorybookBabelConfigInCWD()); -program - .command('upgrade') +command('upgrade') .description('Upgrade your Storybook packages to the latest') .option( '--package-manager ', @@ -85,8 +83,7 @@ program .option('-s --skip-check', 'Skip postinstall version and automigration checks') .action((options: UpgradeOptions) => upgrade(options)); -program - .command('info') +command('info') .description('Prints debugging information about the local environment') .action(() => { consoleLogger.log(chalk.bold('\nEnvironment Info:')); @@ -101,8 +98,7 @@ program .then(consoleLogger.log); }); -program - .command('migrate [migration]') +command('migrate [migration]') .description('Run a Storybook codemod migration on your source files') .option('-l --list', 'List available migrations') .option('-g --glob ', 'Glob for files upon which to apply the migration', '**/*.js') @@ -130,8 +126,7 @@ program }); }); -program - .command('extract [location] [output]') +command('extract [location] [output]') .description('extract stories.json from a built version') .action((location = 'storybook-static', output = path.join(location, 'stories.json')) => extract(location, output).catch((e) => { @@ -140,8 +135,7 @@ program }) ); -program - .command('sandbox [filterValue]') +command('sandbox [filterValue]') .alias('repro') // for retrocompatibility purposes .description('Create a sandbox from a set of possible templates') .option('-o --output ', 'Define an output directory') @@ -154,8 +148,7 @@ program }) ); -program - .command('link ') +command('link ') .description('Pull down a repro from a URL (or a local directory), link it, and run storybook') .option('--local', 'Link a local directory already in your file system') .option('--no-start', 'Start the storybook', true) @@ -166,8 +159,7 @@ program }) ); -program - .command('automigrate [fixId]') +command('automigrate [fixId]') .description('Check storybook for known problems or migrations and apply fixes') .option('-y --yes', 'Skip prompting the user') .option('-n --dry-run', 'Only check for fixes, do not actually run them') @@ -181,8 +173,7 @@ program }); }); -program - .command('dev') +command('dev') .option('-p, --port ', 'Port to run Storybook', (str) => parseInt(str, 10)) .option('-h, --host ', 'Host to run Storybook') .option('-s, --static-dir ', 'Directory where to load static files from', parseList) @@ -239,8 +230,7 @@ program dev({ ...options, packageJson: pkg }); }); -program - .command('build') +command('build') .option('-s, --static-dir ', 'Directory where to load static files from', parseList) .option('-o, --output-dir ', 'Directory where to store built files') .option('-c, --config-dir ', 'Directory where to load Storybook configurations from') From 4efae5ad7147816ea7fbd5747d09f4a467bc34e2 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Wed, 25 Jan 2023 11:31:06 +0800 Subject: [PATCH 2/3] Improve CLI options types --- code/lib/cli/src/generators/types.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/code/lib/cli/src/generators/types.ts b/code/lib/cli/src/generators/types.ts index 766aa8e50eac..c9b3b309482a 100644 --- a/code/lib/cli/src/generators/types.ts +++ b/code/lib/cli/src/generators/types.ts @@ -52,4 +52,6 @@ export type CommandOptions = { linkable?: boolean; commonJs?: boolean; disableTelemetry?: boolean; + enableCrashReports?: boolean; + debug?: boolean; }; From 214c6ae3ac43696f6c8d3ad9bf85d4a78ab28b13 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Wed, 25 Jan 2023 18:48:18 +0800 Subject: [PATCH 3/3] Add extra error sanitization handling --- code/lib/telemetry/src/sanitize.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/code/lib/telemetry/src/sanitize.ts b/code/lib/telemetry/src/sanitize.ts index f3dc5ebde057..4c68ed50db94 100644 --- a/code/lib/telemetry/src/sanitize.ts +++ b/code/lib/telemetry/src/sanitize.ts @@ -33,11 +33,15 @@ export function cleanPaths(str: string, separator: string = sep): string { // Takes an Error and returns a sanitized JSON String export function sanitizeError(error: Error, pathSeparator: string = sep) { - // Hack because Node - error = JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error))); + try { + // Hack because Node + error = JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error))); - // Removes all user paths - const errorString = cleanPaths(JSON.stringify(error), pathSeparator); + // Removes all user paths + const errorString = cleanPaths(JSON.stringify(error), pathSeparator); - return JSON.parse(errorString); + return JSON.parse(errorString); + } catch (err: any) { + return `Sanitization error: ${err?.message}`; + } }