Created
February 11, 2016 17:04
-
-
Save ngryman/96c13e76e0dc8a309d9b to your computer and use it in GitHub Desktop.
Search something on Github - wip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkValidity(url) { | |
return /https:\/\/github.com\/[\w-.]+\/[\w+.]/i.test(url) | |
} | |
function setup(baton) { | |
baton.baseUrl = 'https://api.github.com/repos/' + baton.owner + '/' + baton.repo + '/git' | |
return Promise.resolve(baton) | |
} | |
function fetchMasterRef(baton) { | |
return fetch(baton.baseUrl + '/refs/heads/master') | |
.then(function(res) { return res.json() }) | |
.then(function(json) { | |
baton.sha = json.object.sha | |
return baton | |
}) | |
} | |
function fetchCandidates(baton) { | |
return fetchTree(baton).then(searchCandidates) | |
} | |
function fetchTree(baton) { | |
var url = baton.baseUrl + '/trees/' + baton.sha | |
return fetch(url) | |
.then(function(res) { return res.json() }) | |
.then(function(json) { | |
baton.tree = json.tree | |
return baton | |
}) | |
} | |
function searchCandidates(baton) { | |
var rDirs = /dist|build/ | |
var rFiles = /\.min\.js/ | |
var candidates = baton.tree.reduce(function(candidates, item) { | |
if (rFiles.test(item.path)) { | |
candidates.push(item) | |
} | |
else if (rDirs.test(item.path)) { | |
var req = fetchCandidates(Object.assign({}, { sha: item.sha }, baton)) | |
candidates.push(req) | |
} | |
return candidates | |
}, []) | |
return Promise.all(candidates) | |
} | |
function displayCandidates(baton) { | |
console.log(baton) | |
} | |
setup({ owner: 'ngryman', repo: 'jquery.finger' }) | |
.then(fetchMasterRef) | |
.then(fetchCandidates) | |
.then(function(res) { | |
// console.log(res) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment