Skip to content

Commit

Permalink
chore(ci): add tag resolution for npm releases based on package version;
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalBrainJS committed May 19, 2024
1 parent 540c7a3 commit d82f75a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 35 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
35 changes: 1 addition & 34 deletions bin/pr.js
Original file line number Diff line number Diff line change
@@ -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 = {};
Expand Down
42 changes: 42 additions & 0 deletions bin/repo.js
Original file line number Diff line number Diff line change
@@ -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/);
}
18 changes: 18 additions & 0 deletions bin/resolveNPMTag.js
Original file line number Diff line number Diff line change
@@ -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`);

0 comments on commit d82f75a

Please sign in to comment.