From 89b7f5d1560d4084d1838428949461e5af2daff7 Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Wed, 3 Apr 2024 11:13:29 +0200 Subject: [PATCH] Merge pull request #26721 from storybookjs/yann/add-config-dir-flag-to-migrate CLI: Add --config-dir flag to migrate command (cherry picked from commit fbd2aafb3fd1c2b44c127e00021db9d4f30c0a8a) --- code/lib/cli/src/generate.ts | 2 +- code/lib/cli/src/migrate.ts | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/code/lib/cli/src/generate.ts b/code/lib/cli/src/generate.ts index 53c6259dc35b..5a75f11c6173 100644 --- a/code/lib/cli/src/generate.ts +++ b/code/lib/cli/src/generate.ts @@ -126,6 +126,7 @@ command('migrate [migration]') .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('-c, --config-dir ', 'Directory where to load Storybook configurations from') .option( '-n --dry-run', 'Dry run: verify the migration exists and show the files to which it will be applied' @@ -142,7 +143,6 @@ command('migrate [migration]') list, rename, parser, - logger: consoleLogger, }).catch((err) => { logger.error(err); process.exit(1); diff --git a/code/lib/cli/src/migrate.ts b/code/lib/cli/src/migrate.ts index 29ee10dc82a3..fc85c75d9609 100644 --- a/code/lib/cli/src/migrate.ts +++ b/code/lib/cli/src/migrate.ts @@ -11,7 +11,25 @@ import { getStorybookVersionSpecifier } from './helpers'; const logger = console; -export async function migrate(migration: any, { glob, dryRun, list, rename, parser }: any) { +type CLIOptions = { + glob: string; + configDir?: string; + dryRun?: boolean; + list?: string[]; + /** + * Rename suffix of matching files after codemod has been applied, e.g. ".js:.ts" + */ + rename?: string; + /** + * jscodeshift parser + */ + parser?: 'babel' | 'babylon' | 'flow' | 'ts' | 'tsx'; +}; + +export async function migrate( + migration: any, + { glob, dryRun, list, rename, parser, configDir: userSpecifiedConfigDir }: CLIOptions +) { if (list) { listCodemods().forEach((key: any) => logger.log(key)); } else if (migration) { @@ -19,13 +37,14 @@ export async function migrate(migration: any, { glob, dryRun, list, rename, pars const packageManager = JsPackageManagerFactory.getPackageManager(); const [packageJson, storybookVersion] = await Promise.all([ - // packageManager.retrievePackageJson(), getCoercedStorybookVersion(packageManager), ]); - const { configDir: inferredConfigDir, mainConfig: mainConfigPath } = - getStorybookInfo(packageJson); - const configDir = inferredConfigDir || '.storybook'; + const { configDir: inferredConfigDir, mainConfig: mainConfigPath } = getStorybookInfo( + packageJson, + userSpecifiedConfigDir + ); + const configDir = userSpecifiedConfigDir || inferredConfigDir || '.storybook'; // GUARDS if (!storybookVersion) {