From 35270abb4360fc3ad068fbb168b220411294eefe Mon Sep 17 00:00:00 2001 From: Henrique Taunay Date: Wed, 12 Aug 2015 18:04:33 -0300 Subject: [PATCH] Sync operation support, and custom repo paths as well --- README.md | 34 ++++++++++++++++++++++++++++------ index.js | 29 +++++++++++++++++++++++++++-- test.js | 37 +++++++++++++++++++++++++++---------- 3 files changed, 82 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index f37aa3e..3492339 100644 --- a/README.md +++ b/README.md @@ -2,20 +2,18 @@ > Get the most recent git tag of your repository using git-describe(1) - ## Install ```sh $ npm install --save git-latest-tag ``` - ## Usage ### Use a callback ```js -var getLatestTag = require('git-latest-tag'); +var getLatestTag = require('git-latest-tag').getLatestTag; var options = { all: 'ok', contains: true, @@ -32,7 +30,7 @@ getLatestTag(options, function(err, tag) { ### Use as a readable stream ```js -var getLatestTag = require('git-latest-tag'); +var getLatestTag = require('git-latest-tag').getLatestTag; var options = { all: 'ok', contains: true, @@ -44,6 +42,21 @@ getLatestTag(options) .pipe(...); ``` +### Use it synchronously + +```js +var getLatestTagSync = require('git-latest-tag').getLatestTagSync; +var options = { + all: 'ok', + contains: true, + candidates: 10, + 'commit-ish': 'HEAD' +}; + +var tag = getLatestTagSync(options); +console.log(tag); +//=> latestTag +``` ## API @@ -59,18 +72,27 @@ All options will be dash-cased for you. Please check the available options at http://git-scm.com/docs/git-describe. +You can also define a specific Git repo other than the current path through the 'repoPath' option: + +```js +// Options to get latest tag from current branch of a given repo +var options = { + tags: true, + abbrev: 0, + repoPath: '/path/to/repo' +}; +``` + *NOTE*: if a flag takes no value and the passed `options.value` is truthy, it will generate the flag only without any value. If it's falsy the flag will not be included. If it's a `true`, it will suppress long format, only showing the closest tag in refs/tags namespace and will return an empty string if there is no tags but more than one commit (same as `{ tags: true, abbrev: 0 }`). #### callback(err, tag) - ## License MIT © [Steve Mao](https://github.com/stevemao) - [npm-image]: https://badge.fury.io/js/git-latest-tag.svg [npm-url]: https://npmjs.org/package/git-latest-tag [travis-image]: https://travis-ci.org/stevemao/git-latest-tag.svg?branch=master diff --git a/index.js b/index.js index 380ae42..cec4d45 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ 'use strict'; var exec = require('child_process').exec; +var execSync = require('child_process').execSync; var decamelize = require('decamelize'); var _ = require('lodash'); @@ -50,6 +51,18 @@ function getCmd(opts) { return cmd; } +function getExecOptions(opts) { + + var execOpts = {}; + + if (opts instanceof Object && "repoPath" in opts) { + execOpts.cwd = opts.repoPath; + delete opts.repoPath; + } + + return execOpts; +} + function getLatestTag(opts, cb) { if (typeof opts === 'function') { cb = opts; @@ -58,9 +71,10 @@ function getLatestTag(opts, cb) { cb = cb || function() {}; } + var execOpts = getExecOptions(opts); var cmd = getCmd(opts); - return exec(cmd, function(err, stdout) { + return exec(cmd, execOpts, function(err, stdout) { if (err) { cb(err); } else { @@ -69,4 +83,15 @@ function getLatestTag(opts, cb) { }).stdout; } -module.exports = getLatestTag; +function getLatestTagSync(opts) { + + if(opts === null) opts = {}; + + var execOpts = getExecOptions(opts); + var cmd = getCmd(opts); + var stdout = execSync(cmd, execOpts); + + return String(stdout).trim(); +} + +module.exports = { getLatestTag: getLatestTag, getLatestTagSync: getLatestTagSync }; diff --git a/test.js b/test.js index db38645..a096710 100644 --- a/test.js +++ b/test.js @@ -1,11 +1,13 @@ /*global it */ 'use strict'; -var assert = require('assert'); -var through = require('through2'); -var rewire = require('rewire'); -var gitLatestTag = rewire('./'); +var assert = require('assert'); +var through = require('through2'); +var rewire = require('rewire'); -var getCmd = gitLatestTag.__get__('getCmd'); +var gitLatestTagModule = rewire('./'); +var getCmd = gitLatestTagModule.__get__('getCmd'); +var gitLatestTag = gitLatestTagModule.getLatestTag; +var gitLatestTagSync = gitLatestTagModule.getLatestTagSync; it('without options', function() { var cmd = getCmd(); @@ -67,13 +69,28 @@ it('should callback with options', function(done) { }); }); -it('should callback without options', function(done) { +it('should\'t work without options', function(done) { gitLatestTag(function(err, tag) { - if (err) { assert(err); return done(); - } - assert.equal(tag.indexOf('v'), 0); - done(); }); }); + +it('should work syncronously with true flag', function() { + var tag = gitLatestTagSync(true); + assert.equal(tag.indexOf('v'), 0); +}); + +it('should work syncronously with custom repo path', function() { + var tag = gitLatestTagSync({abbrev: 0, tags: true, repoPath: "."}); + assert.equal(tag.indexOf('v'), 0); +}); + +it('shouldn\'t work syncronously with wrong custom repo path', function() { + assert.throws( + function() { + var tag = gitLatestTagSync({abbrev: 0, tags: true, repoPath: "./somewhere"}); + }, + Error + ); +});