-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
storybook-metadata.ts
217 lines (185 loc) · 6.12 KB
/
storybook-metadata.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import readPkgUp from 'read-pkg-up';
import { detect, getNpmVersion } from 'detect-package-manager';
import {
loadMainConfig,
getStorybookInfo,
getStorybookConfiguration,
getProjectRoot,
} from '@storybook/core-common';
import type { StorybookConfig, PackageJson } from '@storybook/core-common';
import type { StorybookMetadata, Dependency, StorybookAddon } from './types';
import { getActualPackageVersion, getActualPackageVersions } from './package-versions';
import { getMonorepoType } from './get-monorepo-type';
import { cleanPaths } from './sanitize';
export const metaFrameworks = {
next: 'Next',
'react-scripts': 'CRA',
gatsby: 'Gatsby',
'@nuxtjs/storybook': 'nuxt',
'@nrwl/storybook': 'nx',
'@vue/cli-service': 'vue-cli',
'@sveltejs/kit': 'svelte-kit',
} as Record<string, string>;
// @TODO: This should be removed in 7.0 as the framework.options field in main.js will replace this
const getFrameworkOptions = (mainConfig: any) => {
const possibleOptions = [
'angular',
'ember',
'html',
'preact',
'react',
'server',
'svelte',
'vue',
'vue3',
'webComponents',
].map((opt) => `${opt}Options`);
// eslint-disable-next-line no-restricted-syntax
for (const opt of possibleOptions) {
if (opt in mainConfig) {
return mainConfig[opt] as any;
}
}
return undefined;
};
export const sanitizeAddonName = (name: string) => {
return cleanPaths(name)
.replace(/\/dist\/.*/, '')
.replace(/\.[mc]?[tj]?s[x]?$/, '')
.replace(/\/register$/, '')
.replace(/\/manager$/, '')
.replace(/\/preset$/, '');
};
// Analyze a combination of information from main.js and package.json
// to provide telemetry over a Storybook project
export const computeStorybookMetadata = async ({
packageJson,
mainConfig,
}: {
packageJson: PackageJson;
mainConfig: StorybookConfig & Record<string, any>;
}): Promise<StorybookMetadata> => {
const metadata: Partial<StorybookMetadata> = {
generatedAt: new Date().getTime(),
builder: { name: 'webpack4' },
hasCustomBabel: false,
hasCustomWebpack: false,
hasStaticDirs: false,
hasStorybookEslint: false,
refCount: 0,
};
const allDependencies = {
...packageJson?.dependencies,
...packageJson?.devDependencies,
...packageJson?.peerDependencies,
};
const metaFramework = Object.keys(allDependencies).find((dep) => !!metaFrameworks[dep]);
if (metaFramework) {
const { version } = await getActualPackageVersion(metaFramework);
metadata.metaFramework = {
name: metaFrameworks[metaFramework],
packageName: metaFramework,
version,
};
}
const monorepoType = getMonorepoType();
if (monorepoType) {
metadata.monorepo = monorepoType;
}
try {
const packageManagerType = await detect({ cwd: getProjectRoot() });
const packageManagerVerson = await getNpmVersion(packageManagerType);
metadata.packageManager = {
type: packageManagerType,
version: packageManagerVerson,
};
// Better be safe than sorry, some codebases/paths might end up breaking with something like "spawn pnpm ENOENT"
// so we just set the package manager if the detection is successful
// eslint-disable-next-line no-empty
} catch (err) {}
metadata.hasCustomBabel = !!mainConfig.babel;
metadata.hasCustomWebpack = !!mainConfig.webpackFinal;
metadata.hasStaticDirs = !!mainConfig.staticDirs;
if (mainConfig.typescript) {
metadata.typescriptOptions = mainConfig.typescript;
}
if (mainConfig.core?.builder) {
const { builder } = mainConfig.core;
metadata.builder = {
name: typeof builder === 'string' ? builder : builder.name,
options: typeof builder === 'string' ? undefined : builder?.options ?? undefined,
};
}
if (mainConfig.refs) {
metadata.refCount = Object.keys(mainConfig.refs).length;
}
if (mainConfig.features) {
metadata.features = mainConfig.features;
}
const addons: Record<string, StorybookAddon> = {};
if (mainConfig.addons) {
mainConfig.addons.forEach((addon) => {
let addonName;
let options;
if (typeof addon === 'string') {
addonName = sanitizeAddonName(addon);
} else {
options = addon.options;
addonName = sanitizeAddonName(addon.name);
}
addons[addonName] = {
options,
version: undefined,
};
});
}
const addonVersions = await getActualPackageVersions(addons);
addonVersions.forEach(({ name, version }) => {
addons[name].version = version;
});
const addonNames = Object.keys(addons);
// all Storybook deps minus the addons
const storybookPackages = Object.keys(allDependencies)
.filter((dep) => dep.includes('storybook') && !addonNames.includes(dep))
.reduce((acc, dep) => {
return {
...acc,
[dep]: { version: undefined },
};
}, {}) as Record<string, Dependency>;
const storybookPackageVersions = await getActualPackageVersions(storybookPackages);
storybookPackageVersions.forEach(({ name, version }) => {
storybookPackages[name].version = version;
});
const language = allDependencies.typescript ? 'typescript' : 'javascript';
const hasStorybookEslint = !!allDependencies['eslint-plugin-storybook'];
const storybookInfo = getStorybookInfo(packageJson);
const storybookVersion =
storybookPackages[storybookInfo.frameworkPackage]?.version || storybookInfo.version;
return {
...metadata,
storybookVersion,
language,
storybookPackages,
framework: {
name: storybookInfo.framework,
options: getFrameworkOptions(mainConfig),
},
addons,
hasStorybookEslint,
};
};
let cachedMetadata: StorybookMetadata;
export const getStorybookMetadata = async (_configDir: string) => {
if (cachedMetadata) {
return cachedMetadata;
}
const packageJson = readPkgUp.sync({ cwd: process.cwd() }).packageJson as PackageJson;
const configDir =
(_configDir ||
(getStorybookConfiguration(packageJson.scripts.storybook, '-c', '--config-dir') as string)) ??
'.storybook';
const mainConfig = loadMainConfig({ configDir });
cachedMetadata = await computeStorybookMetadata({ mainConfig, packageJson });
return cachedMetadata;
};