diff --git a/scripts/export-bundles-sizes.js b/scripts/export-bundles-sizes.js index 18c2cd655f..4ca4b40968 100644 --- a/scripts/export-bundles-sizes.js +++ b/scripts/export-bundles-sizes.js @@ -1,6 +1,6 @@ const fs = require('fs') const path = require('path') -const { execSync } = require('child_process') +const { getBrowserSdkVersion } = require('./lib/browser-sdk-version') const { getOrg2ApiKey } = require('./lib/secrets') const { runMain, fetch: fetchWrapper } = require('./lib/execution-utils') @@ -8,10 +8,9 @@ const rumPath = path.join(__dirname, '../packages/rum/bundle/datadog-rum.js') const logsPath = path.join(__dirname, '../packages/logs/bundle/datadog-logs.js') const rumSlimPath = path.join(__dirname, '../packages/rum-slim/bundle/datadog-rum-slim.js') const workerPath = path.join(__dirname, '../packages/worker/bundle/worker.js') -const versionPath = path.join(__dirname, '../packages/rum/package.json') -const URL = 'https://http-intake.logs.datadoghq.com/api/v2/logs' -const HEADERS = { +const LOG_INTAKE_URL = 'https://http-intake.logs.datadoghq.com/api/v2/logs' +const LOG_INTAKE_REQUEST_HEADERS = { 'DD-API-KEY': getOrg2ApiKey(), 'Content-Type': 'application/json', } @@ -29,49 +28,27 @@ runMain(async () => { rum_slim: getBundleSize(rumSlimPath), worker: getBundleSize(workerPath), }, - version: getVersion(), - commit: executeGitCommand('git rev-parse HEAD'), + version: getBrowserSdkVersion(), + commit: process.env.CI_COMMIT_SHORT_SHA, branch: process.env.CI_COMMIT_REF_NAME, }, ] - await postBundleSize(URL, logData) + await sendLogToOrg2(logData) }) -function executeGitCommand(command) { - try { - return execSync(command) - .toString('utf8') - .replace(/[\n\r\s]+$/, '') - } catch (error) { - console.error('Failed to execute git command:', error) - return null - } -} - -function getVersion() { - try { - const versionJson = fs.readFileSync(versionPath, 'utf8') - return JSON.parse(versionJson).version - } catch (error) { - console.error('Failed to get version:', error) - return null - } -} - function getBundleSize(pathBundle) { try { const file = fs.statSync(pathBundle) return file.size } catch (error) { - console.error('Failed to get bundle size:', error) - return null + throw new Error('Failed to get bundle size', { cause: error }) } } -async function postBundleSize(url = '', bundleData = {}) { - await fetchWrapper(url, { +async function sendLogToOrg2(bundleData = {}) { + await fetchWrapper(LOG_INTAKE_URL, { method: 'POST', - headers: HEADERS, + headers: LOG_INTAKE_REQUEST_HEADERS, body: JSON.stringify(bundleData), }) } diff --git a/scripts/lib/browser-sdk-version.js b/scripts/lib/browser-sdk-version.js new file mode 100644 index 0000000000..b38226f333 --- /dev/null +++ b/scripts/lib/browser-sdk-version.js @@ -0,0 +1,9 @@ +const lernaJson = require('../../lerna.json') + +function getBrowserSdkVersion() { + return lernaJson.version +} + +module.exports = { + getBrowserSdkVersion, +} diff --git a/scripts/lib/build-env.js b/scripts/lib/build-env.js index de9d6b532b..f94826117c 100644 --- a/scripts/lib/build-env.js +++ b/scripts/lib/build-env.js @@ -1,7 +1,7 @@ const { readFileSync } = require('fs') const path = require('path') const execSync = require('child_process').execSync -const lernaJson = require('../../lerna.json') +const { getBrowserSdkVersion } = require('./browser-sdk-version') const { command } = require('./command') /** @@ -24,12 +24,12 @@ const buildEnvFactories = { SDK_VERSION: () => { switch (getBuildMode()) { case 'release': - return lernaJson.version + return getBrowserSdkVersion() case 'canary': { const commitSha1 = execSync('git rev-parse HEAD').toString().trim() // TODO when tags would allow '+' characters // use build separator (+) instead of prerelease separator (-) - return `${lernaJson.version}-${commitSha1}` + return `${getBrowserSdkVersion()}-${commitSha1}` } default: return 'dev' diff --git a/scripts/lib/execution-utils.js b/scripts/lib/execution-utils.js index 1c73a2794e..f1e7203453 100644 --- a/scripts/lib/execution-utils.js +++ b/scripts/lib/execution-utils.js @@ -26,7 +26,7 @@ function runMain(mainFunction) { .then(() => mainFunction()) .catch((error) => { printError('\nScript exited with error:') - printError(error) + printErrorWithCause(error) process.exit(1) }) } @@ -38,6 +38,14 @@ function printError(...params) { console.log(redColor, ...params, resetColor) } +function printErrorWithCause(error) { + printError(error) + if (error.cause) { + printError('Caused by:') + printErrorWithCause(error.cause) + } +} + function printLog(...params) { const greenColor = '\x1b[32;1m' console.log(greenColor, ...params, resetColor) diff --git a/scripts/release/generate-changelog.js b/scripts/release/generate-changelog.js index a0974da97f..2ae7be9b13 100644 --- a/scripts/release/generate-changelog.js +++ b/scripts/release/generate-changelog.js @@ -5,7 +5,7 @@ const readFile = util.promisify(require('fs').readFile) const emojiNameMap = require('emoji-name-map') -const lernaConfig = require('../../lerna.json') +const { getBrowserSdkVersion } = require('../lib/browser-sdk-version') const { spawnCommand, printError, runMain } = require('../lib/execution-utils') const { command } = require('../lib/command') const { modifyFile } = require('../lib/files-utils') @@ -33,7 +33,7 @@ ${emojisLegend} --- -## v${lernaConfig.version} +## v${getBrowserSdkVersion()} ${changesList} ${content.slice(content.indexOf('\n##'))}` diff --git a/scripts/release/update-peer-dependency-versions.js b/scripts/release/update-peer-dependency-versions.js index d884281caf..bf07a765c8 100644 --- a/scripts/release/update-peer-dependency-versions.js +++ b/scripts/release/update-peer-dependency-versions.js @@ -1,7 +1,7 @@ -const lernaConfig = require('../../lerna.json') const { runMain } = require('../lib/execution-utils') const { modifyFile } = require('../lib/files-utils') const { command } = require('../lib/command') +const { getBrowserSdkVersion } = require('../lib/browser-sdk-version') const JSON_FILES = ['rum', 'rum-slim', 'logs'].map((packageName) => `./packages/${packageName}/package.json`) @@ -25,7 +25,7 @@ runMain(async () => { function updateJsonPeerDependencies(content) { const json = JSON.parse(content) Object.keys(json.peerDependencies).forEach((key) => { - json.peerDependencies[key] = lernaConfig.version + json.peerDependencies[key] = getBrowserSdkVersion() }) return `${JSON.stringify(json, null, 2)}\n` }