From 264f2df7f48bb233473e7927ada3c28c83a393c6 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Thu, 10 Jun 2021 12:17:12 -0400 Subject: [PATCH] Fix buildNumber to be consistent for all package.json's (#3016) --- build/set_build_version.ts | 67 ++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/build/set_build_version.ts b/build/set_build_version.ts index 13d46d4ff3..8315d535bd 100644 --- a/build/set_build_version.ts +++ b/build/set_build_version.ts @@ -18,7 +18,7 @@ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import * as fs from "fs"; +import * as fse from "fs-extra"; import * as path from "path"; import appInfo from "../package.json"; import semver from "semver"; @@ -26,30 +26,57 @@ import fastGlob from "fast-glob"; const packagePath = path.join(__dirname, "../package.json"); const versionInfo = semver.parse(appInfo.version); -const buildNumber = process.env.BUILD_NUMBER || "1"; -let buildChannel = "alpha"; +const buildNumber = process.env.BUILD_NUMBER || Date.now().toString(); -if (versionInfo.prerelease) { - if (versionInfo.prerelease.includes("alpha")) { - buildChannel = "alpha"; - } else { - buildChannel = "beta"; +function getBuildChannel(): string { + /** + * Note: it is by design that we don't use `rc` as a build channel for these versions + */ + switch (versionInfo.prerelease?.[0]) { + case "beta": + return "beta"; + case undefined: + return "latest"; + default: + return "alpha"; } - appInfo.version = `${versionInfo.major}.${versionInfo.minor}.${versionInfo.patch}-${buildChannel}.${versionInfo.prerelease[1]}.${buildNumber}`; -} else { - appInfo.version = `${appInfo.version}-latest.${buildNumber}`; } +async function writeOutExtensionVersion(manifestPath: string) { + const extensionPackageJson = await fse.readJson(manifestPath); -fs.writeFileSync(packagePath, `${JSON.stringify(appInfo, null, 2)}\n`); + extensionPackageJson.version = `${versionInfo.format()}.${buildNumber}`; -const extensionManifests = fastGlob.sync(["extensions/*/package.json"]); - -for (const manifestPath of extensionManifests) { - const packagePath = path.join(__dirname, "..", manifestPath); - - import(packagePath).then((packageInfo) => { - packageInfo.default.version = `${versionInfo.raw}.${Date.now()}`; - fs.writeFileSync(packagePath, `${JSON.stringify(packageInfo.default, null, 2)}\n`); + return fse.writeJson(manifestPath, extensionPackageJson, { + spaces: 2, }); } + +async function writeOutNewVersions() { + await Promise.all([ + fse.writeJson(packagePath, appInfo, { + spaces: 2, + }), + ...(await fastGlob(["extensions/*/package.json"])).map(writeOutExtensionVersion), + ]); +} + +function main() { + const prereleaseParts: string[] = [getBuildChannel()]; + + if (versionInfo.prerelease) { + prereleaseParts.push(versionInfo.prerelease[1].toString()); + } + + prereleaseParts.push(buildNumber); + + appInfo.version = `${versionInfo.major}.${versionInfo.minor}.${versionInfo.patch}-${prereleaseParts.join(".")}`; + + writeOutNewVersions() + .catch((error) => { + console.error(error); + process.exit(1); + }); +} + +main();