|
| 1 | +const { resolve } = require('path'); |
| 2 | +const fs = require('fs'); |
| 3 | +const { execSync } = require('child_process'); |
| 4 | +const https = require('https'); |
| 5 | +const terser = require('terser'); |
| 6 | + |
| 7 | +const repoRoot = resolve(__dirname, '..'); |
| 8 | + |
| 9 | +const runId = process.env.GITHUB_RUN_ID || 'local-run-id'; |
| 10 | + |
| 11 | +const METRICS_SERVICE_URL = process.env.METRICS_SERVICE_URL; |
| 12 | + |
| 13 | +// CDN scripts |
| 14 | +function generateReportForCDNScripts() { |
| 15 | + const reports = []; |
| 16 | + const firebaseRoot = resolve(__dirname, '../packages/firebase'); |
| 17 | + const pkgJson = require(`${firebaseRoot}/package.json`); |
| 18 | + |
| 19 | + const special_files = [ |
| 20 | + 'firebase-performance-standalone.es2017.js', |
| 21 | + 'firebase-performance-standalone.js', |
| 22 | + 'firebase.js' |
| 23 | + ]; |
| 24 | + |
| 25 | + const files = [ |
| 26 | + ...special_files.map(file => `${firebaseRoot}/${file}`), |
| 27 | + ...pkgJson.components.map(component => `${firebaseRoot}/firebase-${component}.js`) |
| 28 | + ]; |
| 29 | + |
| 30 | + for (const file of files) { |
| 31 | + const { size } = fs.statSync(file); |
| 32 | + const fileName = file.split('/').slice(-1)[0]; |
| 33 | + reports.push(makeReportObject('firebase', fileName, size)); |
| 34 | + } |
| 35 | + |
| 36 | + return reports; |
| 37 | +} |
| 38 | + |
| 39 | +// NPM packages |
| 40 | +function generateReportForNPMPackages() { |
| 41 | + const reports = []; |
| 42 | + const fields = [ |
| 43 | + 'main', |
| 44 | + 'module', |
| 45 | + 'esm2017', |
| 46 | + 'browser', |
| 47 | + 'react-native', |
| 48 | + 'lite', |
| 49 | + 'lite-esm2017' |
| 50 | + ]; |
| 51 | + |
| 52 | + const packageInfo = JSON.parse( |
| 53 | + execSync('npx lerna ls --json --scope @firebase/*', { cwd: repoRoot }).toString() |
| 54 | + ); |
| 55 | + |
| 56 | + for (const package of packageInfo) { |
| 57 | + const packageJson = require(`${package.location}/package.json`); |
| 58 | + |
| 59 | + for (const field of fields) { |
| 60 | + if (packageJson[field]) { |
| 61 | + const filePath = `${package.location}/${packageJson[field]}`; |
| 62 | + |
| 63 | + const rawCode = fs.readFileSync(filePath, 'utf-8'); |
| 64 | + |
| 65 | + // remove comments and whitespaces, then get size |
| 66 | + const { code } = terser.minify(rawCode, { |
| 67 | + output: { |
| 68 | + comments: false |
| 69 | + }, |
| 70 | + mangle: false, |
| 71 | + compress: false |
| 72 | + }); |
| 73 | + |
| 74 | + const size = Buffer.byteLength(code, 'utf-8') |
| 75 | + reports.push(makeReportObject(packageJson.name, field, size)); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return reports; |
| 81 | +} |
| 82 | + |
| 83 | +function makeReportObject(sdk, type, value) { |
| 84 | + return { |
| 85 | + sdk, |
| 86 | + type, |
| 87 | + value |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +function generateSizeReport() { |
| 92 | + const reports = [ |
| 93 | + ...generateReportForCDNScripts(), |
| 94 | + ...generateReportForNPMPackages() |
| 95 | + ]; |
| 96 | + |
| 97 | + for (const r of reports) { |
| 98 | + console.log(r.sdk, r.type, r.value); |
| 99 | + } |
| 100 | + |
| 101 | + console.log(`Github Action URL: https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${runId}`); |
| 102 | + |
| 103 | + return { |
| 104 | + log: `https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${runId}`, |
| 105 | + metric: "BinarySize", |
| 106 | + results: reports |
| 107 | + }; |
| 108 | +} |
| 109 | + |
| 110 | +function constructRequestPath() { |
| 111 | + const repo = process.env.GITHUB_REPOSITORY; |
| 112 | + const commit = process.env.GITHUB_SHA; |
| 113 | + let path = `/repos/${repo}/commits/${commit}/reports`; |
| 114 | + if (process.env.GITHUB_EVENT_NAME === 'pull_request') { |
| 115 | + const pullRequestNumber = process.env.GITHUB_PULL_REQUEST_NUMBER; |
| 116 | + const pullRequestBaseSha = process.env.GITHUB_PULL_REQUEST_BASE_SHA; |
| 117 | + path += `?pull_request=${pullRequestNumber}&base_commit=${pullRequestBaseSha}`; |
| 118 | + } |
| 119 | + return path; |
| 120 | +} |
| 121 | + |
| 122 | +function constructRequestOptions(path) { |
| 123 | + const accessToken = execSync('gcloud auth print-identity-token', { encoding: 'utf8' }).trim(); |
| 124 | + return { |
| 125 | + path: path, |
| 126 | + method: 'POST', |
| 127 | + headers: { |
| 128 | + 'Authorization': `Bearer ${accessToken}`, |
| 129 | + 'Content-Type': 'application/json' |
| 130 | + } |
| 131 | + }; |
| 132 | +} |
| 133 | + |
| 134 | +function upload(report) { |
| 135 | + if (!process.env.GITHUB_ACTIONS) { |
| 136 | + console.log('Metrics upload is only enabled on CI.'); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + const path = constructRequestPath(); |
| 141 | + const options = constructRequestOptions(path); |
| 142 | + |
| 143 | + console.log(`${report.metric} report:`, report); |
| 144 | + console.log(`Posting to metrics service endpoint: ${path} ...`); |
| 145 | + |
| 146 | + const request = https.request(METRICS_SERVICE_URL, options, response => { |
| 147 | + response.setEncoding('utf8'); |
| 148 | + console.log(`Response status code: ${response.statusCode}`); |
| 149 | + response.on('data', console.log); |
| 150 | + response.on('end', () => { |
| 151 | + if (response.statusCode !== 202) { |
| 152 | + process.exit(1); |
| 153 | + } |
| 154 | + }) |
| 155 | + }); |
| 156 | + request.write(JSON.stringify(report)); |
| 157 | + request.end(); |
| 158 | +} |
| 159 | + |
| 160 | +const report = generateSizeReport(); |
| 161 | +upload(report); |
0 commit comments