/
autodocs-true.ts
94 lines (75 loc) · 3.12 KB
/
autodocs-true.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import chalk from 'chalk';
import { dedent } from 'ts-dedent';
import type { StorybookConfig } from '@storybook/types';
import type { Fix } from '../types';
import { getStorybookData, updateMainConfig } from '../helpers/mainConfigFile';
const logger = console;
interface AutodocsTrueFrameworkRunOptions {
value?: StorybookConfig['docs']['autodocs'];
}
/**
* Set the docs.autodocs option to true if it isn't already set
*/
export const autodocsTrue: Fix<AutodocsTrueFrameworkRunOptions> = {
id: 'autodocsTrue',
async check({ packageManager, configDir }) {
const { mainConfig } = await getStorybookData({ packageManager, configDir });
const { docs } = mainConfig;
const docsPageToAutodocsMapping = {
true: 'tag' as const,
automatic: true,
false: false,
};
// @ts-expect-error docsPage does not exist anymore but we need to account for legacy code
if (docs?.docsPage) {
// @ts-expect-error same as above
const oldValue = docs?.docsPage.toString();
if (!(oldValue in docsPageToAutodocsMapping)) {
throw new Error(`Unexpected value for docs.docsPage: ${oldValue}`);
}
return {
value: docsPageToAutodocsMapping[oldValue as keyof typeof docsPageToAutodocsMapping],
};
}
return docs?.autodocs === undefined ? { value: true } : null;
},
prompt({ value }) {
const autodocsFormatted = chalk.cyan(`docs: { autodocs: ${JSON.stringify(value ?? true)} }`);
const tagWarning = dedent`
NOTE: if you're upgrading from an older 7.0-beta using the 'docsPage' tag,
please update your story files to use the 'autodocs' tag instead.
`;
if (value) {
return dedent`
We've changed the configuration of autodocs (previous docsPage), so now the value:
- docs.autodocs: true -- means automatically create docs for every CSF file
- docs.autodocs: 'tag' -- means only create autodocs for CSF files with the 'autodocs' tag
- docs.autodocs: false -- means never create autodocs
Based on your prior configuration, we can set the \`docs.autodocs\` to keep your old behaviour:
${autodocsFormatted}
${value === 'tag' ? tagWarning : ''}
More info: ${chalk.yellow(
'https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#autodocs-changes'
)}
`;
}
return dedent`
We've detected that your main.js configuration file has not configured autodocs. In 6.x we
we defaulted to having a autodocs for every story, in 7.x you need to opt in per-component.
However, we can set the \`docs.autodocs\` to true to approximate the old behaviour:
${autodocsFormatted}
More info: ${chalk.yellow(
'https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#autodocs-changes'
)}
`;
},
async run({ result: { value }, dryRun, mainConfigPath }) {
logger.info(`✅ Setting 'docs.autodocs' to true in main.js`);
if (!dryRun) {
await updateMainConfig({ mainConfigPath, dryRun }, async (main) => {
main.removeField(['docs', 'docsPage']);
main.setFieldValue(['docs', 'autodocs'], value ?? true);
});
}
},
};