-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (43 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
'use strict';
const {spawnSync} = require('child_process');
module.exports = baseDir => {
const result = {};
result.date = getCommandOutput('date');
result.hostname = getCommandOutput('hostname');
result.username = getCommandOutput('whoami');
const useBaseDirOptions = {cwd: baseDir};
result.commit = {};
result.commit.hash = getCommandOutput(
'git', ['rev-parse', 'HEAD'],
useBaseDirOptions);
result.commit.subject = getCommandOutput(
'git', ['log', '--pretty=%s', '--max-count', '1'],
useBaseDirOptions);
result.commit.body = getCommandOutput(
'git', ['log', '--pretty=%b', '--max-count', '1'],
useBaseDirOptions);
result.commit.author = getCommandOutput(
'git', ['log', '--pretty=%an', '--max-count', '1'],
useBaseDirOptions);
result.commit.date = getCommandOutput(
'git', ['log', '--pretty=%aD', '--max-count', '1'],
useBaseDirOptions);
result.commit.changes = getCommandOutput(
'git', ['diff', 'HEAD~', '--shortstat'],
useBaseDirOptions);
return result;
};
function getCommandOutput(command, args = [], options) {
const spawnedCommand = spawnSync(command, args, options);
if (spawnedCommand.error) {
console.error(`Command errored:`);
console.error(` ${command}`);
console.error(spawnedCommand.error.toString());
}
if (spawnedCommand.stdout) {
const commandOutput = spawnedCommand.stdout.toString();
const singleLineOutput = commandOutput.trim().replace(/\r?\n|\r/g, ' ');
return singleLineOutput;
}
return null;
}