forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-package.ts
172 lines (160 loc) · 4.67 KB
/
build-package.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
import { program } from 'commander';
// eslint-disable-next-line depend/ban-dependencies
import { execaCommand } from 'execa';
// eslint-disable-next-line depend/ban-dependencies
import { readJSON } from 'fs-extra';
import { posix, resolve, sep } from 'path';
import picocolors from 'picocolors';
import prompts from 'prompts';
import windowSize from 'window-size';
import { getWorkspaces } from './utils/workspace';
async function run() {
const packages = (await getWorkspaces()).filter(({ name }) => name !== '@storybook/root');
const packageTasks = packages
.map((pkg) => {
let suffix = pkg.name.replace('@storybook/', '');
if (pkg.name === '@storybook/cli') {
suffix = 'sb-cli';
}
if (pkg.name === 'storybook') {
suffix = 'cli';
}
return {
...pkg,
suffix,
defaultValue: false,
helpText: `build only the ${pkg.name} package`,
};
})
.reduce(
(acc, next) => {
acc[next.name] = next;
return acc;
},
{} as Record<
string,
{ name: string; defaultValue: boolean; suffix: string; helpText: string }
>
);
const tasks: Record<
string,
{
name: string;
defaultValue: boolean;
suffix: string;
helpText: string;
value?: any;
location?: string;
}
> = {
watch: {
name: `watch`,
defaultValue: false,
suffix: '--watch',
helpText: 'build on watch mode',
},
prod: {
name: `prod`,
defaultValue: false,
suffix: '--prod',
helpText: 'build on production mode',
},
...packageTasks,
};
const main = program
.version('5.0.0')
.option('--all', `build everything ${picocolors.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) => {
const opts = program.opts();
// checks if a flag is passed e.g. yarn build --@storybook/addon-docs --watch
const containsFlag = program.args.includes(tasks[key].suffix);
tasks[key].value = containsFlag || opts.all;
});
let selection;
let watchMode = false;
let prodMode = false;
if (
!Object.keys(tasks)
.map((key) => tasks[key].value)
.filter(Boolean).length
) {
selection = await prompts([
{
type: 'toggle',
name: 'watch',
message: 'Start in watch mode',
initial: false,
active: 'yes',
inactive: 'no',
},
{
type: 'toggle',
name: 'prod',
message: 'Start in production mode',
initial: false,
active: 'yes',
inactive: 'no',
},
{
type: 'autocompleteMultiselect',
message: 'Select the packages to build',
name: 'todo',
min: 1,
hint: 'You can also run directly with package name like `yarn build core`, or `yarn build --all` for all packages!',
// @ts-expect-error @types incomplete
optionsPerPage: windowSize.height - 3, // 3 lines for extra info
choices: packages.map(({ name: key }) => ({
value: key,
title: tasks[key].name || key,
selected: (tasks[key] && tasks[key].defaultValue) || false,
})),
},
]).then(({ watch, prod, todo }: { watch: boolean; prod: boolean; todo: Array<string> }) => {
watchMode = watch;
prodMode = prod;
return todo?.map((key) => tasks[key]);
});
} else {
// hits here when running yarn build --packagename
watchMode = process.argv.includes('--watch');
prodMode = process.argv.includes('--prod');
selection = Object.keys(tasks)
.map((key) => tasks[key])
.filter((item) => !['watch', 'prod'].includes(item.name) && item.value === true);
}
selection?.filter(Boolean).forEach(async (v) => {
const command = (await readJSON(resolve('../code', v.location, 'package.json'))).scripts?.prep
.split(posix.sep)
.join(sep);
if (!command) {
console.log(`No prep script found for ${v.name}`);
return;
}
const cwd = resolve(__dirname, '..', 'code', v.location);
const sub = execaCommand(
`${command}${watchMode ? ' --watch' : ''}${prodMode ? ' --optimized' : ''}`,
{
cwd,
buffer: false,
shell: true,
cleanup: true,
env: {
NODE_ENV: 'production',
},
}
);
sub.stdout?.on('data', (data) => {
process.stdout.write(`${picocolors.cyan(v.name)}:\n${data}`);
});
sub.stderr?.on('data', (data) => {
process.stderr.write(`${picocolors.red(v.name)}:\n${data}`);
});
});
}
run().catch((e) => {
console.log(e);
process.exit(1);
});