Skip to content

Commit ba592e7

Browse files
committed
feat: Add a script for uploading browser bundles to CDN
1 parent c4f11bb commit ba592e7

File tree

3 files changed

+389
-14
lines changed

3 files changed

+389
-14
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"packages/utils"
2020
],
2121
"devDependencies": {
22+
"@google-cloud/storage": "^1.7.0",
2223
"@types/chai": "^4.1.3",
2324
"@types/jest": "^22.2.3",
2425
"@types/mocha": "^5.2.0",

scripts/browser-upload-cdn.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'use strict';
2+
3+
const Storage = require('@google-cloud/storage');
4+
const path = require('path');
5+
const os = require('os');
6+
const fs = require('fs');
7+
8+
const bundleFilesRegex = /^bundle.*\.js.*$/;
9+
const rootDir = path.dirname(__dirname);
10+
const browserDir = path.join(rootDir, 'packages', 'browser');
11+
const browserBuildDir = path.join(browserDir, 'build');
12+
13+
/** Return full paths of files to upload */
14+
function findFiles() {
15+
const bundleFiles = fs
16+
.readdirSync(browserBuildDir)
17+
.filter(filename => filename.match(bundleFilesRegex))
18+
.map(filename => path.join(browserBuildDir, filename));
19+
return bundleFiles;
20+
}
21+
22+
/** Upload sentry-browser bundles to a GCS bucket */
23+
async function uploadFiles() {
24+
const gcsConfigPath =
25+
process.env.BROWSER_GOOGLE_APPLICATION_CREDENTIALS ||
26+
path.join(os.homedir(), '.gcs', 'sentry-browser-sdk.json');
27+
console.log(`Reading GCS configuration from "${gcsConfigPath}"...`);
28+
29+
const gcsConfig = fs.existsSync(gcsConfigPath)
30+
? JSON.parse(fs.readFileSync(gcsConfigPath))
31+
: undefined;
32+
33+
if (!gcsConfig) {
34+
console.error(
35+
'Google Storage configuration (service account key) not found.\n' +
36+
`Place it at ${gcsConfigPath} or use the environment variable ` +
37+
'(BROWSER_GOOGLE_APPLICATION_CREDENTIALS) to specify the path.',
38+
);
39+
process.exit(1);
40+
}
41+
42+
const projectId =
43+
process.env.BROWSER_GOOGLE_PROJECT_ID || gcsConfig.project_id;
44+
if (!projectId) {
45+
console.error('Google project ID not found.');
46+
process.exit(1);
47+
}
48+
49+
const bucketName =
50+
process.env.BROWSER_GOOGLE_BUCKET_NAME || gcsConfig.bucket_name;
51+
if (!bucketName) {
52+
console.error('Bucket name not found in the configuration.');
53+
process.exit(1);
54+
}
55+
56+
const bundleFiles = findFiles();
57+
if (!bundleFiles.length) {
58+
console.error('Error: no files to upload!');
59+
process.exit(1);
60+
}
61+
62+
const browserPackageJson = path.join(browserDir, 'package.json');
63+
const version =
64+
JSON.parse(fs.readFileSync(browserPackageJson)).version || 'unreleased';
65+
66+
const storage = new Storage({
67+
projectId,
68+
});
69+
70+
const bucket = storage.bucket(bucketName);
71+
// TODO increase after some testing is done
72+
const cacheAge = 300;
73+
74+
await Promise.all(
75+
bundleFiles.map(async filepath => {
76+
const destination = path.join(version, path.basename(filepath));
77+
const options = {
78+
gzip: true,
79+
destination: destination,
80+
metadata: {
81+
cacheControl: `public, max-age=${cacheAge}`,
82+
},
83+
};
84+
await bucket.upload(filepath, options);
85+
console.log(`Uploaded "${destination}"`);
86+
}),
87+
);
88+
console.log('Upload complete.');
89+
}
90+
91+
uploadFiles().catch(error => {
92+
console.error('Error occurred:', error);
93+
process.exit(1);
94+
});

0 commit comments

Comments
 (0)