/
configure.ts
142 lines (116 loc) · 3.89 KB
/
configure.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import fs from 'fs';
import path from 'path';
import {
toRequireContext,
StoriesEntry,
normalizeStoriesEntry,
NormalizedStoriesSpecifier,
} from '@storybook/core-common';
import registerRequireContextHook from '@storybook/babel-plugin-require-context-hook/register';
import global from 'global';
import { AnyFramework, ArgsEnhancer, ArgTypesEnhancer, DecoratorFunction } from '@storybook/csf';
import { ClientApi } from './Loader';
import { StoryshotsOptions } from '../api/StoryshotsOptions';
registerRequireContextHook();
const isFile = (file: string): boolean => {
try {
return fs.lstatSync(file).isFile();
} catch (e) {
return false;
}
};
interface Output {
features?: Record<string, boolean>;
preview?: string;
stories?: NormalizedStoriesSpecifier[];
requireContexts?: string[];
}
const supportedExtensions = ['ts', 'tsx', 'js', 'jsx'];
const resolveFile = (configDir: string, supportedFilenames: string[]) =>
supportedFilenames
.flatMap((filename) =>
supportedExtensions.map((ext) => path.join(configDir, `${filename}.${ext}`))
)
.find(isFile) || false;
export const getPreviewFile = (configDir: string): string | false =>
resolveFile(configDir, ['preview', 'config']);
export const getMainFile = (configDir: string): string | false => resolveFile(configDir, ['main']);
function getConfigPathParts(input: string): Output {
const configDir = path.resolve(input);
if (fs.lstatSync(configDir).isDirectory()) {
const output: Output = {};
const preview = getPreviewFile(configDir);
const main = getMainFile(configDir);
if (preview) {
output.preview = preview;
}
if (main) {
const { stories = [], features = {} } = jest.requireActual(main);
output.features = features;
const workingDir = process.cwd();
output.stories = stories.map((entry: StoriesEntry) => {
const specifier = normalizeStoriesEntry(entry, {
configDir,
workingDir,
});
return specifier;
});
output.requireContexts = output.stories.map((specifier) => {
const { path: basePath, recursive, match } = toRequireContext(specifier);
// eslint-disable-next-line no-underscore-dangle
return global.__requireContext(workingDir, basePath, recursive, match);
});
}
return output;
}
return { preview: configDir };
}
function configure<TFramework extends AnyFramework>(
options: {
storybook: ClientApi<TFramework>;
} & StoryshotsOptions
): void {
const { configPath = '.storybook', config, storybook } = options;
if (config && typeof config === 'function') {
config(storybook);
return;
}
const {
preview,
features = {},
stories = [],
requireContexts = [],
} = getConfigPathParts(configPath);
global.FEATURES = features;
global.STORIES = stories.map((specifier) => ({
...specifier,
importPathMatcher: specifier.importPathMatcher.source,
}));
if (preview) {
// This is essentially the same code as lib/core/src/server/preview/virtualModuleEntry.template
const { parameters, decorators, globals, globalTypes, argsEnhancers, argTypesEnhancers } =
jest.requireActual(preview);
if (decorators) {
decorators.forEach((decorator: DecoratorFunction<TFramework>) =>
storybook.addDecorator(decorator)
);
}
if (parameters || globals || globalTypes) {
storybook.addParameters({ ...parameters, globals, globalTypes });
}
if (argsEnhancers) {
argsEnhancers.forEach((enhancer: ArgsEnhancer<TFramework>) =>
storybook.addArgsEnhancer(enhancer as any)
);
}
if (argTypesEnhancers) {
argTypesEnhancers.forEach((enhancer: ArgTypesEnhancer<TFramework>) =>
storybook.addArgTypesEnhancer(enhancer as any)
);
}
}
if (requireContexts && requireContexts.length) {
storybook.configure(requireContexts, false, false);
}
}
export default configure;