Skip to content

Commit

Permalink
Merge pull request #22186 from storybookjs/norbert/fix-22167
Browse files Browse the repository at this point in the history
Migrate: skip the automigration for gf markdown when user isn't using mdx
  • Loading branch information
ndelangen authored and shilman committed May 3, 2023
1 parent a24659a commit b18526f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
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) => {
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 @@ -5878,6 +5878,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

0 comments on commit b18526f

Please sign in to comment.