-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(storybook): ignore nx storybook plugin from sb eslint
- Loading branch information
Showing
4 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/storybook/src/migrations/update-16-1-0/eslint-ignore-react-plugin.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { | ||
addProjectConfiguration, | ||
ProjectConfiguration, | ||
readJson, | ||
Tree, | ||
updateJson, | ||
} from '@nx/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; | ||
import * as variousProjects from '../update-15-7-0/test-configs/various-configs.json'; | ||
import eslintIgnoreReactPlugin from './eslint-ignore-react-plugin'; | ||
|
||
describe('Ignore @nx/react/plugins/storybook in Storybook eslint plugin', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(async () => { | ||
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); | ||
addProjectConfiguration( | ||
tree, | ||
variousProjects['main-vite'].name, | ||
variousProjects['main-vite'] as ProjectConfiguration | ||
); | ||
tree.write('apps/main-vite/tsconfig.json', `{}`); | ||
tree.write( | ||
`apps/main-vite/.storybook/main.js`, | ||
` | ||
module.exports = { | ||
stories: ['../src/lib/**/*.stories.@(mdx|js|jsx|ts|tsx)'], | ||
addons: ['@storybook/addon-essentials'], | ||
framework: { | ||
name: '@storybook/react-vite', | ||
options: {}, | ||
}, | ||
}; | ||
` | ||
); | ||
addProjectConfiguration( | ||
tree, | ||
variousProjects['main-webpack'].name, | ||
variousProjects['main-webpack'] as ProjectConfiguration | ||
); | ||
|
||
if (!tree.exists('.eslintrc.json')) { | ||
tree.write('.eslintrc.json', '{}'); | ||
} | ||
updateJson(tree, '.eslintrc.json', (json) => { | ||
json.extends ??= []; | ||
json.extends.push('plugin:storybook/recommended'); | ||
return json; | ||
}); | ||
}); | ||
|
||
it('should not ignore the plugin if it is not used', async () => { | ||
await eslintIgnoreReactPlugin(tree); | ||
const eslintConfig = readJson(tree, '.eslintrc.json'); | ||
expect(eslintConfig.rules).toBeUndefined(); | ||
}); | ||
|
||
it(`should add ignore @nx/react/plugins/storybook to eslint config`, async () => { | ||
tree.write('apps/main-webpack/tsconfig.json', `{}`); | ||
tree.write( | ||
`apps/main-webpack/.storybook/main.js`, | ||
` | ||
module.exports = { | ||
stories: ['../src/lib/**/*.stories.@(mdx|js|jsx|ts|tsx)'], | ||
addons: ['@storybook/addon-essentials', '@nx/react/plugins/storybook'], | ||
framework: { | ||
name: '@storybook/react-webpack5', | ||
options: {}, | ||
}, | ||
}; | ||
` | ||
); | ||
|
||
await eslintIgnoreReactPlugin(tree); | ||
|
||
const eslintConfig = readJson(tree, '.eslintrc.json'); | ||
expect(eslintConfig.rules).toHaveProperty( | ||
'storybook/no-uninstalled-addons' | ||
); | ||
|
||
expect(eslintConfig.rules['storybook/no-uninstalled-addons']).toMatchObject( | ||
[ | ||
'error', | ||
{ | ||
ignore: ['@nx/react/plugins/storybook'], | ||
}, | ||
] | ||
); | ||
}); | ||
}); |
49 changes: 49 additions & 0 deletions
49
packages/storybook/src/migrations/update-16-1-0/eslint-ignore-react-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { | ||
getProjects, | ||
joinPathFragments, | ||
Tree, | ||
formatFiles, | ||
updateJson, | ||
} from '@nx/devkit'; | ||
|
||
export default async function (tree: Tree) { | ||
const projects = getProjects(tree); | ||
const reactPlugin = '@nx/react/plugins/storybook'; | ||
let shouldIgnoreReactPlugin = false; | ||
|
||
for (const [, config] of projects) { | ||
let sbConfigPath = joinPathFragments(config.root, '.storybook/main.ts'); | ||
|
||
if (!tree.exists(sbConfigPath)) { | ||
sbConfigPath = joinPathFragments(config.root, '.storybook/main.js'); | ||
} | ||
|
||
if (!tree.exists(sbConfigPath)) { | ||
continue; | ||
} | ||
|
||
const sbConfig = tree.read(sbConfigPath, 'utf-8'); | ||
console.log('sbConfig', sbConfig); | ||
if (sbConfig.includes(reactPlugin)) { | ||
shouldIgnoreReactPlugin = true; | ||
break; | ||
} | ||
} | ||
|
||
if (shouldIgnoreReactPlugin && tree.exists('.eslintrc.json')) { | ||
updateJson(tree, '.eslintrc.json', (json) => { | ||
if (json.extends?.includes('plugin:storybook/recommended')) { | ||
json.rules ??= {}; | ||
json.rules['storybook/no-uninstalled-addons'] = [ | ||
'error', | ||
{ | ||
ignore: ['@nx/react/plugins/storybook'], | ||
}, | ||
]; | ||
return json; | ||
} | ||
}); | ||
} | ||
|
||
await formatFiles(tree); | ||
} |