Skip to content

Instantly share code, notes, and snippets.

@ngryman
Created February 11, 2016 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngryman/96c13e76e0dc8a309d9b to your computer and use it in GitHub Desktop.
Save ngryman/96c13e76e0dc8a309d9b to your computer and use it in GitHub Desktop.
Search something on Github - wip
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