UNPKG

2.19 kBJavaScriptView Raw
1import path, { dirname, join } from 'path';
2import { logger } from '@storybook/node-logger';
3import { serverRequire } from '@storybook/core-common';
4
5const requireMain = configDir => {
6 var _serverRequire;
7
8 const absoluteConfigDir = path.isAbsolute(configDir) ? configDir : path.join(process.cwd(), configDir);
9 const mainFile = path.join(absoluteConfigDir, 'main');
10 return (_serverRequire = serverRequire(mainFile)) !== null && _serverRequire !== void 0 ? _serverRequire : {};
11};
12
13export function addons(options = {}) {
14 const checkInstalled = (addon, main) => {
15 var _main$addons;
16
17 const existingAddon = (_main$addons = main.addons) === null || _main$addons === void 0 ? void 0 : _main$addons.find(entry => {
18 const name = typeof entry === 'string' ? entry : entry.name;
19 return name === null || name === void 0 ? void 0 : name.startsWith(addon);
20 });
21
22 if (existingAddon) {
23 logger.info(`Found existing addon ${JSON.stringify(existingAddon)}, skipping.`);
24 }
25
26 return !!existingAddon;
27 };
28
29 const main = requireMain(options.configDir);
30 return ['docs', 'controls', 'actions', 'backgrounds', 'viewport', 'toolbars', 'measure', 'outline'].filter(key => options[key] !== false).map(key => `@storybook/addon-${key}`).filter(addon => !checkInstalled(addon, main)) // Use `require.resolve` to ensure Yarn PnP compatibility
31 // Files of various addons should be resolved in the context of `addon-essentials` as they are listed as deps here
32 // and not in `@storybook/core` nor in SB user projects. If `@storybook/core` make the require itself Yarn 2 will
33 // throw an error saying that the package to require must be added as a dependency. Doing `require.resolve` will
34 // allow `@storybook/core` to work with absolute path directly, no more require of dep no more issue.
35 // File to load can be `preset.js`, `register.js`, or the package entry point, so we need to check all these cases
36 // as it's done in `lib/core/src/server/presets.js`.
37 .map(addon => {
38 try {
39 return dirname(require.resolve(join(addon, 'package.json'))); // eslint-disable-next-line no-empty
40 } catch (err) {}
41
42 return require.resolve(addon);
43 });
44}
\No newline at end of file