Last active
January 29, 2017 21:14
-
-
Save ybootin/0c36cf2726e7b072fe1262daf10c30b6 to your computer and use it in GitHub Desktop.
Github clone - clone and update all your github repos
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
var github = require('octonode'); | |
var fs = require('fs'); | |
var execsync = require('child_process').execSync; | |
var parse = require('parse-link-header'); | |
var token = "xxxxxxxxxxxxxx" | |
var path = "/Users/xxx/github" | |
var debug = true | |
if (debug) { | |
var baseExecSync = execsync | |
execsync = function(args, opt) { | |
console.log(args, opt) | |
baseExecSync(args, opt) | |
} | |
} | |
if (!fs.existsSync(path)) { | |
throw 'path' + path + ' not found !' | |
} | |
var client = github.client(token); | |
var baseUrl = '/user/repos' | |
function fetchContent(url) { | |
var repos = {} | |
var currentPage = 1 | |
var lastPage | |
function internalFetch(resolve, reject) { | |
client.get(url , { | |
page: currentPage, | |
access_token: token | |
}, function (err, status, body, headers) { | |
if (err) { | |
return reject(err) | |
} | |
body.forEach(function(item) { | |
repos[item.name] = item.ssh_url | |
}) | |
var links = parse(headers.link) | |
lastPage = lastPage || parseInt(links.last.page, 10) | |
if (++currentPage <= lastPage) { | |
internalFetch(resolve, reject) | |
} else { | |
resolve(repos) | |
} | |
}) | |
} | |
return new Promise(function(resolve, reject) { | |
internalFetch(resolve, reject) | |
}) | |
} | |
fetchContent(baseUrl).then(function(repos) { | |
Object.keys(repos).forEach(function(name) { | |
var repospath = path + '/' + name | |
// clone repo if not exists | |
if (!fs.existsSync(repospath)) { | |
execsync('git clone ' + repos[name], { | |
cwd: path | |
}) | |
} else { | |
execsync('git fetch origin', { | |
cwd: repospath | |
}) | |
} | |
}) | |
}).catch(function(msg) { | |
throw msg | |
}) |
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
{ | |
"name": "github-clone", | |
"version": "1.0.0", | |
"description": "", | |
"main": "main.js", | |
"dependencies": {}, | |
"devDependencies": { | |
"octonode": "^0.7.8", | |
"parse-link-header": "^0.4.1", | |
"promise": "^7.1.1", | |
"promise-es6": "^0.1.0" | |
}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment