|
| 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