-
Notifications
You must be signed in to change notification settings - Fork 0
/
createVersionFile.js
44 lines (37 loc) · 1.06 KB
/
createVersionFile.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
const git = require('git-rev-sync');
const yaml = require('js-yaml');
const readFileSync = require('fs').readFileSync;
const writeFileSync = require('fs').writeFileSync;
function getCommitHash() {
let commitHash = 'unknown';
try {
commitHash = git.long();
} catch (e) {
console.log('Failed to retrieve commit hash: ', e);
}
return commitHash;
}
function getCommitDate() {
let commitDate = 'unknown';
try {
commitDate = git.date()?.toUTCString();
} catch (e) {
console.log('Failed to retrieve commit date: ', e);
}
return commitDate;
}
function getAppVersion() {
const packageJsonFilePath = `${process.env.NODE_PATH || '.'}/package.json`;
const packageJsonData = JSON.parse(readFileSync(packageJsonFilePath));
return packageJsonData.version;
}
function createVersionFile() {
const versionFilePath = `${process.env.NODE_PATH || '.'}/version`;
const fileData = {
version: getAppVersion(),
commit: getCommitHash(),
date: getCommitDate(),
};
writeFileSync(versionFilePath, yaml.dump(fileData, {}));
}
createVersionFile();