Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (69 loc) · 2.19 KB
/
Copy pathindex.js
File metadata and controls
80 lines (69 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const r = require('request')
const conf = require('commander')
const pkg = require('./package')
const cp = require('child_process')
const fs = require('fs')
const path = require('path')
const progress = require('progress')
const sleep = require('sleep')
const chalk = require('chalk')
conf
.version(pkg.version)
.option('-u, --username <username>', 'github username')
.option('-p, --password <password>', 'github password')
.option('-d, --domain <domain>' , 'github host')
.parse(process.argv)
const getCurrentPage = (currentPage) => {
r.get({
url: conf.domain
? `http://${conf.username}:${conf.password}@${conf.domain}/api/v3/users/${conf.username}/gists?page=${currentPage}&per_page=100`
: `https://${conf.username}:${conf.password}@api.github.com/users/${conf.username}/gists?page=${currentPage}&per_page=100`,
json: true,
headers: {
'User-Agent': 'backup-gists',
'Accept': 'application/vnd.github.v3+json'
}
}, (err, res, body) => {
if (err || res.statusCode !== 200) {
throw new Error(`
Failed:
err: ${err},
status: ${res && res.statusCode}
body: ${body}
`)
}
// try make backup dir
const BACKUP_DIR = `backup-${(new Date()).toString().split(' ')[4].replace(/:/g, '-')}`
try {
fs.mkdirSync(BACKUP_DIR)
} catch (e) {
if (e.code !== 'EEXIST') throw new Error(`mkdir ${BACKUP_DIR} failed: ${e}`)
}
if(! body instanceof Array) {
throw new TypeError(`unexpected body format: ${typeof body}`)
}
console.log(chalk.green(`Download page: ${currentPage}.`))
const pg = new progress(chalk.green('Backup [:bar] :percent'), {
complete: '=',
incomplete: ' ',
width: 20,
total: body.length
})
pg.tick()
body.forEach( (gist, idx) => {
sleep.sleep(1)
cp.exec(`git clone ${gist.git_pull_url}`, {
cwd: path.join(__dirname, BACKUP_DIR)
}, (err, stdout, stderr) => {
if(err) {
console.error(chalk.red(`clone ${gist.git_pull_url} failed: ${err || stderr}`))
}
})
pg.tick()
})
if (body.length === 100) {
getCurrentPage(currentPage+1)
}
})
}
getCurrentPage(1)