forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandbox.ts
287 lines (246 loc) · 9.63 KB
/
sandbox.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import prompts from 'prompts';
import path from 'path';
import chalk from 'chalk';
import boxen from 'boxen';
import { dedent } from 'ts-dedent';
import { downloadTemplate } from 'giget';
import { existsSync, readdir } from 'fs-extra';
import invariant from 'tiny-invariant';
import { lt, prerelease } from 'semver';
import type { Template, TemplateKey } from './sandbox-templates';
import { allTemplates as TEMPLATES } from './sandbox-templates';
import type { PackageManagerName } from '@storybook/core-common';
import { JsPackageManagerFactory } from '@storybook/core-common';
import { versions } from '@storybook/core-common';
import { doInitiate } from './initiate';
const logger = console;
interface SandboxOptions {
filterValue?: string;
output?: string;
branch?: string;
init?: boolean;
packageManager: PackageManagerName;
}
type Choice = keyof typeof TEMPLATES;
const toChoices = (c: Choice): prompts.Choice => ({ title: TEMPLATES[c].name, value: c });
export const sandbox = async ({
output: outputDirectory,
filterValue,
init,
...options
}: SandboxOptions) => {
// Either get a direct match when users pass a template id, or filter through all templates
let selectedConfig: Template | undefined = TEMPLATES[filterValue as TemplateKey];
let templateId: Choice | null = selectedConfig ? (filterValue as TemplateKey) : null;
const { packageManager: pkgMgr } = options;
const packageManager = JsPackageManagerFactory.getPackageManager({
force: pkgMgr,
});
const latestVersion = await packageManager.latestVersion('@storybook/cli');
const nextVersion = await packageManager
.latestVersion('@storybook/cli@next')
.catch((e) => '0.0.0');
const currentVersion = versions['@storybook/cli'];
const isPrerelease = prerelease(currentVersion);
const isOutdated = lt(currentVersion, isPrerelease ? nextVersion : latestVersion);
const borderColor = isOutdated ? '#FC521F' : '#F1618C';
const downloadType = !isOutdated && init ? 'after-storybook' : 'before-storybook';
const branch = isPrerelease ? 'next' : 'main';
const messages = {
welcome: `Creating a Storybook ${chalk.bold(currentVersion)} sandbox..`,
notLatest: chalk.red(dedent`
This version is behind the latest release, which is: ${chalk.bold(latestVersion)}!
You likely ran the init command through npx, which can use a locally cached version, to get the latest please run:
${chalk.bold('npx storybook@latest sandbox')}
You may want to CTRL+C to stop, and run with the latest version instead.
`),
longInitTime: chalk.yellow(
'The creation of the sandbox will take longer, because we will need to run init.'
),
prerelease: chalk.yellow('This is a pre-release version.'),
};
logger.log(
boxen(
[messages.welcome]
.concat(isOutdated && !isPrerelease ? [messages.notLatest] : [])
.concat(init && (isOutdated || isPrerelease) ? [messages.longInitTime] : [])
.concat(isPrerelease ? [messages.prerelease] : [])
.join('\n'),
{ borderStyle: 'round', padding: 1, borderColor }
)
);
if (!selectedConfig) {
const filterRegex = new RegExp(`^${filterValue || ''}`, 'i');
const keys = Object.keys(TEMPLATES) as Choice[];
// get value from template and reduce through TEMPLATES to filter out the correct template
const choices = keys.reduce<Choice[]>((acc, group) => {
const current = TEMPLATES[group];
if (!filterValue) {
acc.push(group);
return acc;
}
if (
current.name.match(filterRegex) ||
group.match(filterRegex) ||
current.expected.builder.match(filterRegex) ||
current.expected.framework.match(filterRegex) ||
current.expected.renderer.match(filterRegex)
) {
acc.push(group);
return acc;
}
return acc;
}, []);
if (choices.length === 0) {
logger.info(
boxen(
dedent`
🔎 You filtered out all templates. 🔍
After filtering all the templates with "${chalk.yellow(
filterValue
)}", we found no results. Please try again with a different filter.
Available templates:
${keys.map((key) => chalk.blue`- ${key}`).join('\n')}
`.trim(),
{ borderStyle: 'round', padding: 1, borderColor: '#F1618C' } as any
)
);
process.exit(1);
}
if (choices.length === 1) {
[templateId] = choices;
} else {
logger.info(
boxen(
dedent`
🤗 Welcome to ${chalk.yellow('sb sandbox')}! 🤗
Create a ${chalk.green('new project')} to minimally reproduce Storybook issues.
1. select an environment that most closely matches your project setup.
2. select a location for the reproduction, outside of your project.
After the reproduction is ready, we'll guide you through the next steps.
`.trim(),
{ borderStyle: 'round', padding: 1, borderColor: '#F1618C' } as any
)
);
templateId = await promptSelectedTemplate(choices);
}
const hasSelectedTemplate = !!(templateId ?? null);
if (!hasSelectedTemplate) {
logger.error('Somehow we got no templates. Please rerun this command!');
return;
}
selectedConfig = templateId ? TEMPLATES[templateId] : undefined;
if (!selectedConfig) {
throw new Error('🚨 Sandbox: please specify a valid template type');
}
}
let selectedDirectory = outputDirectory;
const outputDirectoryName = outputDirectory || templateId;
if (selectedDirectory && existsSync(`${selectedDirectory}`)) {
logger.info(`⚠️ ${selectedDirectory} already exists! Overwriting...`);
}
if (!selectedDirectory) {
const { directory } = await prompts(
{
type: 'text',
message: 'Enter the output directory',
name: 'directory',
initial: outputDirectoryName ?? undefined,
validate: async (directoryName) =>
existsSync(directoryName)
? `${directoryName} already exists. Please choose another name.`
: true,
},
{
onCancel: () => {
logger.log('Command cancelled by the user. Exiting...');
process.exit(1);
},
}
);
selectedDirectory = directory;
}
invariant(selectedDirectory);
try {
const templateDestination = path.isAbsolute(selectedDirectory)
? selectedDirectory
: path.join(process.cwd(), selectedDirectory);
logger.info(`🏃 Adding ${selectedConfig.name} into ${templateDestination}`);
logger.log(`📦 Downloading sandbox template (${chalk.bold(downloadType)})...`);
try {
// Download the sandbox based on subfolder "after-storybook" and selected branch
const gitPath = `github:storybookjs/sandboxes/${templateId}/${downloadType}#${branch}`;
await downloadTemplate(gitPath, {
force: true,
dir: templateDestination,
});
// throw an error if templateDestination is an empty directory using fs-extra
if ((await readdir(templateDestination)).length === 0) {
const selected = chalk.yellow(templateId);
throw new Error(dedent`
Template downloaded from ${chalk.blue(gitPath)} is empty.
Are you use it exists? Or did you want to set ${selected} to inDevelopment first?
`);
}
// when user wanted an sandbox that has been initiated, but force-downloaded the before-storybook directory
// then we need to initiate the sandbox
// this is to ensure we DO get the latest version of the template (output of the generator), but we initialize using the version of storybook that the CLI is.
// we warned the user about the fact they are running an old version of storybook
// we warned the user the sandbox step would take longer
if (downloadType === 'before-storybook' && init) {
const before = process.cwd();
process.chdir(templateDestination);
// we run doInitiate, instead of initiate, to avoid sending this init event to telemetry, because it's not a real world project
await doInitiate({
dev: process.env.CI !== 'true' && process.env.IN_STORYBOOK_SANBOX !== 'true',
...options,
});
process.chdir(before);
}
} catch (err) {
logger.error(`🚨 Failed to download sandbox template: ${String(err)}`);
throw err;
}
const initMessage = init
? chalk.yellow(dedent`
yarn install
yarn storybook
`)
: `Recreate your setup, then ${chalk.yellow(`npx storybook@latest init`)}`;
logger.info(
boxen(
dedent`
🎉 Your Storybook reproduction project is ready to use! 🎉
${chalk.yellow(`cd ${selectedDirectory}`)}
${initMessage}
Once you've recreated the problem you're experiencing, please:
1. Document any additional steps in ${chalk.cyan('README.md')}
2. Publish the repository to github
3. Link to the repro repository in your issue
Having a clean repro helps us solve your issue faster! 🙏
`.trim(),
{ borderStyle: 'round', padding: 1, borderColor: '#F1618C' } as any
)
);
} catch (error) {
logger.error('🚨 Failed to create sandbox');
throw error;
}
};
async function promptSelectedTemplate(choices: Choice[]): Promise<Choice | null> {
const { template } = await prompts(
{
type: 'select',
message: '🌈 Select the template',
name: 'template',
choices: choices.map(toChoices),
},
{
onCancel: () => {
logger.log('Command cancelled by the user. Exiting...');
process.exit(1);
},
}
);
return template || null;
}