Skip to content

Commit 753995d

Browse files
committed
Run in parallel
1 parent cd76020 commit 753995d

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

tools/gulp/tasks/payload.ts

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,13 @@ task('payload', ['material:clean-build'], async () => {
3434
// Open a connection to Firebase. For PRs the connection will be established as a guest.
3535
const database = openFirebaseDashboardApp(!isTravisMasterBuild());
3636
const currentSha = process.env['TRAVIS_PULL_REQUEST_SHA'] || process.env['TRAVIS_COMMIT'];
37-
const authToken = process.env['FIREBASE_ACCESS_TOKEN'];
38-
const previousPayload = await getLastPayloadResults(database);
3937

40-
// Calculate library sizes by combining the CDK and Material FESM 2015 bundles.
41-
const previousSize = previousPayload.cdk_fesm_2015 + previousPayload.material_fesm_2015;
42-
const currentSize = results.cdk_fesm_2015 + results.material_fesm_2015;
43-
const deltaSize = currentSize - previousSize;
44-
45-
// Publish the results to firebase when it runs on Travis but not as a PR.
46-
if (isTravisMasterBuild()) {
47-
await database.ref('payloads').child(currentSha).set(results);
48-
}
49-
50-
if (authToken) {
51-
// Update the Github status of the current commit by sending a request to the dashboard
52-
// firebase http trigger function.
53-
await updateGithubStatus(currentSha, deltaSize, authToken);
54-
} else {
55-
console.warn('Cannot set the Github status because there is no "FIREBASE_ACCESS_TOKEN" set.');
56-
}
38+
// Upload the payload results and calculate the payload diff in parallel. Otherwise the
39+
// payload task will take much more time inside of Travis builds.
40+
await Promise.all([
41+
uploadPayloadResults(database, currentSha, results),
42+
calculatePayloadDiff(database, currentSha, results)
43+
]);
5744

5845
// Disconnect database connection because Firebase otherwise prevents Gulp from exiting.
5946
database.delete();
@@ -71,6 +58,31 @@ function getFilesize(filePath: string) {
7158
return statSync(filePath).size / 1000;
7259
}
7360

61+
/**
62+
* Calculates the difference between the last and current library payload.
63+
* The results will be published as a commit status on Github.
64+
*/
65+
async function calculatePayloadDiff(database: any, currentSha: string, currentPayload: any) {
66+
const authToken = process.env['FIREBASE_ACCESS_TOKEN'];
67+
68+
if (!authToken) {
69+
console.error('Cannot calculate Payload diff because there is no "FIREBASE_ACCESS_TOKEN" ' +
70+
'environment variable set.');
71+
return;
72+
}
73+
74+
const previousPayload = await getLastPayloadResults(database);
75+
76+
// Calculate library sizes by combining the CDK and Material FESM 2015 bundles.
77+
const previousSize = previousPayload.cdk_fesm_2015 + previousPayload.material_fesm_2015;
78+
const currentSize = currentPayload.cdk_fesm_2015 + currentPayload.material_fesm_2015;
79+
const deltaSize = currentSize - previousSize;
80+
81+
// Update the Github status of the current commit by sending a request to the dashboard
82+
// firebase http trigger function.
83+
await updateGithubStatus(currentSha, deltaSize, authToken);
84+
}
85+
7486
/**
7587
* Updates the Github status of a given commit by sending a request to a Firebase function of
7688
* the dashboard. The function has access to the Github repository and can set status for PRs too.
@@ -98,6 +110,13 @@ async function updateGithubStatus(commitSha: string, payloadDiff: number, authTo
98110
});
99111
}
100112

113+
/** Uploads the current payload results to the Dashboard database. */
114+
async function uploadPayloadResults(database: any, currentSha: string, currentPayload: any) {
115+
if (isTravisMasterBuild()) {
116+
await database.ref('payloads').child(currentSha).set(currentPayload);
117+
}
118+
}
119+
101120
/** Gets the last payload uploaded from previous Travis builds. */
102121
async function getLastPayloadResults(database: admin.database.Database) {
103122
const snapshot = await database.ref('payloads')

0 commit comments

Comments
 (0)