import program from 'commander'; import path from 'path'; import chalk from 'chalk'; import envinfo from 'envinfo'; import leven from 'leven'; import { sync as readUpSync } from 'read-pkg-up'; import { logger } from '@storybook/node-logger'; import type { CommandOptions } from './generators/types'; import { initiate } from './initiate'; import { add } from './add'; import { migrate } from './migrate'; import { extract } from './extract'; import { upgrade, type UpgradeOptions } from './upgrade'; import { repro } from './repro'; import { reproNext } from './repro-next'; import { link } from './link'; import { automigrate } from './automigrate'; import { generateStorybookBabelConfigInCWD } from './babel-config'; import { dev } from './dev'; import { build } from './build'; 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'); program .command('init') .description('Initialize Storybook into your project.') .option('-f --force', 'Force add Storybook') .option('-s --skip-install', 'Skip installing deps') .option('--package-manager ', 'Force package manager for installing deps') .option('-N --use-npm', 'Use npm to install deps (deprecated)') .option('--use-pnp', 'Enable pnp mode for Yarn 2+') .option('-p --parser ', 'jscodeshift parser') .option('-t --type ', 'Add Storybook for a specific project type') .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) => initiate(options, pkg).catch((err) => { logger.error(err); process.exit(1); }) ); program .command('add ') .description('Add an addon to your Storybook') .option( '--package-manager ', 'Force package manager for installing dependencies' ) .option('-N --use-npm', 'Use NPM to install dependencies (deprecated)') .option('-s --skip-postinstall', 'Skip package specific postinstall config modifications') .action((addonName: string, options: any) => add(addonName, options)); program .command('babelrc') .description('generate the default storybook babel config into your current working directory') .action(() => generateStorybookBabelConfigInCWD()); program .command('upgrade') .description('Upgrade your Storybook packages to the latest') .option( '--package-manager ', 'Force package manager for installing dependencies' ) .option('-N --use-npm', 'Use NPM to install dependencies (deprecated)') .option('-y --yes', 'Skip prompting the user') .option('-n --dry-run', 'Only check for upgrades, do not install') .option('-t --tag ', 'Upgrade to a certain npm dist-tag (e.g. next, prerelease)') .option('-p --prerelease', 'Upgrade to the pre-release packages') .option('-s --skip-check', 'Skip postinstall version and automigration checks') .action((options: UpgradeOptions) => upgrade(options)); program .command('info') .description('Prints debugging information about the local environment') .action(() => { consoleLogger.log(chalk.bold('\nEnvironment Info:')); envinfo .run({ System: ['OS', 'CPU'], Binaries: ['Node', 'Yarn', 'npm'], Browsers: ['Chrome', 'Edge', 'Firefox', 'Safari'], npmPackages: '@storybook/*', npmGlobalPackages: '@storybook/*', }) .then(consoleLogger.log); }); program .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') .option('-p --parser ', 'jscodeshift parser') .option( '-n --dry-run', 'Dry run: verify the migration exists and show the files to which it will be applied' ) .option( '-r --rename ', 'Rename suffix of matching files after codemod has been applied, e.g. ".js:.ts"' ) .action((migration, { configDir, glob, dryRun, list, rename, parser }) => { migrate(migration, { configDir, glob, dryRun, list, rename, parser, logger: consoleLogger, }).catch((err) => { logger.error(err); process.exit(1); }); }); program .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) => { logger.error(e); process.exit(1); }) ); program .command('repro [outputDirectory]') .description('Create a reproduction from a set of possible templates') .option('-f --renderer ', 'Filter on given renderer') .option('-t --template