forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-package-version.js
executable file
·76 lines (67 loc) · 2.12 KB
/
set-package-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
'use strict';
const fs = require('fs');
const versionParser = require('./version-parser.js');
const packageJson = require('../package.json');
const TRAVIS_MODE = process.env.TRAVIS_MODE;
let newVersion = '';
try {
if (TRAVIS_MODE === 'release') {
// write the version field in the package json to the version in the git tag
const tag = process.env.TRAVIS_TAG;
if (!versionParser.isValidVersion(tag)) {
throw new Error('Unsuported tag for release: ' + tag);
}
// remove v
newVersion = tag.substring(1);
} else if (TRAVIS_MODE === 'releaseCanary' || TRAVIS_MODE === 'netlifyPr') {
// bump patch in version from latest git tag
let currentVersion = getLatestVersionTag();
// remove v
currentVersion = currentVersion.substring(1);
let matched = false;
newVersion = currentVersion.replace(/^(\d+)\.(\d+)\.(\d+).*$/, function(_, major, minor, patch) {
matched = true;
return major + '.' + minor + '.' + (parseInt(patch, 10) + 1);
});
if (!matched) {
throw new Error('Error calculating version.');
}
if (TRAVIS_MODE === 'netlifyPr') {
newVersion += `-pr.${getCommitHash().substr(0, 8)}`;
} else {
newVersion += `-canary.${getCommitNum()}`;
}
} else {
throw new Error('Unsupported travis mode: ' + TRAVIS_MODE);
}
packageJson.version = newVersion;
fs.writeFileSync('./package.json', JSON.stringify(packageJson));
console.log('Set version: ' + newVersion);
} catch(e) {
console.error(e);
process.exit(1);
}
process.exit(0);
function getCommitNum() {
return parseInt(exec('git rev-list --count HEAD'), 10);
}
function getCommitHash() {
return exec('git rev-parse HEAD');
}
function getLatestVersionTag() {
let commitish = '';
while(true) {
const tag = exec('git describe --abbrev=0 --match="v*" ' + commitish);
if (!tag) {
throw new Error('Could not find tag.');
}
if (versionParser.isValidStableVersion(tag)) {
return tag;
}
// next time search older tags than this one
commitish = tag + '~1';
}
}
function exec(cmd) {
return require('child_process').execSync(cmd).toString().trim();
}