-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
repro-next.ts
190 lines (157 loc) · 5.51 KB
/
repro-next.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
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 } from 'fs-extra';
import { allTemplates as TEMPLATES } from './repro-templates';
const logger = console;
interface ReproOptions {
filterValue?: string;
output?: string;
branch?: string;
init?: boolean;
}
type Choice = keyof typeof TEMPLATES;
const toChoices = (c: Choice): prompts.Choice => ({ title: TEMPLATES[c].name, value: c });
export const reproNext = async ({
output: outputDirectory,
filterValue,
branch,
init,
}: ReproOptions) => {
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];
const filterRegex = new RegExp(filterValue, 'i');
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);
}
let selectedTemplate: Choice | null = null;
if (choices.length === 1) {
[selectedTemplate] = choices;
} else {
logger.info(
boxen(
dedent`
🤗 Welcome to ${chalk.yellow('sb repro NEXT')}! 🤗
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
)
);
selectedTemplate = await promptSelectedTemplate(choices);
}
const hasSelectedTemplate = !!(selectedTemplate ?? null);
if (!hasSelectedTemplate) {
logger.error('Somehow we got no templates. Please rerun this command!');
return;
}
const selectedConfig = TEMPLATES[selectedTemplate];
if (!selectedConfig) {
throw new Error('🚨 Repro: please specify a valid template type');
}
let selectedDirectory = outputDirectory;
const outputDirectoryName = outputDirectory || selectedTemplate;
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,
validate: async (directoryName) =>
existsSync(directoryName)
? `${directoryName} already exists. Please choose another name.`
: true,
});
selectedDirectory = directory;
}
try {
const templateDestination = path.isAbsolute(selectedDirectory)
? selectedDirectory
: path.join(process.cwd(), selectedDirectory);
logger.info(`🏃 Adding ${selectedConfig.name} into ${templateDestination}`);
logger.log('📦 Downloading repro template...');
try {
const templateType = init ? 'after-storybook' : 'before-storybook';
// Download the repro based on subfolder "after-storybook" and selected branch
await downloadTemplate(
`github:storybookjs/repro-templates-temp/${selectedTemplate}/${templateType}#${branch}`,
{
force: true,
dir: templateDestination,
}
);
} catch (err) {
logger.error(`🚨 Failed to download repro template: ${err.message}`);
throw err;
}
const initMessage = init
? chalk.yellow(`yarn install\nyarn storybook`)
: `Recreate your setup, then ${chalk.yellow(`run npx storybook 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 repro');
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),
});
return template || null;
}