Skip to content

Commit

Permalink
simplify eslint installation
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Nov 11, 2021
1 parent 7b7fa14 commit 81923fc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 82 deletions.
42 changes: 2 additions & 40 deletions lib/cli/src/automigrate/fixes/eslint-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,51 +96,13 @@ describe('eslint-plugin fix', () => {
});

describe('should install eslint plugin', () => {
it('with globs defined in main.js', async () => {
it('when .eslintrc is using a supported extension', async () => {
await expect(
checkEslint({
packageJson,
main: {
stories: [
// this is the path relative to .storybook
// or wherever the main.js file is
'../stories/**/*.stories.@(js|ts|tsx)',
'../src/components/Accordion.stories.tsx',
],
},
})
).resolves.toMatchObject({
storiesGlobs: [
// this should be the path based on main.js AND root directory
'stories/**/*.stories.@(js|ts|tsx)',
'src/components/Accordion.stories.tsx',
],
});
});

it('with composed globs from CSF3 configuration format', async () => {
await expect(
checkEslint({
packageJson,
main: {
stories: [
// this is the path relative to .storybook
// or wherever the main.js file is
{
dir: '../src/components',
},
{
dir: '../src/pages',
},
],
},
})
).resolves.toMatchObject({
storiesGlobs: [
// this should be the path based on main.js AND root directory
'src/components/**/*.stories.@(js|ts|jsx|tsx)',
'src/pages/**/*.stories.@(js|ts|jsx|tsx)',
],
unsupportedExtension: undefined,
});
});

Expand Down
50 changes: 8 additions & 42 deletions lib/cli/src/automigrate/fixes/eslint-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,9 @@ const logger = console;
interface EslintPluginRunOptions {
main: ConfigFile;
eslintFile: string;
storiesGlobs: string[];
unsupportedExtension?: string;
}

type StoriesConfig =
| string
| {
dir: string;
};

/**
* Does the user not have eslint-plugin-storybook installed?
*
Expand All @@ -47,7 +40,7 @@ export const eslintPlugin: Fix<EslintPluginRunOptions> = {

const { mainConfig } = config;
if (!mainConfig) {
logger.warn('Unable to find storybook main.js config');
logger.warn('Unable to find storybook main.js config, skipping');
return null;
}

Expand All @@ -60,28 +53,14 @@ export const eslintPlugin: Fix<EslintPluginRunOptions> = {
}

if (!eslintFile && !unsupportedExtension) {
logger.warn('Unable to find .eslintrc config file');
logger.warn('Unable to find .eslintrc config file, skipping');
return null;
}

// If in the future the eslint plugin has a framework option, using main to extract the framework field will be very useful
const main = await readConfig(mainConfig);

const storiesGlobs: string[] = [];
if (!unsupportedExtension) {
const rawStoriesGlobs = main.getFieldValue(['stories']);
rawStoriesGlobs.forEach((glob: StoriesConfig) => {
if (typeof glob === 'string') {
if (!glob.endsWith('.mdx')) {
storiesGlobs.push(glob.replace(/(\|mdx)|(mdx\|)|(\|md)|(md\|)/g, ''));
}
} else {
// stories in CSF3 format. Users only specify the folder so we add a wildcard
storiesGlobs.push(`${glob.dir}/**/*.stories.@(js|ts|jsx|tsx)`);
}
});
}

return { eslintFile, main, storiesGlobs, unsupportedExtension };
return { eslintFile, main, unsupportedExtension };
},

prompt() {
Expand All @@ -94,11 +73,7 @@ export const eslintPlugin: Fix<EslintPluginRunOptions> = {
`;
},

async run({
result: { main, eslintFile, storiesGlobs, unsupportedExtension },
packageManager,
dryRun,
}) {
async run({ result: { eslintFile, unsupportedExtension }, packageManager, dryRun }) {
const deps = [`eslint-plugin-storybook`];

logger.info(`✅ Adding dependencies: ${deps}`);
Expand All @@ -123,18 +98,9 @@ export const eslintPlugin: Fix<EslintPluginRunOptions> = {
logger.info(`✅ Configuring eslint rules in ${eslint.fileName}`);

if (!dryRun) {
logger.info(`✅ Adding Storybook to plugin list`);
eslint.setFieldValue(['plugins'], [...eslint.getFieldValue(['plugins']), 'storybook']);

const storybookOverrides = {
files: storiesGlobs,
extends: ['plugin:storybook/recommended'],
};

logger.info(`✅ Adding overrides using stories defined in ${main.fileName}`);
const currentOverrides = eslint.getFieldValue(['overrides']) || [];

eslint.setFieldValue(['overrides'], [...currentOverrides, storybookOverrides]);
logger.info(`✅ Adding Storybook to extends list`);
const extendsConfig = eslint.getFieldValue(['extends']) || [];
eslint.setFieldValue(['extends'], [...extendsConfig, 'plugin:storybook/recommended']);
await writeConfig(eslint);
}
},
Expand Down

0 comments on commit 81923fc

Please sign in to comment.