forked from MardukExchange/lnsovbridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseGitCommit.js
43 lines (34 loc) · 1.02 KB
/
parseGitCommit.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
const fs = require('fs');
const childProcess = require('child_process');
const executeCommand = (command) => {
return childProcess.execSync(command, { encoding: 'utf8' });
};
const getCommitHash = () => {
const result = executeCommand('git reflog --decorate -1');
// Do not show the commit hash if that commit is also a tag
const tag = result.match(/tag\:\s[a-zA-Z0-9\-\.]+\,/g);
if (tag && tag.length > 0) {
return '';
} else {
const match = result.match(/[a-z0-9]+\s\(HEAD/g);
if (match) {
return match[0].slice(0, -6);
} else {
return '';
}
}
};
const isDirty = () => {
const result = executeCommand('git status --short');
return result.length > 0;
};
const versionFilePath = `${__dirname}/lib/Version.ts`;
try {
// Delete the version file if it exists
fs.unlinkSync(versionFilePath);
} catch (error) {}
const commitHash = getCommitHash();
fs.writeFileSync(
versionFilePath,
`export default '${commitHash === '' ? '' : '-' + commitHash}${isDirty() ? '-dirty' : ''}';\n`,
);