Skip to content

Instantly share code, notes, and snippets.

@danyshaanan
Last active January 1, 2017 09:35
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 danyshaanan/6377287 to your computer and use it in GitHub Desktop.
Save danyshaanan/6377287 to your computer and use it in GitHub Desktop.
A NodeJS script to clone/pull all of your public gists at gist.github.com: `node rip_gists.js danyshaanan`.
gists.json
target
#!/usr/bin/env node
// Clones/pull all public gists of a user
const fs = require('fs')
const username = (process.argv[2] || '').replace(/[^0-9A-Za-z_]/g,'')
if (!username) throw new Error('no username supplied!')
const ensureDir = dir => (fs.existsSync(dir) || fs.mkdirSync(dir), dir)
const dir = ensureDir(`${ensureDir('target')}/${username}`)
const gistsJson = `./${dir}/gists.json`
const throwWith = (com, e) => { throw new Error(`exec error for command "${command}": ${e}`) }
const execute = (command, cb = _ => _) => {
console.log(`executing command: ${command}`)
require('child_process').exec(command, e => { e ? throwWith(command, e) : cb() })
}
const getGist = ({id}) => execute(`cd ${dir}/${id} && git pull || git clone git@gist.github.com:/${id}.git ${dir}/${id}`)
const wgetCommand = `wget https://api.github.com/users/${username}/gists -O ${gistsJson}`
execute(wgetCommand, _ => { require(gistsJson).forEach(getGist) })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment