From 097b0d18e93d12c53b77741d6bfdc8a1fc11828b Mon Sep 17 00:00:00 2001 From: Dmitriy Mozgovoy Date: Sun, 19 May 2024 20:34:58 +0300 Subject: [PATCH] chore(ci): add tag resolution for npm releases based on package version; (#6404) --- .github/workflows/publish.yml | 6 ++++- bin/pr.js | 35 +---------------------------- bin/repo.js | 42 +++++++++++++++++++++++++++++++++++ bin/resolveNPMTag.js | 18 +++++++++++++++ 4 files changed, 66 insertions(+), 35 deletions(-) create mode 100644 bin/repo.js create mode 100644 bin/resolveNPMTag.js diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index ada28b2..63ef2fb 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -42,6 +42,10 @@ jobs: id: tag_version with: tag: "v${{ steps.package-version.outputs.current-version }}" + ############# RESOLVE NPM TAG ############## + - name: NPM TAG + id: 'npm_tag' + run: node ./bin/resolveNPMTag.js ############# GITHUB RELEASE ############## - name: "Create a GitHub release v${{ steps.package-version.outputs.current-version }}" uses: ncipollo/release-action@v1 @@ -53,7 +57,7 @@ jobs: ${{ steps.extract-release-notes.outputs.release_notes }} ############# NPM RELEASE ############## - name: Publish the release to NPM - run: npm publish --provenance --access public + run: npm publish --provenance --access public --tag ${{ steps.npm_tag.outputs.tag || 'latest' }} env: NODE_AUTH_TOKEN: ${{secrets.npm_token}} notify: diff --git a/bin/pr.js b/bin/pr.js index 4a6db09..aae0c67 100644 --- a/bin/pr.js +++ b/bin/pr.js @@ -1,41 +1,8 @@ -import util from "util"; -import cp from "child_process"; import Handlebars from "handlebars"; import fs from "fs/promises"; import prettyBytes from 'pretty-bytes'; import {gzipSize} from 'gzip-size'; - -const exec = util.promisify(cp.exec); - -const getBlobHistory = async (filepath, maxCount= 5) => { - const log = (await exec( - `git log --max-count=${maxCount} --no-walk --tags=v* --oneline --format=%H%d -- ${filepath}` - )).stdout; - - const commits = []; - - let match; - - const regexp = /^(\w+) \(tag: (v?[.\d]+)\)$/gm; - - while((match = regexp.exec(log))) { - commits.push({ - sha: match[1], - tag: match[2], - size: await getBlobSize(filepath, match[1]) - }) - } - - return commits; -} - -const getBlobSize = async (filepath, sha ='HEAD') => { - const size = (await exec( - `git cat-file -s ${sha}:${filepath}` - )).stdout; - - return size ? +size : 0; -} +import {getBlobHistory} from './repo.js'; const generateFileReport = async (files) => { const stat = {}; diff --git a/bin/repo.js b/bin/repo.js new file mode 100644 index 0000000..884d9e4 --- /dev/null +++ b/bin/repo.js @@ -0,0 +1,42 @@ +import util from "util"; +import cp from "child_process"; + +export const exec = util.promisify(cp.exec); + +export const getBlobSize = async (filepath, sha ='HEAD') => { + const size = (await exec( + `git cat-file -s ${sha}:${filepath}` + )).stdout; + + return size ? +size : 0; +} + +export const getBlobHistory = async (filepath, maxCount= 5) => { + const log = (await exec( + `git log --max-count=${maxCount} --no-walk --tags=v* --oneline --format=%H%d -- ${filepath}` + )).stdout; + + const commits = []; + + let match; + + const regexp = /^(\w+) \(tag: (v?[.\d]+)\)$/gm; + + while((match = regexp.exec(log))) { + commits.push({ + sha: match[1], + tag: match[2], + size: await getBlobSize(filepath, match[1]) + }) + } + + return commits; +} + +export const getTags = async (pattern = 'v*', sort = '-v:refname') => { + const log = (await exec( + `git tag -l ${pattern} --sort=${sort}` + )).stdout; + + return log.split(/\r?\n/); +} diff --git a/bin/resolveNPMTag.js b/bin/resolveNPMTag.js new file mode 100644 index 0000000..608d3a4 --- /dev/null +++ b/bin/resolveNPMTag.js @@ -0,0 +1,18 @@ +import {exec, getTags} from "./repo.js"; +import fs from "fs"; +import {colorize} from "./helpers/colorize.js"; + +const {version} = JSON.parse(fs.readFileSync('./package.json')); + +const [major] = version.split('.'); +const tags = await getTags(); +const latestTag = (tags[0] || '').replace(/^v/, ''); + +const isBeta = !/^v?(\d+).(\d)+.(\d)+$/.test(version); +const isLatest = latestTag === version; + +let tag = isBeta ? 'next' : isLatest ? 'latest' : `v${major}`; + +console.log(colorize()`Version [${version}] [${isBeta ? 'prerelease' : 'release'}] latest [${latestTag}]=> NPM Tag [${tag}]`); + +await exec(`echo "tag=${tag}" >> $GITHUB_OUTPUT`);