From 705195e1ec526fce8aa20de023f13b2ab286c435 Mon Sep 17 00:00:00 2001 From: Boopathi Nedunchezhiyan Date: Thu, 12 Sep 2024 12:46:44 +0200 Subject: [PATCH] fix: create new canary version from published pkg --- .github/workflows/publish-canary.yml | 6 ++-- package.json | 1 + scripts/set-canary-version.js | 54 ++++++++++++++++++++++++++++ yarn.lock | 5 +++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100755 scripts/set-canary-version.js diff --git a/.github/workflows/publish-canary.yml b/.github/workflows/publish-canary.yml index eb7b652b..1dd756e5 100644 --- a/.github/workflows/publish-canary.yml +++ b/.github/workflows/publish-canary.yml @@ -16,7 +16,9 @@ jobs: node-version: 20 registry-url: "https://registry.npmjs.org" - run: yarn install - - run: yarn version --prerelease --preid=canary --no-git-tag-version + - run: node scripts/set-canary-version.js + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} - run: yarn publish --tag canary --non-interactive env: - NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/package.json b/package.json index dc567487..0fcb9d7f 100644 --- a/package.json +++ b/package.json @@ -100,6 +100,7 @@ "@typescript-eslint/parser": "6.7.0", "benchmark": "^2.1.4", "codecov": "^3.8.2", + "compare-versions": "^6.1.1", "eslint": "^8.49.0", "eslint-config-prettier": "^9.0.0", "graphql": "^16.8.0", diff --git a/scripts/set-canary-version.js b/scripts/set-canary-version.js new file mode 100755 index 00000000..03f206b0 --- /dev/null +++ b/scripts/set-canary-version.js @@ -0,0 +1,54 @@ +#!/usr/bin/env node + +const fs = require("fs/promises"); +const path = require("path"); +const { execSync } = require("child_process"); +const { compareVersions } = require("compare-versions"); + +const ROOT_DIR = path.join(__dirname, ".."); + +main().catch((e) => { + console.error(e); + process.exit(1); +}); + +async function main() { + const pkg = require(path.join(ROOT_DIR, "package.json")); + const { name, version } = pkg; + const canaryVersion = getCanaryVersion(name); + + const versionCompare = compareVersions(version, canaryVersion); + + if (versionCompare > 0) { + console.info( + `${name} v${version} is greater than the published canary v${canaryVersion}! Creating new canary version.` + ); + + createNewCanaryVersion(); + return; + } else { + console.info( + `${name} v${version} is less than the published canary v${canaryVersion}! Creating the next canary version.` + ); + + // set the current version to package.json + setPackageVersion(canaryVersion); + + // create the next canary version + createNewCanaryVersion(); + } +} + +function getCanaryVersion(name) { + const manifestStr = execSync(`npm show ${name} dist-tags --json`).toString(); + const manifest = JSON.parse(manifestStr); + return manifest.canary; +} + +function createNewCanaryVersion() { + execSync(`yarn version --prerelease --preid=canary --no-git-tag-version`); +} + +function setPackageVersion(version) { + execSync(`npm pkg set version=${version}`); +} diff --git a/yarn.lock b/yarn.lock index 69a56943..1649fb16 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2519,6 +2519,11 @@ commander@^4.0.0: resolved "https://registry.yarnpkg.com/commander/-/commander-4.1.1.tgz#9fd602bd936294e9e9ef46a3f4d6964044b18068" integrity sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA== +compare-versions@^6.1.1: + version "6.1.1" + resolved "https://registry.yarnpkg.com/compare-versions/-/compare-versions-6.1.1.tgz#7af3cc1099ba37d244b3145a9af5201b629148a9" + integrity sha512-4hm4VPpIecmlg59CHXnRDnqGplJFrbLG4aFEl5vl6cK1u76ws3LLvX7ikFnTDl5vo39sjWD6AaDPYodJp/NNHg== + concat-map@0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"