Skip to content

Commit

Permalink
fix(build): ensure build does not fail when package version env varia…
Browse files Browse the repository at this point in the history
…ble is undefined

Previously, the build process failed when the 'version' environment variable was not defined,
due to a lack of validation for the presence of this variable. This fix introduces a check to
ensure that the package version, obtained from either the 'version' environment variable or
the fallback 'package.json' file, is defined before proceeding with the build.
  • Loading branch information
jobl committed Jan 31, 2024
1 parent 4a63585 commit 1012017
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const output = 'dist/webrtc-client';
export default args => {
let target;
let format = args.format;
let version = process.env.version;
// Determine the target and format based on provided arguments
if (format) {
if (format.toLowerCase().startsWith('es')) {
Expand All @@ -32,22 +31,21 @@ export default args => {
format = 'es';
target = 'es6';
}
// Validate version format (SemVer)
const [semVer, major, minor, patch, prerelease, buildmetadata] =
version.match(
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/
) ?? [];
if (semVer) {
version = semVer;
console.info('Major: ' + major);
console.info('Minor: ' + minor);
console.info('Patch: ' + patch);
console.info('Prerelease: ' + prerelease);
console.info('Buildmetadata: ' + buildmetadata);
console.info('Using provided version: ' + version);
// Determine the package version by using the 'version' environment variable (for CI/CD processes) or fallback to the version specified in the 'package.json' file.
let version = process.env.version ?? process.env.npm_package_version;
// Validate the version format
if (typeof version === 'string') {
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
const versionRegex =
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
if (!versionRegex.test(version)) {
throw new Error(
'The provided version string does not comply with the Semantic Versioning (SemVer) format required. Please refer to https://semver.org/ for more details on the SemVer specification.'
);
}
console.info('Building version: ' + version);
} else {
version = process.env.npm_package_version;
console.warn('(!) Invalid version format provided, using package.json version: ' + version);
throw new Error('Version is undefined or not a string.');
}

return [
Expand Down

0 comments on commit 1012017

Please sign in to comment.