From 1012017d169ba9895382ab6586d6dd94e05d1315 Mon Sep 17 00:00:00 2001 From: Jonas <7703727+jobl@users.noreply.github.com> Date: Wed, 31 Jan 2024 12:14:47 +0100 Subject: [PATCH] fix(build): ensure build does not fail when package version env variable 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. --- rollup.config.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index 0f4c769..5c0ab38 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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')) { @@ -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 [