Skip to content
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

CLI: Update stories glob in mdx codemod and not in mdx automigration #21809

Merged
merged 5 commits into from
Mar 30, 2023
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
2 changes: 0 additions & 2 deletions code/lib/cli/src/automigrate/fixes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { autodocsTrue } from './autodocs-true';
import { addReact } from './add-react';
import { nodeJsRequirement } from './nodejs-requirement';
import { missingBabelRc } from './missing-babelrc';
import { bareMdxStoriesGlob } from './bare-mdx-stories-glob';
import { angularBuilders } from './angular-builders';
import { angularBuildersMultiproject } from './angular-builders-multiproject';

Expand All @@ -36,7 +35,6 @@ export const allFixes: Fix[] = [
removedGlobalClientAPIs,
mdx1to2,
mdxgfm,
bareMdxStoriesGlob,
autodocsTrue,
addReact,
missingBabelRc,
Expand Down
102 changes: 69 additions & 33 deletions code/lib/cli/src/automigrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { join } from 'path';
import { getStorybookInfo, loadMainConfig } from '@storybook/core-common';
import semver from 'semver';
import { JsPackageManagerFactory, useNpmWarning } from '../js-package-manager';
import type { PackageManagerName } from '../js-package-manager';

import type { Fix, FixId, FixOptions, FixSummary } from './fixes';
import { FixStatus, PreCheckFailure, allFixes } from './fixes';
Expand Down Expand Up @@ -64,12 +65,6 @@ export const automigrate = async ({
return null;
}

if (useNpm) {
useNpmWarning();
// eslint-disable-next-line no-param-reassign
pkgMgr = 'npm';
}

const selectedFixes = inputFixes || allFixes;
const fixes = fixId ? selectedFixes.filter((f) => f.id === fixId) : selectedFixes;

Expand All @@ -81,6 +76,72 @@ export const automigrate = async ({

augmentLogsToFile();

logger.info('🔎 checking possible migrations..');

const { fixResults, fixSummary } = await runFixes({
fixes,
useNpm,
pkgMgr,
userSpecifiedConfigDir,
rendererPackage,
skipInstall,
dryRun,
yes,
});

const hasFailures = Object.values(fixResults).some(
(r) => r === FixStatus.FAILED || r === FixStatus.CHECK_FAILED
);

// if migration failed, display a log file in the users cwd
if (hasFailures) {
await move(TEMP_LOG_FILE_PATH, join(process.cwd(), LOG_FILE_NAME), { overwrite: true });
} else {
await remove(TEMP_LOG_FILE_PATH);
}

const packageManager = JsPackageManagerFactory.getPackageManager({ force: pkgMgr });
const installationMetadata = await packageManager.findInstallations([
'@storybook/*',
'storybook',
]);

logger.info();
logger.info(
getMigrationSummary({ fixResults, fixSummary, logFile: LOG_FILE_PATH, installationMetadata })
);
logger.info();

cleanup();

return fixResults;
};

export async function runFixes({
fixes,
dryRun,
yes,
useNpm,
pkgMgr,
userSpecifiedConfigDir,
rendererPackage,
skipInstall,
}: {
fixes: Fix[];
yes?: boolean;
dryRun?: boolean;
useNpm?: boolean;
pkgMgr?: PackageManagerName;
userSpecifiedConfigDir?: string;
rendererPackage?: string;
skipInstall?: boolean;
}) {
if (useNpm) {
useNpmWarning();
// eslint-disable-next-line no-param-reassign
pkgMgr = 'npm';
}

const packageManager = JsPackageManagerFactory.getPackageManager({ force: pkgMgr });

const {
Expand Down Expand Up @@ -126,7 +187,6 @@ export const automigrate = async ({
};
}

logger.info('🔎 checking possible migrations..');
const fixResults = {} as Record<FixId, FixStatus>;
const fixSummary: FixSummary = { succeeded: [], failed: {}, manual: [], skipped: [] };

Expand Down Expand Up @@ -248,29 +308,5 @@ export const automigrate = async ({
}
}

const hasFailures = Object.values(fixResults).some(
(r) => r === FixStatus.FAILED || r === FixStatus.CHECK_FAILED
);

// if migration failed, display a log file in the users cwd
if (hasFailures) {
await move(TEMP_LOG_FILE_PATH, join(process.cwd(), LOG_FILE_NAME), { overwrite: true });
} else {
await remove(TEMP_LOG_FILE_PATH);
}

const installationMetadata = await packageManager.findInstallations([
'@storybook/*',
'storybook',
]);

logger.info();
logger.info(
getMigrationSummary({ fixResults, fixSummary, logFile: LOG_FILE_PATH, installationMetadata })
);
logger.info();

cleanup();

return fixResults;
};
return { fixResults, fixSummary };
}
22 changes: 21 additions & 1 deletion code/lib/cli/src/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import { listCodemods, runCodemod } from '@storybook/codemod';
import { runFixes } from './automigrate';
import { bareMdxStoriesGlob } from './automigrate/fixes/bare-mdx-stories-glob';
import { JsPackageManagerFactory } from './js-package-manager';
import { getStorybookVersionSpecifier } from './helpers';

export async function migrate(migration: any, { glob, dryRun, list, rename, logger, parser }: any) {
const logger = console;

export async function migrate(migration: any, { glob, dryRun, list, rename, parser }: any) {
if (list) {
listCodemods().forEach((key: any) => logger.log(key));
} else if (migration) {
if (migration === 'mdx-to-csf' && !dryRun) {
await runFixes({ fixes: [bareMdxStoriesGlob] });
await addStorybookBlocksPackage();
}
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
await runCodemod(migration, { glob, dryRun, logger, rename, parser });
} else {
throw new Error('Migrate: please specify a migration name or --list');
}
}

export async function addStorybookBlocksPackage() {
const packageManager = JsPackageManagerFactory.getPackageManager();
const packageJson = packageManager.retrievePackageJson();
const versionToInstall = getStorybookVersionSpecifier(packageManager.retrievePackageJson());
logger.info(`✅ Adding "@storybook/blocks" package`);
await packageManager.addDependencies({ installAsDevDependencies: true, packageJson }, [
`@storybook/blocks@${versionToInstall}`,
]);
}