Skip to content

Instantly share code, notes, and snippets.

@toky-nomena
Created March 24, 2025 22:09
Show Gist options
  • Save toky-nomena/2ffd8d45c199fea5ad8115505ebef28c to your computer and use it in GitHub Desktop.
Save toky-nomena/2ffd8d45c199fea5ad8115505ebef28c to your computer and use it in GitHub Desktop.
const { exec } = require("child_process");
const { promisify } = require("util");
const path = require("path");
const execAsync = promisify(exec);
// Base directory for repositories
const baseDir = path.resolve("~/dev/projects");
// List of repositories (relative to baseDir)
const repositories = [
"repo-1",
"repo-2"
];
// Branch to check latest tag
const branch = "origin/main";
// Function to get the latest tag on a specific branch with strict format vX.XX.X
async function getLatestTag(repoPath, branch) {
try {
await execAsync("git fetch --tags", { cwd: repoPath });
const tagCommand = "git tag --list 'v[0-9]+\.[0-9]+\.[0-9]+' --sort=-v:refname | head -n 1";
const { stdout } = await execAsync(tagCommand, { cwd: repoPath });
return stdout.trim().match(/^v\d+\.\d{2}\.\d+$/) ? colorize(stdout.trim(), 32) : colorize("No matching tags found.", 33); // Green or Yellow
} catch (error) {
return colorize(`Error fetching latest tag: ${error.message}`, 31); // Red
}
}
// Function to colorize output
function colorize(text, colorCode) {
return `\x1b[${colorCode}m${text}\x1b[0m`;
}
// Loop through repositories and get latest tag using async/await
(async () => {
for (const repo of repositories) {
const repoPath = path.join(baseDir, repo);
console.log(colorize(`\n${path.basename(repoPath)}: Latest tag on ${branch}`, 36)); // Cyan
console.log(await getLatestTag(repoPath, branch));
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment