-
Notifications
You must be signed in to change notification settings - Fork 6
/
git.version.ts
61 lines (53 loc) · 1.75 KB
/
git.version.ts
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
import fs = require('fs');
import { Observable } from 'rxjs/Rx';
const exec = require('child_process').exec;
const revision = new Observable<string>(s => {
exec('git rev-parse --short HEAD',
(error: Error, stdout: Buffer, stderr: Buffer) => {
if (error !== null) {
console.log('git error: ' + error + stderr);
}
s.next(stdout.toString().trim());
s.complete();
});
});
const branch = new Observable<string>(s => {
exec('git rev-parse --abbrev-ref HEAD',
(error: Error, stdout: Buffer, stderr: Buffer) => {
if (error !== null) {
console.log('git error: ' + error + stderr);
}
s.next(stdout.toString().trim());
s.complete();
});
});
Observable
.combineLatest(revision, branch)
.subscribe(([rev, bra]) => {
console.log(`version: '${process.env.npm_package_version}', revision: '${rev}', branch: '${bra}'`);
const contentJs = '' +
'// this file is automatically generated by git.version.ts script\n' +
'export const versions = {\n' +
' revision: \'' + rev + '\',\n' +
' branch: \'' + bra + '\'\n' +
'};\n';
const contentJson = '' +
'{\n' +
' "revision": "' + rev + '",\n' +
' "branch": "' + bra + '"\n' +
'}\n';
fs.writeFileSync(
'src/environments/versions.ts',
contentJs,
{encoding: 'utf8'}
);
try {
fs.writeFileSync(
'dist/version.json',
contentJson,
{encoding: 'utf8'}
);
} catch (e) {
// do nothing
}
});