Skip to content

Automigration: Refactor automigration command handling and add tests #31302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: next
Choose a base branch
from
Draft
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
14 changes: 2 additions & 12 deletions code/addons/a11y/src/postinstall.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
// eslint-disable-next-line depend/ban-dependencies
import { execa } from 'execa';
import { runAutomigrate } from 'storybook/internal/cli';

import type { PostinstallOptions } from '../../../lib/cli-storybook/src/add';

const $ = execa({
preferLocal: true,
stdio: 'inherit',
// we stream the stderr to the console
reject: false,
});

export default async function postinstall(options: PostinstallOptions) {
await $({
stdio: 'inherit',
})`storybook automigrate addonA11yAddonTest ${options.yes ? '--yes' : ''}`;
await runAutomigrate('addonA11yAddonTest', options);
}
7 changes: 2 additions & 5 deletions code/addons/vitest/src/postinstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'node:fs/promises';
import { writeFile } from 'node:fs/promises';

import { babelParse, generate, traverse } from 'storybook/internal/babel';
import { runAutomigrate } from 'storybook/internal/cli';
import {
JsPackageManagerFactory,
extractProperFrameworkName,
Expand All @@ -18,8 +19,6 @@ import {
import { readConfig, writeConfig } from 'storybook/internal/csf-tools';
import { colors, logger } from 'storybook/internal/node-logger';

// eslint-disable-next-line depend/ban-dependencies
import { $ } from 'execa';
import { findUp } from 'find-up';
import { dirname, extname, join, relative, resolve } from 'pathe';
import picocolors from 'picocolors';
Expand Down Expand Up @@ -335,9 +334,7 @@ export default async function postInstall(options: PostinstallOptions) {
if (a11yAddon) {
try {
logger.plain(`${step} Setting up ${addonA11yName} for @storybook/addon-vitest:`);
await $({
stdio: 'inherit',
})`storybook automigrate addonA11yAddonTest ${options.yes ? '--yes' : ''}`;
await runAutomigrate('addonA11yAddonTest', options);
} catch (e) {
printError(
'🚨 Oh no!',
Expand Down
1 change: 1 addition & 0 deletions code/core/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './dirs';
export * from './project_types';
export * from './NpmOptions';
export * from './eslintPlugin';
export * from './runner';
171 changes: 171 additions & 0 deletions code/core/src/cli/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import type { AddOptions } from '../../../lib/cli-storybook/src/add';
import type { AutofixOptionsFromCLI } from '../../../lib/cli-storybook/src/automigrate/fixes';
import type { FixesIDs, allFixes } from '../../../lib/cli-storybook/src/automigrate/fixes';
import type { DoctorOptions } from '../../../lib/cli-storybook/src/doctor';
import { JsPackageManagerFactory } from '../common';
import type { RemoveAddonOptions } from '../common/utils/remove-addon';

// Common options shared across multiple commands
interface CommonOptions {
packageManager?: 'npm' | 'pnpm' | 'yarn1' | 'yarn2' | 'bun';
cwd?: string;
}

/**
* Execute a Storybook CLI command
*
* @private
* @param args The command arguments to pass to Storybook CLI
* @returns A promise that resolves when the command completes
*/
const executeCommand = async (args: string[], options: CommonOptions = {}) => {
const { packageManager: pkgMgr } = options;

const packageManager = JsPackageManagerFactory.getPackageManager({ force: pkgMgr }, options.cwd);
await packageManager.runPackageCommand('storybook', args, options.cwd, 'inherit');
};

/**
* Run the 'add' command to add an addon to Storybook
*
* @example
*
* ```ts
* await runAdd('@storybook/addon-a11y', {
* yes: true,
* configDir: 'config',
* packageManager: 'npm',
* });
* ```
*/
export const runAdd = async (addonName: string, options: AddOptions = {}) => {
const args = ['add', addonName];

if (options.yes) {
args.push('--yes');
}

if (options.configDir) {
args.push('--config-dir', options.configDir);
}

if (options.packageManager) {
args.push('--package-manager', options.packageManager);
}

if (options.skipPostinstall) {
args.push('--skip-postinstall');
}

return executeCommand(args);
};

/**
* Run the 'remove' command to remove an addon from Storybook
*
* @example
*
* ```ts
* await runRemove('@storybook/addon-a11y', {
* configDir: 'config',
* packageManager: 'npm',
* });
* ```
*/
export const runRemove = async (addonName: string, options: RemoveAddonOptions = {}) => {
const args = ['remove', addonName];

if (options.configDir) {
args.push('--config-dir', options.configDir);
}

if (options.packageManager) {
args.push('--package-manager', options.packageManager);
}

if (options.cwd) {
args.push('--cwd', options.cwd);
}

return executeCommand(args);
};

/**
* Run the 'automigrate' command to check and fix incompatibilities
*
* @example
*
* ```ts
* await runAutomigrate('addon-a11y-parameters', {
* yes: true,
* configDir: 'config',
* packageManager: 'npm',
* });
* ```
*/
export const runAutomigrate = async (
fixId?: FixesIDs<typeof allFixes>,
options: AutofixOptionsFromCLI = {}
) => {
const args = ['automigrate'];

if (fixId) {
args.push(fixId);
}

if (options.yes) {
args.push('--yes');
}

if (options.dryRun) {
args.push('--dry-run');
}

if (options.configDir) {
args.push('--config-dir', options.configDir);
}

if (options.packageManager) {
args.push('--package-manager', options.packageManager);
}

if (options.list) {
args.push('--list');
}

if (options.skipInstall) {
args.push('--skip-install');
}

if (options.renderer) {
args.push('--renderer', options.renderer);
}

return executeCommand(args);
};

/**
* Run the 'doctor' command to check for problems and get suggestions
*
* @example
*
* ```ts
* await runDoctor({
* configDir: 'config',
* packageManager: 'npm',
* });
* ```
*/
export const runDoctor = async (options: DoctorOptions = {}) => {
const args = ['doctor'];

if (options.configDir) {
args.push('--config-dir', options.configDir);
}

if (options.packageManager) {
args.push('--package-manager', options.packageManager);
}

return executeCommand(args, options);
};
2 changes: 1 addition & 1 deletion code/core/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export * from './utils/log-config';
export * from './utils/normalize-stories';
export * from './utils/paths';
export * from './utils/readTemplate';
export * from './utils/remove';
export * from './utils/resolve-path-in-sb-cache';
export * from './utils/symlinks';
export * from './utils/template';
Expand All @@ -46,6 +45,7 @@ export * from './utils/sync-main-preview-addons';
export * from './js-package-manager';
export * from './utils/scan-and-transform-files';
export * from './utils/transform-imports';
export * from './utils/addons';

export { versions };

Expand Down
Loading
Loading