Skip to content

Instantly share code, notes, and snippets.

@ybootin ybootin/main.js
Last active Jan 29, 2017

Embed
What would you like to do?
Github clone - clone and update all your github repos
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
})
{
"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
You can’t perform that action at this time.