Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions code/lib/cli-storybook/src/automigrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export const automigrate = async ({
isLatest,
storiesPaths,
hasCsfFactoryPreview,
glob,
}: AutofixOptions): Promise<{
fixResults: Record<string, FixStatus>;
preCheckFailure?: PreCheckFailure;
Expand All @@ -146,6 +147,8 @@ export const automigrate = async ({
result: null,
storybookVersion,
storiesPaths,
yes,
glob,
});

return null;
Expand Down Expand Up @@ -380,6 +383,7 @@ export async function runFixes({
skipInstall,
storybookVersion,
storiesPaths,
yes,
});
logger.log(`✅ ran ${picocolors.cyan(f.id)} migration`);

Expand Down
3 changes: 2 additions & 1 deletion code/lib/cli-storybook/src/automigrate/multi-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export async function runAutomigrationsForProjects(
selectedAutomigrations: AutomigrationCheckResult[],
options: MultiProjectRunAutomigrationOptions
): Promise<Record<ConfigDir, AutomigrationResult>> {
const { dryRun, skipInstall, automigrations } = options;
const { dryRun, skipInstall, automigrations, yes } = options;
const projectResults: Record<ConfigDir, AutomigrationResult> = {};

const applicableAutomigrations = selectedAutomigrations.filter((am) =>
Expand Down Expand Up @@ -378,6 +378,7 @@ export async function runAutomigrationsForProjects(
skipInstall,
storybookVersion: project.storybookVersion,
storiesPaths: project.storiesPaths,
yes,
};

await fix.run(runOptions);
Expand Down
6 changes: 6 additions & 0 deletions code/lib/cli-storybook/src/automigrate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export interface RunOptions<ResultType> {
skipInstall?: boolean;
storybookVersion: string;
storiesPaths: string[];
/** Skip prompts and use defaults (from --yes flag) */
yes?: boolean;
/** Glob pattern for story files (for csf-factories codemod) */
glob?: string;
}

/**
Expand Down Expand Up @@ -97,6 +101,8 @@ export interface AutofixOptionsFromCLI {
skipInstall?: boolean;
hideMigrationSummary?: boolean;
skipDoctor?: boolean;
/** Glob pattern for story files (for csf-factories codemod) */
glob?: string;
}

export enum FixStatus {
Expand Down
1 change: 1 addition & 0 deletions code/lib/cli-storybook/src/bin/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ command('automigrate [fixId]')
'The renderer package for the framework Storybook is using.'
)
.option('--skip-doctor', 'Skip doctor check')
.option('--glob <pattern>', 'Glob pattern for story files (for csf-factories codemod)')
.action(async (fixId, options) => {
withTelemetry('automigrate', { cliOptions: options }, async () => {
logger.intro(fixId ? `Running ${fixId} automigration` : 'Running automigrations');
Expand Down
36 changes: 28 additions & 8 deletions code/lib/cli-storybook/src/codemod/csf-factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@ async function runStoriesCodemod(options: {
packageManager: JsPackageManager;
useSubPathImports: boolean;
previewConfigPath: string;
yes: boolean | undefined;
glob: string | undefined;
}) {
const { dryRun, packageManager, ...codemodOptions } = options;
const { dryRun, packageManager, yes, glob, ...codemodOptions } = options;
try {
let globString = '{stories,src}/**/{Button,Header,Page,button,header,page}.stories.*';
if (!optionalEnvToBoolean(process.env.IN_STORYBOOK_SANDBOX)) {
const inSandbox = optionalEnvToBoolean(process.env.IN_STORYBOOK_SANDBOX) ?? false;
let globString = glob ?? '**/*.{stories,story}.{js,jsx,ts,tsx,mjs,mjsx,mts,mtsx}';

if (!glob && inSandbox) {
// Sandbox uses limited glob for faster testing (unless glob explicitly provided)
globString = '{stories,src}/**/{Button,Header,Page,button,header,page}.stories.*';
} else if (!glob && !yes) {
logger.log('Please enter the glob for your stories to migrate');
globString = await prompt.text({
message: 'glob',
initialValue: '**/*.{stories,story}.{js,jsx,ts,tsx,mjs,mjsx,mts,mtsx}',
initialValue: globString,
});
}

Expand All @@ -52,10 +59,21 @@ async function runStoriesCodemod(options: {
export const csfFactories: CommandFix = {
id: 'csf-factories',
promptType: 'command',
async run({ dryRun, mainConfig, mainConfigPath, previewConfigPath, packageManager, configDir }) {
let useSubPathImports = true;

if (!optionalEnvToBoolean(process.env.IN_STORYBOOK_SANDBOX)) {
async run({
dryRun,
mainConfig,
mainConfigPath,
previewConfigPath,
packageManager,
configDir,
yes,
glob,
}) {
const inSandbox = optionalEnvToBoolean(process.env.IN_STORYBOOK_SANDBOX) ?? false;
// Defaults to false for users and true in sandbox
let useSubPathImports = inSandbox;

if (!yes && !inSandbox) {
// prompt whether the user wants to use imports map
logger.logBox(dedent`
The CSF Factories format can benefit from using absolute imports of your ${picocolors.cyan(previewConfigPath)} file. We can configure that for you, using subpath imports (a node standard), by adjusting the imports property of your package.json.
Expand Down Expand Up @@ -96,6 +114,8 @@ export const csfFactories: CommandFix = {
packageManager,
useSubPathImports,
previewConfigPath: previewConfigPath!,
yes,
glob,
});

logger.step('Applying codemod on your main config...');
Expand Down
Loading