forked from excaliburjs/Excalibur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.js
77 lines (65 loc) · 2.46 KB
/
version.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const { execSync } = require('child_process');
const request = require('sync-request');
const appveyorBuild = process.env.APPVEYOR_BUILD_NUMBER || '';
const travisBuild = process.env.TRAVIS_BUILD_NUMBER || '';
const commit = process.env.TRAVIS_COMMIT || '';
const travisPr = process.env.TRAVIS_PULL_REQUEST || 'false';
const travisTag = process.env.TRAVIS_TAG || '';
// ignore local builds
if (!appveyorBuild && !travisBuild) {
console.info('Attempting to find closest Git tag');
let version = 'local';
try {
execSync('git fetch');
const commit = execSync('git rev-parse HEAD').toString().trim();
const tag = execSync(`git describe --tags ${commit} --abbrev=0`).toString().trim();
if (tag) {
version = tag.substr(1) + '-' + commit.substring(0, 7);
}
} catch (err) {
console.error(err);
}
console.info(`Local build, using version "${version}"`);
module.exports = version;
return;
}
// ignore community PR builds
if (!process.env.GH_TOKEN && travisPr !== 'false') {
console.info('Travis PR build, using version', 'pr-' + travisPr);
module.exports = 'pr-' + travisPr;
return;
}
// use release tag if given
if (travisTag) {
const version = travisTag.match(/^v?([0-9\.]+)$/)[1];
console.info("Using Travis tag version", travisTag, version);
module.exports = version;
return;
}
// fail build if no GH_TOKEN present
if (!process.env.GH_TOKEN) {
throw Error('Missing environment variable GH_TOKEN');
}
// Fetch latest GH release tag version
var res = request('GET', 'https://api.github.com/repos/excaliburjs/Excalibur/releases/latest', {
headers: {
'User-Agent': 'excaliburjs/0.1',
'Authorization': 'token ' + process.env.GH_TOKEN
}
});
const statusCode = res.statusCode;
if (statusCode !== 200) {
throw Error('Fatal error fetching GH release version, status: ' + statusCode);
}
const tag_name = JSON.parse(res.getBody()).tag_name;
const version = tag_name.match(/^v?([0-9\.]+)$/)[1]; // strip v prefix
// Nuget doesn't yet support the + suffix in versions
const appveyVersion = version + '.' + appveyorBuild + '-alpha';
const travisVersion = version + '-alpha.' + travisBuild + '+' + commit.substring(0, 7);
if (appveyorBuild) {
console.info('Using Appveyor build as version', appveyVersion);
module.exports = appveyVersion;
} else {
console.info('Using Travis build as version', travisVersion);
module.exports = travisVersion;
}