This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
forked from aichbauer/node-count-git-tags
/
index.js
61 lines (49 loc) · 1.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import execa from 'execa';
import isGit from 'is-git-repository';
import { platform } from 'os';
import makepath from 'path';
import pathIsAbsolute from 'path-is-absolute';
import shellescape from 'shell-escape';
const cwd = process.cwd();
// escape bad arguments
var escapeShell = function(cmd) {
if(cmd !== undefined){
var arg = cmd.toString().split(" ");
return shellescape(arg);
}
}
const countGitTags = ({ path, local } = {}) => {
let countOfTags = 0;
let thisPath = path || cwd;
thisPath = pathIsAbsolute(thisPath) ? thisPath : makepath.join(cwd, thisPath);
const thisLocal = local === undefined ? true : local;
thisPath = escapeShell(thisPath);
thisLocal = escapeShell(thisLocal);
if (!isGit(thisPath)) {
return 0;
}
try {
let getTagsExec;
let getTags;
if (thisLocal) {
if (platform() === 'win32') {
getTagsExec = `pushd ${thisPath} & git tag`;
} else {
getTagsExec = `(cd ${thisPath} ; git tag)`;
}
getTags = execa.shellSync(getTagsExec).stdout;
} else {
if (platform() === 'win32') {
getTagsExec = `pushd ${thisPath} & git ls-remote --tags`;
} else {
getTagsExec = `(cd ${thisPath} ; git ls-remote --tags)`;
}
getTags = execa.shellSync(getTagsExec).stdout;
}
countOfTags = getTags.length === 0 ? getTags.length : getTags.split('\n').length;
return countOfTags;
} catch (e) {
return 0;
}
};
export default countGitTags;