-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathindex.ts
57 lines (52 loc) · 1.57 KB
/
index.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
import path from 'path';
import { logger } from '@storybook/node-logger';
import { serverRequire } from '@storybook/core-common';
interface PresetOptions {
configDir: string;
docs?: boolean;
controls?: boolean;
actions?: boolean;
backgrounds?: boolean;
viewport?: boolean;
toolbars?: boolean;
measure?: boolean;
outline?: boolean;
}
const requireMain = (configDir: string) => {
const absoluteConfigDir = path.isAbsolute(configDir)
? configDir
: path.join(process.cwd(), configDir);
const mainFile = path.join(absoluteConfigDir, 'main');
return serverRequire(mainFile) ?? {};
};
export function addons(options: PresetOptions) {
const checkInstalled = (addonName: string, main: any) => {
const addon = `@storybook/addon-${addonName}`;
const existingAddon = main.addons?.find((entry: string | { name: string }) => {
const name = typeof entry === 'string' ? entry : entry.name;
return name?.startsWith(addon);
});
if (existingAddon) {
logger.info(`Found existing addon ${JSON.stringify(existingAddon)}, skipping.`);
}
return !!existingAddon;
};
const main = requireMain(options.configDir);
return [
'docs',
'controls',
'actions',
'backgrounds',
'viewport',
'toolbars',
'measure',
'outline',
'highlight',
]
.filter((key) => (options as any)[key] !== false)
.filter((addon) => !checkInstalled(addon, main))
.map((addon) => {
// We point to the re-export from addon-essentials to support yarn pnp and pnpm.
return `@storybook/addon-essentials/${addon}`;
});
}