-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
48 lines (41 loc) · 1.12 KB
/
index.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
'use strict';
var GitHub = require('github-base');
var isObject = require('isobject');
var commits = require('commits');
module.exports = function(owner, repo, options) {
var opts = normalize(owner, repo, options);
var github = new GitHub(opts);
var url = '/repos/:owner/:repo/git/trees/:sha';
if (opts.recursive === true) {
url += '?recursive=1';
}
return commits(opts)
.then(function(commits) {
if (!opts.sha) {
opts.sha = commits[0].sha;
}
return new Promise(function(resolve, reject) {
github.get(url, opts, function(err, files) {
if (err) {
reject(err);
return;
}
resolve(files);
});
});
});
};
function normalize(owner, repo, options) {
if (isObject(repo)) {
options = Object.assign({}, repo, options);
var segs = owner.split('/');
owner = segs.shift();
repo = segs.pop();
} else if (isObject(owner)) {
options = Object.assign({}, owner, repo, options);
owner = null;
repo = null;
}
var defaults = {owner: owner, repo: repo};
return Object.assign({}, defaults, options);
}