-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathextract-version-info.js
More file actions
50 lines (36 loc) · 1.24 KB
/
extract-version-info.js
File metadata and controls
50 lines (36 loc) · 1.24 KB
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
const path = require('path');
const fs = require('fs');
const { gitDescribeSync } = require('git-describe');
const { version } = require('./package.json');
const REPO_ABSPATH = __dirname;
const GIT_HASH_ABSPATH = path.join(REPO_ABSPATH, '/git-commit-hash.txt');
const VERSION_FILE_ABSPATH = path.join(REPO_ABSPATH, '/client/src/environments/version-info.ts');
let gitInfo = {};
let gitFailed = false;
try {
const output = gitDescribeSync({
dirtyMark: false,
dirtySemver: false
});
gitInfo.hash = output.hash;
} catch(e) {
console.log('Git describe failed');
gitFailed = true;
}
if (gitFailed) {
// read git commit hash from file
const commitHash = fs.readFileSync(GIT_HASH_ABSPATH, { encoding: 'utf8', flag: 'r' });
if (!commitHash) {
throw new Error();
}
gitInfo.hash = commitHash.trim();
}
if (!gitInfo.hash) {
console.log('Failed to extract git hash');
throw new Error();
}
gitInfo.version = version;
console.log(JSON.stringify(gitInfo, null, 4));
const fileContent = `export const VERSION_INFO = ${JSON.stringify(gitInfo, null, 4)};\r\n`;
fs.writeFileSync(VERSION_FILE_ABSPATH, fileContent);
console.log(`Wrote version info ${gitInfo.version}-${gitInfo.hash} to ${path.relative(REPO_ABSPATH, VERSION_FILE_ABSPATH)}`);