-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
77 lines (63 loc) · 1.88 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Simple module for git revision information
* @author Matthew Davies <[email protected]>
* @version 0.0.2
*/
// Get the necessary process stuff
var process = require('child_process'),
exec = process.exec,
execSync = process.execSync
/**
* Private function to parse the output before sending it out
* @param {string} output the string from stdout
* @return {string}
*/
function parseOutput(output) {
return output.split('\n').join('')
}
/**
* The actual exported module
* @param {string} type the type of revision information wanted
* @param {[Function]} cb either a callback (for async return) or nothing (for sync return)
* @return {[string]} if sync, return a string
*/
module.exports = function(type, cb) {
// The actual shell command that will be run
var command
// Switch off the type and set the shell command
switch(type) {
// Short git SHA1 hash
case 'short':
command = 'git rev-parse --short HEAD'
break;
// Full git SHA1 hash
case 'hash':
case 'long':
command = 'git rev-parse HEAD'
break;
// The current tag if there is a tag,
// otherwise, just returns the current hash
case 'tag':
command = 'git describe --always --tag --abbrev=0'
break;
// Current branch
case 'branch':
command = 'git rev-parse --abbrev-ref HEAD'
break;
// If the type doesn't match anything,
// throw a ReferenceError
default:
throw ReferenceError('Revision type "' + type + '" is invalid')
}
// If the callback is a function, run asynchronously
if(typeof cb == 'function')
exec(command, {cwd: __dirname}, function (err, stdout, stderr) {
if(err)
cb(parseOutput(stderr), err)
else
cb(parseOutput(stdout))
})
// Otherwise return the synchronous results
else
return parseOutput(execSync(command, {cwd: __dirname}).toString())
}