-
Notifications
You must be signed in to change notification settings - Fork 4
/
circle-branch.js
52 lines (46 loc) · 1.63 KB
/
circle-branch.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
// Script to switch the shared-goodcity branch to the same branch as this project
// Aims to be platform independent with minimal requirements.
// Requirements: nodejs, git, github
var fs = require('fs');
var https = require('https');
var sharedGoodCityRepo = 'crossroads/shared.goodcity';
var packageJsonFile = 'package.json';
var defaultBranch = 'master';
var execSync = require("child_process").execSync;
function exec(cmd) {
return execSync(cmd, {encoding:"utf8"});
}
var currentBranch = exec('git rev-parse --abbrev-ref HEAD').trim() || defaultBranch;
var branches = '';
var options = {
host: 'api.github.com',
port: 443,
path: '/repos/' + sharedGoodCityRepo + '/branches',
headers: {'user-agent':'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)'}
};
https.get(options, function(res) {
var data = '';
res.on('data', function(d) {
data += d;
});
res.on('end', function() {
setBranch(data);
});
}).on('error', function(e) {
console.error(e);
});
function setBranch(branches) {
var matched = JSON.parse(branches)
.map(function(b) { return b.name; })
.filter(function(name) { return name.indexOf(currentBranch) == 0; });
var branchToUse = matched[0] || defaultBranch;
console.log(sharedGoodCityRepo + " using " + branchToUse);
var sharedGoodCityPackage = "git://github.com/" + sharedGoodCityRepo + ".git#" + branchToUse
var packageJson = JSON.parse(fs.readFileSync(packageJsonFile, 'utf8'));
packageJson['dependencies']['shared-goodcity'] = sharedGoodCityPackage;
fs.writeFile(packageJsonFile, JSON.stringify(packageJson, null, 2), function(err) {
if(err) {
console.log(err);
}
});
}