/
getStorybookInfo.js
122 lines (108 loc) · 2.81 KB
/
getStorybookInfo.js
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
// Figure out the Storybook version and view layer
import fs from 'fs-extra';
const viewLayers = [
'react',
'vue',
'vue3',
'angular',
'html',
'web-components',
'polymer',
'ember',
'marko',
'mithril',
'riot',
'svelte',
'preact',
'rax',
];
const supportedAddons = [
'a11y',
'actions',
'backgrounds',
'centered',
'contexts',
'cssresources',
'design-assets',
'docs',
'events',
'google-analytics',
'graphql',
'info',
'jest',
'knobs',
'links',
'notes',
'ondevice-actions',
'ondevice-backgrounds',
'ondevice-knobs',
'ondevice-notes',
'options',
'queryparams',
'storyshots',
'storysource',
'viewport',
];
const resolve = (name) => {
try {
const path = require.resolve(`@storybook/${name}/package.json`, { paths: [process.cwd()] });
return Promise.resolve(path);
} catch (error) {
return Promise.reject(error);
}
};
const read = async (filepath) => JSON.parse(await fs.readFile(filepath, 'utf8'));
const timeout = (count) =>
new Promise((_, rej) => {
setTimeout(() => rej(new Error('The attempt to find the Storybook version timed out')), count);
});
const neverResolve = new Promise(() => {});
const disregard = () => neverResolve;
const findViewlayer = async ({ env }) => {
// Allow setting Storybook version via CHROMATIC_STORYBOOK_VERSION='react@4.0-alpha.8' for unusual cases
if (env.CHROMATIC_STORYBOOK_VERSION) {
const [viewLayer, version] = env.CHROMATIC_STORYBOOK_VERSION.split('@');
if (!viewLayer || !version) {
throw new Error('CHROMATIC_STORYBOOK_VERSION was provided but could not be used');
}
return { viewLayer, version };
}
// Try to find the Storybook viewlayer package
const findings = viewLayers.map((v) => resolve(v));
const rejectedFindings = findings.map((p) => p.then(disregard, () => true));
const allFailed = Promise.all(rejectedFindings).then(() => {
throw new Error(
'Could not find a supported Storybook viewlayer package. Make sure one is installed, or set CHROMATIC_STORYBOOK_VERSION.'
);
});
return Promise.race([
...findings.map((p, i) =>
p.then(
(l) => read(l).then((r) => ({ viewLayer: viewLayers[i], ...r })),
disregard // keep it pending forever
)
),
allFailed,
timeout(10000),
]);
};
const findAddons = async () => {
const result = await Promise.all(
supportedAddons.map((name) =>
resolve(`addon-${name}`)
.then((l) =>
read(l).then((r) => ({ name, packageName: r.name, packageVersion: r.version }))
)
.catch((e) => false)
)
);
return { addons: result.filter(Boolean) };
};
export default async function getStorybookInfo(ctx) {
const storybookInfo = await findViewlayer(ctx);
const addonInfo = await findAddons();
return {
...storybookInfo,
...addonInfo,
};
}