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

CLI: Upgrade automigrations to use new safe helpers #20693

Merged
merged 2 commits into from
Jan 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion code/lib/cli/src/automigrate/fixes/mainjsFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const mainjsFramework: Fix<MainjsFrameworkRunOptions> = {
}

const main = await readConfig(mainConfig);
const currentFramework = main.getFieldValue(['framework']);
const currentFramework = main.getFieldNode(['framework']);
const features = main.getFieldValue(['features']);

if (currentFramework) return null;
Expand Down
32 changes: 26 additions & 6 deletions code/lib/cli/src/automigrate/fixes/missing-babelrc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,33 @@ describe('missing-babelrc fix', () => {

// different babel extensions
await expect(
check({ extraFiles: { '.babelrc': babelContent }, packageJson })
check({
extraFiles: { '.babelrc': babelContent },
packageJson,
main: { framework: '@storybook/react' },
})
).resolves.toBeNull();
await expect(
check({ extraFiles: { '.babelrc.json': babelContent }, packageJson })
check({
extraFiles: { '.babelrc.json': babelContent },
packageJson,
main: { framework: '@storybook/react' },
})
).resolves.toBeNull();
await expect(
check({ extraFiles: { 'babel.config.json': babelContent }, packageJson })
check({
extraFiles: { 'babel.config.json': babelContent },
packageJson,
main: { framework: '@storybook/react' },
})
).resolves.toBeNull();

// babel field in package.json
await expect(
check({ packageJson: { ...packageJson, babel: babelContent } })
check({
packageJson: { ...packageJson, babel: babelContent },
main: { framework: '@storybook/react' },
})
).resolves.toBeNull();
});

Expand All @@ -72,7 +87,9 @@ describe('missing-babelrc fix', () => {
},
};

await expect(check({ packageJson })).resolves.toBeNull();
await expect(
check({ packageJson, main: { framework: '@storybook/nextjs' } })
).resolves.toBeNull();
});

it('skips when using CRA preset', async () => {
Expand All @@ -84,7 +101,10 @@ describe('missing-babelrc fix', () => {
};

await expect(
check({ packageJson, main: { addons: ['@storybook/preset-create-react-app'] } })
check({
packageJson,
main: { framework: '@storybook/react', addons: ['@storybook/preset-create-react-app'] },
})
).resolves.toBeNull();
});

Expand Down
18 changes: 9 additions & 9 deletions code/lib/cli/src/automigrate/fixes/missing-babelrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ export const missingBabelRc: Fix<MissingBabelRcOptions> = {

const main = await readConfig(mainConfig);

const frameworkField = main.getFieldValue(['framework']);
const frameworkPackage =
typeof frameworkField === 'string' ? frameworkField : frameworkField?.name;
const frameworkPackage = main.getNameFromPath(['framework']);

const addons: any[] = main.getFieldValue(['addons']) || [];
const addons = main.getNamesFromPath(['addons']);

const hasCraPreset = addons.find((addon) => {
const name = typeof addon === 'string' ? addon : addon.name;
return name === '@storybook/preset-create-react-app';
});
const hasCraPreset =
addons && addons.find((addon) => addon === '@storybook/preset-create-react-app');

if (frameworksThatNeedBabelConfig.includes(frameworkPackage) && !hasCraPreset) {
if (
frameworkPackage &&
frameworksThatNeedBabelConfig.includes(frameworkPackage) &&
!hasCraPreset
) {
const config = await loadPartialConfigAsync({
babelrc: true,
filename: '__fake__.js', // somehow needed to detect .babelrc.* files
Expand Down
2 changes: 1 addition & 1 deletion code/lib/cli/src/automigrate/fixes/new-frameworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export const newFrameworks: Fix<NewFrameworkRunOptions> = {
// 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 frameworkPackage = main.getFieldValue(['framework']) as keyof typeof packagesMap;
const frameworkPackage = main.getNameFromPath(['framework']) as keyof typeof packagesMap;
const builder = main.getFieldValue(['core', 'builder']);

if (!frameworkPackage) {
Expand Down
6 changes: 2 additions & 4 deletions code/lib/cli/src/automigrate/fixes/sveltekit-framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,16 @@ export const sveltekitFramework: Fix<SvelteKitFrameworkRunOptions> = {
}

const main = await readConfig(mainConfig);
const frameworkConfig = main.getFieldValue(['framework']);
const framework = main.getNameFromPath(['framework']);

if (!frameworkConfig) {
if (!framework) {
logger.warn(dedent`
❌ Unable to determine Storybook framework, skipping ${chalk.cyan(fixId)} fix.
🤔 Are you running automigrate from your project directory?
`);
return null;
}

const framework = typeof frameworkConfig === 'string' ? frameworkConfig : frameworkConfig.name;

if (framework === '@storybook/sveltekit') {
// already using the new framework
return null;
Expand Down