-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
build-shrink-ray.js
106 lines (89 loc) · 2.93 KB
/
build-shrink-ray.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
/* eslint-disable no-console */
require('isomorphic-fetch');
const {resolve} = require('path');
const {execSync} = require('child_process');
const {existsSync, readFileSync} = require('fs-extra');
const BASE_BRANCH = 'master';
const repo = 'polaris-react';
const sha = process.env.CIRCLE_SHA1;
const postWebpackReportURL = `https://shrink-ray.shopifycloud.com/repos/${repo}/commits/${sha}/reports`;
const build = resolve(__dirname, '..', 'shrink-ray-build/build');
const report = resolve(build, 'bundle-analysis', 'report.html');
process.on('unhandledRejection', (reason) => {
throw reason;
});
if (sha) {
setupShrinkRay();
} else {
console.log(
'sha is not available, building bundle without pinging shrink-ray',
);
buildPackages();
}
function buildPackages() {
execSync('yarn run webpack --config shrink-ray-build/webpack.config.js', {
stdio: 'inherit',
});
}
function setupShrinkRay() {
console.log('[shrink-ray] Running shrink-ray prebuild script...');
sendCommitStatus('pending')
.then((response) => {
console.log(`[shrink-ray] status: ${response.status}`);
console.log(`[shrink-ray] statusText: ${response.statusText}`);
console.log('[shrink-ray] shrink-ray prebuild script completed.');
buildPackages();
return postReportToShrinkRay();
})
.then((response) => {
console.log(`[shrink-ray] Status: ${response.status}`);
console.log(`[shrink-ray] Status text: ${response.statusText}`);
return response.text();
})
.then((responseText) => {
console.log(`[shrink-ray] Response text: ${responseText}`);
console.log('[shrink-ray] Postbuild script completed.');
return true;
})
.catch((error) => {
console.log('ERROR: ', error);
// eslint-disable-next-line promise/no-nesting
sendCommitStatus('error')
.then(() => {
throw error;
})
.catch(() => {
throw error;
});
});
}
function sendCommitStatus(state) {
// prettier-ignore
const statusUrl = `https://shrink-ray.shopifycloud.com/repos/${repo}/commits/${sha}/status/${state}`;
console.log(`[shrink-ray] POST ${statusUrl}`);
return fetch(statusUrl, {method: 'POST'});
}
function postReportToShrinkRay() {
console.log('[shrink-ray] Running shrink-ray postbuild script...');
console.log(`[shrink-ray] POST ${postWebpackReportURL}`);
if (!existsSync(report)) {
sendCommitStatus('error');
throw new Error('webpack-bundle-analyzer report not found.');
}
// fetch latest in BuildKite pipeline
execSync('git fetch origin master');
const masterSha = execSync(`git merge-base HEAD origin/${BASE_BRANCH}`, {
encoding: 'utf8',
}).trim();
console.log('[shrink-ray] masterSha: ', masterSha);
return fetch(postWebpackReportURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
report: readFileSync(report, 'utf8'),
masterSha,
}),
});
}