forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuild-all-release-channels.js
183 lines (161 loc) · 6.51 KB
/
build-all-release-channels.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
'use strict';
/* eslint-disable no-for-of-loops/no-for-of-loops */
const fs = require('fs');
const {spawnSync} = require('child_process');
const path = require('path');
const tmp = require('tmp');
// Runs the build script for both stable and experimental release channels,
// by configuring an environment variable.
const sha = (
spawnSync('git', ['show', '-s', '--format=%h']).stdout + ''
).trim();
const ReactVersion = JSON.parse(fs.readFileSync('packages/react/package.json'))
.version;
if (process.env.CIRCLE_NODE_TOTAL) {
// In CI, we use multiple concurrent processes. Allocate half the processes to
// build the stable channel, and the other half for experimental. Override
// the environment variables to "trick" the underlying build script.
const total = parseInt(process.env.CIRCLE_NODE_TOTAL, 10);
const halfTotal = Math.floor(total / 2);
const index = parseInt(process.env.CIRCLE_NODE_INDEX, 10);
if (index < halfTotal) {
const nodeTotal = halfTotal;
const nodeIndex = index;
const version = '0.0.0-' + sha;
updateTheReactVersionThatDevToolsReads(ReactVersion + '-' + sha);
buildForChannel('stable', nodeTotal, nodeIndex);
processStable('./build', version);
} else {
const nodeTotal = total - halfTotal;
const nodeIndex = index - halfTotal;
const version = '0.0.0-experimental-' + sha;
updateTheReactVersionThatDevToolsReads(
ReactVersion + '-experimental-' + sha
);
buildForChannel('experimental', nodeTotal, nodeIndex);
processExperimental('./build', version);
}
// TODO: Currently storing artifacts as `./build2` so that it doesn't conflict
// with old build job. Remove once we migrate rest of build/test pipeline.
fs.renameSync('./build', './build2');
} else {
// Running locally, no concurrency. Move each channel's build artifacts into
// a temporary directory so that they don't conflict.
const stableVersion = '0.0.0-' + sha;
buildForChannel('stable', '', '');
const stableDir = tmp.dirSync().name;
fs.renameSync('./build', stableDir);
processStable(stableDir, stableVersion);
const experimentalVersion = '0.0.0-experimental-' + sha;
buildForChannel('experimental', '', '');
const experimentalDir = tmp.dirSync().name;
fs.renameSync('./build', experimentalDir);
processExperimental(experimentalDir, experimentalVersion);
// Then merge the experimental folder into the stable one. processExperimental
// will have already removed conflicting files.
//
// In CI, merging is handled automatically by CircleCI's workspace feature.
spawnSync('rsync', ['-ar', experimentalDir + '/', stableDir + '/']);
// Now restore the combined directory back to its original name
// TODO: Currently storing artifacts as `./build2` so that it doesn't conflict
// with old build job. Remove once we migrate rest of build/test pipeline.
fs.renameSync(stableDir, './build2');
}
function buildForChannel(channel, nodeTotal, nodeIndex) {
spawnSync('node', ['./scripts/rollup/build.js', ...process.argv.slice(2)], {
stdio: ['pipe', process.stdout, process.stderr],
env: {
...process.env,
RELEASE_CHANNEL: channel,
CIRCLE_NODE_TOTAL: nodeTotal,
CIRCLE_NODE_INDEX: nodeIndex,
},
});
}
function processStable(buildDir, version) {
if (fs.existsSync(buildDir + '/node_modules')) {
updatePackageVersions(buildDir + '/node_modules', version);
fs.renameSync(buildDir + '/node_modules', buildDir + '/oss-stable');
}
if (fs.existsSync(buildDir + '/facebook-www')) {
for (const fileName of fs.readdirSync(buildDir + '/facebook-www')) {
const filePath = buildDir + '/facebook-www/' + fileName;
const stats = fs.statSync(filePath);
if (!stats.isDirectory()) {
fs.renameSync(filePath, filePath.replace('.js', '.classic.js'));
}
}
}
if (fs.existsSync(buildDir + '/sizes')) {
fs.renameSync(buildDir + '/sizes', buildDir + '/sizes-stable');
}
}
function processExperimental(buildDir, version) {
if (fs.existsSync(buildDir + '/node_modules')) {
updatePackageVersions(buildDir + '/node_modules', version);
fs.renameSync(buildDir + '/node_modules', buildDir + '/oss-experimental');
}
if (fs.existsSync(buildDir + '/facebook-www')) {
for (const fileName of fs.readdirSync(buildDir + '/facebook-www')) {
const filePath = buildDir + '/facebook-www/' + fileName;
const stats = fs.statSync(filePath);
if (!stats.isDirectory()) {
fs.renameSync(filePath, filePath.replace('.js', '.modern.js'));
}
}
}
if (fs.existsSync(buildDir + '/sizes')) {
fs.renameSync(buildDir + '/sizes', buildDir + '/sizes-experimental');
}
// Delete all other artifacts that weren't handled above. We assume they are
// duplicates of the corresponding artifacts in the stable channel. Ideally,
// the underlying build script should not have produced these files in the
// first place.
for (const pathName of fs.readdirSync(buildDir)) {
if (
pathName !== 'oss-experimental' &&
pathName !== 'facebook-www' &&
pathName !== 'sizes-experimental'
) {
spawnSync('rm', ['-rm', buildDir + '/' + pathName]);
}
}
}
function updatePackageVersions(modulesDir, version) {
const allReactModuleNames = fs.readdirSync('packages');
for (const moduleName of fs.readdirSync(modulesDir)) {
const packageJSONPath = path.join(modulesDir, moduleName, 'package.json');
const stats = fs.statSync(packageJSONPath);
if (stats.isFile()) {
const packageInfo = JSON.parse(fs.readFileSync(packageJSONPath));
// Update version
packageInfo.version = version;
// Update dependency versions
if (packageInfo.dependencies) {
for (const dep of Object.keys(packageInfo.dependencies)) {
if (allReactModuleNames.includes(dep)) {
packageInfo.dependencies[dep] = version;
}
}
}
if (packageInfo.peerDependencies) {
for (const dep of Object.keys(packageInfo.peerDependencies)) {
if (allReactModuleNames.includes(dep)) {
packageInfo.peerDependencies[dep] = version;
}
}
}
// Write out updated package.json
fs.writeFileSync(packageJSONPath, JSON.stringify(packageInfo, null, 2));
}
}
}
function updateTheReactVersionThatDevToolsReads(version) {
// Overwrite the ReactVersion module before the build script runs so that it
// is included in the final bundles. This only runs in CI, so it's fine to
// edit the source file.
fs.writeFileSync(
'./packages/shared/ReactVersion.js',
`export default '${version}';\n`
);
}