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

Migrate: skip the automigration for gf markdown when user isn't using mdx #22186

Merged
merged 5 commits into from
May 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions code/lib/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"@types/semver": "^7.3.4",
"@types/shelljs": "^0.8.7",
"@types/util-deprecate": "^1.0.0",
"slash": "^5.0.0",
"strip-json-comments": "^3.1.1",
"typescript": "~4.9.3"
},
Expand Down
11 changes: 10 additions & 1 deletion code/lib/cli/src/automigrate/fixes/mdx-gfm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import type { PackageJson } from '../../js-package-manager';
import { makePackageManager, mockStorybookData } from '../helpers/testing-helpers';
import { mdxgfm } from './mdx-gfm';

jest.mock('globby', () => ({
__esModule: true,
default: jest.fn().mockResolvedValue(['a/fake/file.mdx']),
}));

const check = async ({
packageJson,
main: mainConfig,
Expand Down Expand Up @@ -88,7 +93,9 @@ describe('continue', () => {
await expect(
check({
packageJson,
main: {},
main: {
stories: ['**/*.stories.mdx'],
},
})
).resolves.toBeTruthy();
});
Expand All @@ -97,6 +104,7 @@ describe('continue', () => {
check({
packageJson,
main: {
stories: ['**/*.stories.mdx'],
addons: [
{
name: '@storybook/addon-essentials',
Expand All @@ -118,6 +126,7 @@ describe('continue', () => {
check({
packageJson,
main: {
stories: ['**/*.stories.mdx'],
addons: ['@storybook/addon-essentials'],
},
})
Expand Down
21 changes: 21 additions & 0 deletions code/lib/cli/src/automigrate/fixes/mdx-gfm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { dedent } from 'ts-dedent';
import semver from 'semver';
import { join } from 'path';
import slash from 'slash';
import glob from 'globby';
import { getStorybookData, updateMainConfig } from '../helpers/mainConfigFile';
import type { Fix } from '../types';
import { getStorybookVersionSpecifier } from '../../helpers';
Expand All @@ -22,9 +25,27 @@ export const mdxgfm: Fix<Options> = {
return null;
}

const hasMDXFiles = await mainConfig?.stories?.reduce(async (acc, item) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems worse? considering it's not using the glob patterns set in main.ts, and will ignore any MDX files not following the .stories.mdx pattern.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright sounds good, maybe the other automigration should follow this pattern then

const val = await acc;

if (val === true) {
return true;
}

const pattern =
typeof item === 'string'
? slash(join(configDir, item))
: slash(join(configDir, item.directory, item.files));

const files = await glob(pattern);

return files.some((f) => f.endsWith('.mdx'));
}, Promise.resolve(false));

const usesMDX1 = mainConfig?.features?.legacyMdx1 === true || false;
const skip =
usesMDX1 ||
!hasMDXFiles ||
!!mainConfig.addons?.find((item) => {
if (item === '@storybook/addon-mdx-gfm') {
return true;
Expand Down
1 change: 1 addition & 0 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5745,6 +5745,7 @@ __metadata:
semver: ^7.3.7
shelljs: ^0.8.5
simple-update-notifier: ^1.0.0
slash: ^5.0.0
strip-json-comments: ^3.1.1
tempy: ^1.0.1
ts-dedent: ^2.0.0
Expand Down