From f35e2f53932d89c8fe4c569367cb04e310e25bd7 Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Fri, 10 May 2024 19:15:06 +0530 Subject: [PATCH] fix(get): simplify prerelease detection logic (#166) Fixes: #165 --- src/postinstall.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/postinstall.js b/src/postinstall.js index 66b2c89..6222b05 100644 --- a/src/postinstall.js +++ b/src/postinstall.js @@ -22,7 +22,7 @@ async function postinstall() { /** * @type {fs.PathLike} */ - const nodeManifestPath = path.resolve(__dirname, '..' ,'package.json'); + const nodeManifestPath = path.resolve(__dirname, '..', 'package.json'); /** * @type {object} */ @@ -35,21 +35,17 @@ async function postinstall() { ].join('.'); let flavor = process.env.npm_config_nwjs_build_type || process.env.NWJS_BUILD_TYPE || 'normal'; - /* Check if version is a prelease. */ - if (typeof parsedVersion?.prerelease?.[0] === 'string') { - let prerelease = parsedVersion.prerelease[0].split('-'); - if (prerelease.length > 1) { - prerelease = prerelease.slice(0, -1); - } - version = [version, ...prerelease].join('-'); - } + /** + * If `parsedVersion` is `null`, then prerelease is `"null"`. + * If `parsedVersion?.prerelease` is `[]`, then prerelease is `"undefined"`. + * If `parsedVersion?.prerelease[0]` is `"N-sdk"` where N represents a build number, then prerelease is `"N-sdk"`. + * + * @type {"null" | "undefined" | "N-sdk" | ""} + */ + const prerelease = String(parsedVersion?.prerelease[0]); /* Check build flavor and slice that off the `version`. */ - if (version.endsWith('-sdk')) { - version = version.slice(0, -4); - flavor = 'sdk'; - } else if (version.endsWith('sdk')) { - version = version.slice(0, -3); + if (prerelease.endsWith('-sdk')) { flavor = 'sdk'; }