forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-package.js
161 lines (141 loc) · 4.57 KB
/
build-package.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
#!/usr/bin/env node
/* eslint-disable global-require */
const { resolve } = require('path');
const terminalSize = require('window-size');
const { checkDependenciesAndRun, spawn } = require('./utils/cli-utils');
const getStorybookPackages = () => {
const listCommand = spawn(`lerna list`, {
stdio: 'pipe',
});
const packages = listCommand.output
.toString()
.match(/@storybook\/(.)*/g)
.sort();
return packages;
};
function run() {
const prompts = require('prompts');
const program = require('commander');
const chalk = require('chalk');
const log = require('npmlog');
log.heading = 'storybook';
const prefix = 'build';
log.addLevel('aborted', 3001, { fg: 'red', bold: true });
const packages = getStorybookPackages();
const packageTasks = packages
.map((package) => {
return {
name: package,
suffix: package.replace('@storybook/', ''),
defaultValue: false,
helpText: `build only the ${package} package`,
};
})
.reduce((acc, next) => {
acc[next.name] = next;
return acc;
}, {});
const tasks = {
watch: {
name: `watch`,
defaultValue: false,
suffix: '--watch',
helpText: 'build on watch mode',
},
...packageTasks,
};
const main = program.version('5.0.0').option('--all', `build everything ${chalk.gray('(all)')}`);
Object.keys(tasks)
.reduce((acc, key) => acc.option(tasks[key].suffix, tasks[key].helpText), main)
.parse(process.argv);
Object.keys(tasks).forEach((key) => {
// checks if a flag is passed e.g. yarn build --@storybook/addon-docs --watch
const containsFlag = program.rawArgs.includes(tasks[key].suffix);
tasks[key].value = containsFlag || program.all;
});
let selection;
let watchMode = false;
if (
!Object.keys(tasks)
.map((key) => tasks[key].value)
.filter(Boolean).length
) {
selection = prompts([
{
type: 'toggle',
name: 'mode',
message: 'Start in watch mode',
initial: false,
active: 'yes',
inactive: 'no',
},
{
type: 'autocompleteMultiselect',
message: 'Select the packages to build',
name: 'todo',
hint:
'You can also run directly with package name like `yarn build core`, or `yarn build --all` for all packages!',
optionsPerPage: terminalSize.height - 3, // 3 lines for extra info
choices: packages.map((key) => ({
value: key,
title: tasks[key].name || key,
selected: (tasks[key] && tasks[key].defaultValue) || false,
})),
},
]).then(({ mode, todo }) => {
watchMode = mode;
return todo.map((key) => tasks[key]);
});
} else {
// hits here when running yarn build --packagename
watchMode = process.argv.includes('--watch');
selection = Promise.resolve(
Object.keys(tasks)
.map((key) => tasks[key])
.filter((item) => item.name !== 'watch' && item.value === true)
);
}
selection
.then((list) => {
if (list.length === 0) {
log.warn(prefix, 'Nothing to build!');
} else {
const packageNames = list
// filters out watch command if --watch is used
.map((key) => key.suffix)
.filter(Boolean);
let glob =
packageNames.length > 1
? `@storybook/{${packageNames.join(',')}}`
: `@storybook/${packageNames[0]}`;
const isAllPackages = process.argv.includes('--all');
if (isAllPackages) {
glob = '@storybook/*';
log.warn(
'You are building a lot of packages on watch mode. This is an expensive action and might slow your computer down.\nIf this is an issue, run yarn build to filter packages and speed things up!'
);
}
if (watchMode) {
const runWatchMode = () => {
const baseWatchCommand = `lerna exec --scope '${glob}' --parallel -- cross-env-shell node ${resolve(
__dirname
)}`;
const watchTsc = `${baseWatchCommand}/utils/watch-tsc.js`;
const watchBabel = `${baseWatchCommand}/utils/watch-babel.js`;
const command = `concurrently --kill-others-on-fail "${watchTsc}" "${watchBabel}"`;
spawn(command);
};
runWatchMode();
} else {
spawn(`lerna run prepare --scope "${glob}"`);
}
process.stdout.write('\x07');
}
})
.catch((e) => {
log.aborted(prefix, chalk.red(e.message));
log.silly(prefix, e);
process.exit(1);
});
}
checkDependenciesAndRun(run);