Last active
January 1, 2017 09:35
-
-
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`.
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
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
gists.json | |
target |
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
#!/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 [email protected]:/${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