forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.js
executable file
·282 lines (261 loc) · 7.05 KB
/
bootstrap.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
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
#!/usr/bin/env node
/* eslint-disable global-require, no-octal-escape */
const childProcess = require('child_process');
const { lstatSync, readdirSync } = require('fs');
const { join } = require('path');
const logger = console;
let cooldown = 0;
try {
require('inquirer');
require('commander');
require('chalk');
require('npmlog');
} catch (e) {
logger.log('🕘 running bootstrap on a clean repo, we have to install dependencies');
childProcess.spawnSync('yarn', ['install', '--ignore-optional'], {
stdio: ['inherit', 'inherit', 'inherit'],
});
process.stdout.write('\x07');
process.stdout.write('\033c');
// give the filesystem some time
cooldown = 1000;
} finally {
setTimeout(run, cooldown);
}
function run() {
const inquirer = require('inquirer');
const program = require('commander');
const chalk = require('chalk');
const log = require('npmlog');
const isTgz = source => lstatSync(source).isFile() && source.match(/.tgz$/);
const getDirectories = source =>
readdirSync(source)
.map(name => join(source, name))
.filter(isTgz);
log.heading = 'storybook';
const prefix = 'bootstrap';
log.addLevel('aborted', 3001, { fg: 'red', bold: true });
const spawn = (command, options = {}) => {
const out = childProcess.spawnSync(`${command}`, {
shell: true,
stdio: 'inherit',
...options,
});
if (out.status !== 0) {
process.exit(out.status);
}
return out;
};
const main = program
.version('5.0.0')
.option('--all', `Bootstrap everything ${chalk.gray('(all)')}`);
const createTask = ({
defaultValue,
option,
name,
check = () => true,
command,
pre = [],
order,
}) => ({
value: false,
defaultValue: defaultValue || false,
option: option || undefined,
name: name || 'unnamed task',
check: check || (() => true),
order,
command: () => {
// run all pre tasks
pre
.map(key => tasks[key])
.forEach(task => {
if (task.check()) {
task.command();
}
});
log.info(prefix, name);
command();
},
});
const tasks = {
core: createTask({
name: `Core, Dll & Examples ${chalk.gray('(core)')}`,
defaultValue: true,
option: '--core',
command: () => {
log.info(prefix, 'yarn workspace');
},
pre: ['install', 'build', 'dll'],
order: 1,
}),
reset: createTask({
name: `Clean repository ${chalk.red('(reset)')}`,
defaultValue: false,
option: '--reset',
command: () => {
log.info(prefix, 'git clean');
spawn('node -r esm ./scripts/reset.js');
},
order: 0,
}),
install: createTask({
name: `Install dependencies ${chalk.gray('(install)')}`,
defaultValue: false,
option: '--install',
command: () => {
spawn('yarn install --ignore-optional --network-concurrency 8');
},
order: 1,
}),
build: createTask({
name: `Build packages ${chalk.gray('(build)')}`,
defaultValue: false,
option: '--build',
command: () => {
log.info(prefix, 'prepare');
spawn('lerna run prepare');
},
order: 2,
}),
dll: createTask({
name: `Generate DLL ${chalk.gray('(dll)')}`,
defaultValue: false,
option: '--dll',
command: () => {
log.info(prefix, 'dll');
setTimeout(() => {
spawn('lerna run createDlls --scope "@storybook/ui"');
}, 5000);
},
order: 3,
}),
docs: createTask({
name: `Documentation ${chalk.gray('(docs)')}`,
defaultValue: false,
option: '--docs',
command: () => {
spawn('yarn bootstrap:docs');
},
order: 6,
}),
packs: createTask({
name: `Build tarballs of packages ${chalk.gray('(build-packs)')}`,
defaultValue: false,
option: '--packs',
command: () => {
spawn('yarn build-packs');
},
check: () => getDirectories(join(__dirname, '..', 'packs')).length === 0,
order: 5,
}),
registry: createTask({
name: `Run local registry ${chalk.gray('(reg)')}`,
defaultValue: false,
option: '--reg',
command: () => {
spawn('./scripts/run-registry.js');
},
order: 11,
}),
dev: createTask({
name: `Run build in watch mode ${chalk.gray('(dev)')}`,
defaultValue: false,
option: '--dev',
command: () => {
spawn('yarn dev');
},
order: 9,
}),
};
const groups = {
main: ['core', 'docs'],
buildtasks: ['install', 'build', 'dll', 'packs'],
devtasks: ['dev', 'registry', 'reset'],
};
Object.keys(tasks)
.reduce((acc, key) => acc.option(tasks[key].option, tasks[key].name), main)
.parse(process.argv);
Object.keys(tasks).forEach(key => {
tasks[key].value = program[tasks[key].option.replace('--', '')] || program.all;
});
const createSeperator = input => `- ${input}${' ---------'.substr(0, 12)}`;
const choices = Object.values(groups)
.map(l =>
l.map(key => ({
name: tasks[key].name,
checked: tasks[key].defaultValue,
}))
)
.reduce(
(acc, i, k) =>
acc.concat(new inquirer.Separator(createSeperator(Object.keys(groups)[k]))).concat(i),
[]
);
let selection;
if (
!Object.keys(tasks)
.map(key => tasks[key].value)
.filter(Boolean).length
) {
selection = inquirer
.prompt([
{
type: 'checkbox',
message: 'Select the bootstrap activities',
name: 'todo',
pageSize: Object.keys(tasks).length + Object.keys(groups).length,
choices,
},
])
.then(({ todo }) =>
todo.map(name => tasks[Object.keys(tasks).find(i => tasks[i].name === name)])
)
.then(list => {
if (list.find(i => i === tasks.reset)) {
return inquirer
.prompt([
{
type: 'confirm',
message: `${chalk.red(
'DESTRUCTIVE'
)} deletes node_modules, files not present in git ${chalk.underline(
'will get trashed'
)}, except for .idea and .vscode, ${chalk.cyan('Continue?')}`,
name: 'sure',
},
])
.then(({ sure }) => {
if (sure) {
return list;
}
throw new Error('problem is between keyboard and chair');
});
}
return list;
});
} else {
selection = Promise.resolve(
Object.keys(tasks)
.map(key => tasks[key])
.filter(item => item.value === true)
);
}
selection
.then(list => {
if (list.length === 0) {
log.warn(prefix, 'Nothing to bootstrap');
} else {
list
.sort((a, b) => a.order - b.order)
.forEach(key => {
key.command();
});
process.stdout.write('\x07');
}
})
.catch(e => {
log.aborted(prefix, chalk.red(e.message));
log.silly(prefix, e);
process.exit(1);
});
}