This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-helper.js
57 lines (42 loc) · 1.58 KB
/
git-helper.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
'use strict';
const semver = require('semver');
const cpexec = require('child_process').exec;
// Promisified child process handler
const exec = (command, options) => new Promise((resolve, reject) => {
cpexec(command, { cwd: options.cwd }, (err, result) => {
if (err) {
reject(err);
}
else {
resolve(result ? result.trim() : '');
}
});
});
// Fetches updates from remote
const fetch = options => exec('git fetch', options).then(() => undefined);
// Gets the latest/highest semver version tag
const latestVersionTag = options =>
exec('git tag --list', options)
.then(stdout => {
stdout = stdout.split(/\n/)
.filter(tag => semver.valid(tag) && semver.satisfies(tag, options.filter, {
includePrerelease: options.includePrerelease === true
})).sort(semver.rcompare)[0];
return semver.clean(stdout ? stdout.trim() : '') || '';
});
// Tags the repository at the current state
const tag = (tag, message, push, options) =>
exec(`git tag -a ${tag} -m "${message}"`, options)
.then(() => {
if (push) {
return exec('git push --tags', options);
}
}).then(() => tag);
// Gets the current local ref/branch
const branch = options => exec('git rev-parse --abbrev-ref HEAD', options);
// Checks to see if the current local state of the repository is in sync with its remote
const checkStatus = options =>
fetch(options).then(() => branch(options)).then(branch =>
exec(`git status --porcelain && git log ${branch}..origin/${options.branch || branch} --oneline`, options))
.then(stdout => !stdout);
module.exports = { tag, branch, fetch, checkStatus, latestVersionTag };