Skip to content

Commit

Permalink
CLI: Fix eslint configuration for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed May 12, 2024
1 parent 7c43348 commit 3bc2a51
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
21 changes: 21 additions & 0 deletions code/lib/cli/src/automigrate/helpers/eslintPlugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, it, expect, vi } from 'vitest';
import { normalizeExtends } from './eslintPlugin';

describe('normalizeExtends', () => {
it('returns empty array when existingExtends is falsy', () => {
expect(normalizeExtends(null)).toEqual([]);
expect(normalizeExtends(undefined)).toEqual([]);
});

it('returns existingExtends when it is a string', () => {
expect(normalizeExtends('foo')).toEqual(['foo']);
});

it('returns existingExtends when it is an array', () => {
expect(normalizeExtends(['foo'])).toEqual(['foo']);
});

it('throws when existingExtends is not a string or array', () => {
expect(() => normalizeExtends(true)).toThrowError('Invalid eslint extends true');
});
});
30 changes: 14 additions & 16 deletions code/lib/cli/src/automigrate/helpers/eslintPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ export async function extractEslintInfo(packageManager: JsPackageManager): Promi
return { hasEslint, isStorybookPluginInstalled, eslintConfigFile };
}

export const normalizeExtends = (existingExtends: any): string[] => {
if (!existingExtends) return [];
if (typeof existingExtends === 'string') return [existingExtends];
if (Array.isArray(existingExtends)) return existingExtends;
throw new Error(`Invalid eslint extends ${existingExtends}`);
};

export async function configureEslintPlugin(
eslintFile: string | undefined,
packageManager: JsPackageManager
Expand All @@ -55,38 +62,29 @@ export async function configureEslintPlugin(
paddedLog(`Configuring Storybook ESLint plugin at ${eslintFile}`);
if (eslintFile.endsWith('json')) {
const eslintConfig = (await readJson(eslintFile)) as { extends?: string[] };
const existingConfigValue = Array.isArray(eslintConfig.extends)
? eslintConfig.extends
: [eslintConfig.extends].filter(Boolean);
eslintConfig.extends = [
...(existingConfigValue || []),
'plugin:storybook/recommended',
] as string[];
const existingExtends = normalizeExtends(eslintConfig.extends).filter(Boolean);
eslintConfig.extends = [...existingExtends, 'plugin:storybook/recommended'] as string[];

const eslintFileContents = await readFile(eslintFile, 'utf8');
const spaces = detectIndent(eslintFileContents).amount || 2;
await writeJson(eslintFile, eslintConfig, { spaces });
} else {
const eslint = await readConfig(eslintFile);
const extendsConfig = eslint.getFieldValue(['extends']) || [];
const existingConfigValue = Array.isArray(extendsConfig)
? extendsConfig
: [extendsConfig].filter(Boolean);
eslint.setFieldValue(
['extends'],
[...(existingConfigValue || []), 'plugin:storybook/recommended']
);
const existingExtends = normalizeExtends(eslint.getFieldValue(['extends'])).filter(Boolean);
eslint.setFieldValue(['extends'], [...existingExtends, 'plugin:storybook/recommended']);

await writeConfig(eslint);
}
} else {
paddedLog(`Configuring eslint-plugin-storybook in your package.json`);
const packageJson = await packageManager.retrievePackageJson();
const existingExtends = normalizeExtends(packageJson.eslintConfig?.extends).filter(Boolean);

await packageManager.writePackageJson({
...packageJson,
eslintConfig: {
...packageJson.eslintConfig,
extends: [...(packageJson.eslintConfig?.extends || []), 'plugin:storybook/recommended'],
extends: [...existingExtends, 'plugin:storybook/recommended'],
},
});
}
Expand Down

0 comments on commit 3bc2a51

Please sign in to comment.