shrewdbeans
shrewdbeans

Reputation: 12549

Is there a way to read Git tags with JavaScript

I'm going to start tagging a codebase using Git tags, but I would also like to display this tag somewhere within my app, so that the testers know which version they are testing.

Is there a way to read what the current Git tag using JavaScript?

FYI, I use Grunt.js to build the app. However, a vanilla JS solution would preferable.

Upvotes: 3

Views: 3556

Answers (2)

matsev
matsev

Reputation: 33789

isomorphic-git has a listTags() function that does just that. The package is available as a npm package. Example snippet copied from the API documentation:

let tags = await git.listTags({ dir: '.' })
console.log(tags)

Currently, isomorphic-git does not support creating tags (at the time of writing, the latest release of the package is version 0.37.3). However, there is a GitHub issue implying that the work has been started and it will be added in the future.

Upvotes: 2

theaccordance
theaccordance

Reputation: 889

Using the git-rev-sync package, you can create a custom task to retrieve the current tag and store it as a grunt.option to use elsewhere in your grunt tasks:

var git = require('git-rev-sync');

function currentGitTag() {
    var currentTag = git.tag();
    grunt.option('tag', currentTag);
}

grunt.registerTask('gitTag', currentGitTag);

To access the value within grunt: grunt.option('tag');.

You can also access the value via template strings: <%= grunt.option('tag') %>

Upvotes: 1

Related Questions