-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI: Add main.js docs.autodocs automigration
- Loading branch information
Showing
4 changed files
with
166 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import type { StorybookConfig } from '@storybook/types'; | ||
import { autodocsTags } from './autodocs-tags'; | ||
|
||
const check = async ({ | ||
main: mainConfig, | ||
storybookVersion = '7.0.0', | ||
previewConfigPath, | ||
}: { | ||
main: Partial<StorybookConfig> & Record<string, unknown>; | ||
storybookVersion?: string; | ||
previewConfigPath?: string; | ||
}) => { | ||
return autodocsTags.check({ | ||
packageManager: {} as any, | ||
configDir: '', | ||
mainConfig: mainConfig as any, | ||
storybookVersion, | ||
previewConfigPath, | ||
}); | ||
}; | ||
|
||
it('with no docs setting', async () => { | ||
await expect( | ||
check({ | ||
main: {}, | ||
}) | ||
).resolves.toBeFalsy(); | ||
}); | ||
|
||
describe('docs.autodocs = true', () => { | ||
it('errors with no preview.js', async () => { | ||
await expect( | ||
check({ | ||
main: { | ||
docs: { autodocs: true }, | ||
}, | ||
}) | ||
).rejects.toThrowError(); | ||
}); | ||
|
||
it('continues with preview.js', async () => { | ||
await expect( | ||
check({ | ||
main: { | ||
docs: { autodocs: true }, | ||
}, | ||
previewConfigPath: '.storybook/preview.js', | ||
}) | ||
).resolves.toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('docs.autodocs != true', () => { | ||
it('docs.autodocs = false', async () => { | ||
await expect( | ||
check({ | ||
main: { | ||
docs: { autodocs: false }, | ||
}, | ||
}) | ||
).resolves.toBeTruthy(); | ||
}); | ||
|
||
it('docs.autodocs = "tag"', async () => { | ||
await expect( | ||
check({ | ||
main: { | ||
docs: { autodocs: 'tag' }, | ||
}, | ||
}) | ||
).resolves.toBeTruthy(); | ||
}); | ||
}); |
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 { dedent } from 'ts-dedent'; | ||
import chalk from 'chalk'; | ||
import type { DocsOptions } from '@storybook/types'; | ||
import { readConfig, writeConfig } from '@storybook/csf-tools'; | ||
import { updateMainConfig } from '../helpers/mainConfigFile'; | ||
import type { Fix } from '../types'; | ||
|
||
const logger = console; | ||
|
||
interface Options { | ||
autodocs: DocsOptions['autodocs']; | ||
previewConfigPath?: string; | ||
} | ||
|
||
/** | ||
*/ | ||
export const autodocsTags: Fix<Options> = { | ||
id: 'autodocs-tags', | ||
versionRange: ['*.*.*', '>=8.0.*'], | ||
async check({ mainConfig, previewConfigPath }) { | ||
const autodocs = mainConfig?.docs?.autodocs; | ||
if (autodocs === undefined) return null; | ||
|
||
if (autodocs === true && !previewConfigPath) { | ||
throw Error(dedent` | ||
❌ Failed to remove the deprecated ${chalk.cyan('docs.autodocs')} setting from ${chalk.cyan( | ||
'.storybook/main.js' | ||
)}. | ||
There is no preview config file to add the ${chalk.cyan('autodocs')} tag to. | ||
Please perform the migration by hand: ${chalk.yellow( | ||
'github.com/storybookjs/storybook/blob/next/MIGRATION.md#mainjs-docsautodocs-is-deprecated' | ||
)} | ||
`); | ||
return null; | ||
} | ||
|
||
return { autodocs, previewConfigPath }; | ||
}, | ||
|
||
prompt({ autodocs, previewConfigPath }) { | ||
let falseMessage = '', | ||
trueMessage = ''; | ||
|
||
if (autodocs === false) { | ||
falseMessage = dedent` | ||
There is no ${chalk.cyan('docs.autodocs = false')} equivalent. | ||
You'll need to check your stories to ensure none are tagged with ${chalk.cyan('autodocs')}. | ||
`; | ||
} else if (autodocs === true) { | ||
trueMessage = ` and update ${chalk.cyan(previewConfigPath)}`; | ||
} | ||
|
||
return dedent` | ||
The ${chalk.cyan('docs.autodocs')} setting in ${chalk.cyan( | ||
'.storybook/main.js' | ||
)} is deprecated.${falseMessage} | ||
Learn more: ${chalk.yellow( | ||
'github.com/storybookjs/storybook/blob/next/MIGRATION.md#mainjs-docsautodocs-is-deprecated' | ||
)} | ||
Remove ${chalk.cyan('docs.autodocs')}${trueMessage}? | ||
`; | ||
}, | ||
|
||
async run({ dryRun, mainConfigPath, result }) { | ||
if (!dryRun) { | ||
if (result.autodocs === true) { | ||
logger.info(`✅ Adding "autodocs" tag to ${result.previewConfigPath}`); | ||
const previewConfig = await readConfig(result.previewConfigPath!); | ||
const tags = previewConfig.getFieldNode(['tags']); | ||
if (tags) { | ||
previewConfig.appendValueToArray(['tags'], 'autodocs'); | ||
} else { | ||
previewConfig.setFieldValue(['tags'], ['autodocs']); | ||
} | ||
await writeConfig(previewConfig); | ||
} | ||
|
||
await updateMainConfig({ mainConfigPath, dryRun: !!dryRun }, async (main) => { | ||
logger.info(`✅ Removing "docs.autodocs" from ${mainConfigPath}`); | ||
main.removeField(['docs', 'autodocs']); | ||
}); | ||
} | ||
}, | ||
}; |
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