1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00
metamask-extension/development/build/utils.js
Mark Stacey 345ed9f6f2
Add build type to Sentry environment (#12441)
The build type (i.e. the distribution) is now included in the Sentry
environment during setup, for all builds except the "main" build. This
will allow us to track Flask and beta errors separately from other
errors.

A constant was created for the build types. The equivalent constant in
our build scripts was updated to match it more closely, for
consistency. We can't use the same constant in both places because our
shared constants are in modules that use ES6 exports, and our build
script does not yet support ES6 exports.

The singular `BuildType` was used rather than `BuildTypes` to match our
naming conventions elsewhere for enums. We name them like classes or
types, rather than like a collection.

Relates to #11896
2021-10-25 14:27:30 -02:30

69 lines
2.3 KiB
JavaScript

const semver = require('semver');
const { version } = require('../../package.json');
/**
* The distribution this build is intended for.
*
* This should be kept in-sync with the `BuildType` map in `shared/constants/app.js`.
*/
const BuildType = {
beta: 'beta',
flask: 'flask',
main: 'main',
};
/**
* Map the current version to a format that is compatible with each browser.
*
* The given version number is assumed to be a SemVer version number. Additionally, if the version
* has a prerelease component, it is assumed to have the format "<build type>.<build version",
* where the build version is a positive integer.
*
* @param {string} currentVersion - The current version.
* @param {string[]} platforms - A list of browsers to generate versions for.
* @returns {Object} An object with the browser as the key and the browser-specific version object
* as the value. For example, the version `9.6.0-beta.1` would return the object
* `{ firefox: { version: '9.6.0.beta1' }, chrome: { version: '9.6.0.1', version_name: 'beta' } }`.
*/
function getBrowserVersionMap(platforms) {
const major = semver.major(version);
const minor = semver.minor(version);
const patch = semver.patch(version);
const prerelease = semver.prerelease(version);
let buildType;
let buildVersion;
if (prerelease) {
if (prerelease.length !== 2) {
throw new Error(`Invalid prerelease version: '${prerelease.join('.')}'`);
}
[buildType, buildVersion] = prerelease;
if (!String(buildVersion).match(/^\d+$/u)) {
throw new Error(`Invalid prerelease build version: '${buildVersion}'`);
} else if (buildType !== BuildType.beta) {
throw new Error(`Invalid prerelease build type: ${buildType}`);
}
}
return platforms.reduce((platformMap, platform) => {
const versionParts = [major, minor, patch];
const browserSpecificVersion = {};
if (prerelease) {
if (platform === 'firefox') {
versionParts.push(`${buildType}${buildVersion}`);
} else {
versionParts.push(buildVersion);
browserSpecificVersion.version_name = buildType;
}
}
browserSpecificVersion.version = versionParts.join('.');
platformMap[platform] = browserSpecificVersion;
return platformMap;
}, {});
}
module.exports = {
BuildType,
getBrowserVersionMap,
};