-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·83 lines (68 loc) · 2.13 KB
/
index.js
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
81
82
83
#!/usr/bin/env node
'use strict'
const ora = require('ora')
const ghGot = require('gh-got')
const simpleGit = require('simple-git')
const argv = require('minimist')(process.argv.slice(2))
const Multispinner = require('multispinner')
const { dots } = require('cli-spinners/spinners.json')
const { Promise, promisify } = require('bluebird')
const { assert } = require('hoek')
const mkdirp = require('mkdirp')
const { workingDir, organisation, token } = argv
assert(workingDir, '--workingDir is required')
assert(organisation, '--organisation is required')
function findLink (linkHeader, type) {
if (!linkHeader) { return null }
const links = linkHeader.split(',')
const nextLink = links.find(l => l.includes(`rel="${type}"`))
if (!nextLink) { return null }
return nextLink.match(/page=([0-9]+)/)[1]
}
async function fetchPage (page) {
const options = token ? { token } : {}
const res = await ghGot(`orgs/${organisation}/repos?page=${page}`, options)
const nextPage = findLink(res.headers.link, 'next')
const repos = res.body.map(repo => ({
url: repo.ssh_url,
name: repo.name
}))
return { nextPage, repos }
}
async function init () {
await createEnv()
const git = simpleGit(workingDir)
let list = []
let nextPage = 1
const spinner = ora('Finding repositories').start()
while (nextPage) {
const result = await fetchPage(nextPage)
const repos = result.repos.map(repo => {
spinner.text = `Found ${list.length} repositories`
return repo
})
list = [].concat(list, repos)
nextPage = result.nextPage
}
spinner.succeed(`Found ${list.length} repositories`)
const multispinner = new Multispinner(list.map(l => l.name), {
preText: 'Cloning',
interval: dots.interval,
frames: dots.frames
})
await Promise.map(list, async repo => {
try {
const clone = promisify(git.clone, { context: git })
await clone(repo.url)
multispinner.success(repo.name)
} catch (e) {
multispinner.error(repo.name)
}
}, { concurrency: 10 })
console.info('Finished')
}
async function createEnv () {
const mkdirPrms = promisify(mkdirp)
await mkdirPrms(workingDir)
}
init()