-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
125 lines (105 loc) · 3.26 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var co = require('co')
var request = require('cogent')
var channel = require('chanel')
var cp = require('child_process')
var reset = require('yield-ratelimit-reset')
var parseLink = require('parse-link-header')
var GITHUB_USERNAME = process.env.GITHUB_USERNAME
var GITHUB_PASSWORD = process.env.GITHUB_PASSWORD
if (GITHUB_PASSWORD && GITHUB_USERNAME) {
request = request.extend({
auth: GITHUB_USERNAME + ':' + GITHUB_PASSWORD,
})
}
module.exports = execute
function* execute(org, user, options) {
var state = {}
process.on('uncaughtException', onError)
console.log('organization: ' + org)
console.log('user: ' + user)
var me = yield exec('npm', ['whoami'])
if (!me) throw new Error('log in to npm, man!')
console.log('me: ' + me)
var page = 1;
var ch = channel({
concurrency: 5,
discard: true,
})
while (true) {
console.log('searching page %s of repositories', page)
var res = yield* request('https://api.github.com/search/repositories?q=fork:true+user:' + org + '&page=' + page, true)
if (res.statusCode !== 200) throw new Error('error searching user\'s repos: ' + JSON.stringify(res.headers))
var items = res.body.items
for (var i = 0; i < items.length; i++) {
ch.push(co(add(items[i])))
}
yield* reset(res.headers)
if (!res.headers.link) break
var links = parseLink(res.headers.link)
if (!links.next) break
page++
}
console.log('done searching for repositories')
yield ch(true)
console.log('you have successfully shared your developer life blood with another')
process.removeListener('uncaughtException', onError)
function onError() {
Object.keys(state).forEach(function (repo) {
state[repo] = Date.now() - state[repo]
})
console.log(JSON.stringify(state, null, 2))
setImmediate(function () {
process.exit()
})
}
function* add(data) {
if (!data.size) return
var master = data.default_branch
if (!master) return
var repo = data.name
state[repo] = Date.now()
var res
try {
res = yield* request('https://raw.githubusercontent.com/' + org + '/' + repo + '/' + master + '/package.json', true)
} catch (err) {
return // ignore timeouts and shit
}
delete state[repo]
if (res.statusCode !== 200) {
res.resume()
return
}
var json = res.body
if (json.private) return
var name = json.name
if (!name) return
var owners
try {
owners = yield exec('npm', ['owner', 'ls', name])
} catch (err) {
console.log('repo "' + name + '" isnt published on npm')
return
}
// you don't have rights!
if (!~owners.indexOf(me))
return console.log('you dont have publishing rights to "' + name + '"')
// this particular individual already has rights!
if (~owners.indexOf(user))
return console.log(user + ' already has the rights to "' + name + '"')
try {
yield exec('npm', ['owner', 'add', user, name])
console.log(user + ' added as owner to "' + name + '"')
} catch (err) {
console.error(err)
// who knows why this would happen
}
}
}
function exec(cmd, args, done) {
cp.execFile(cmd, args, function (err, stdout) {
done(err, !err && stdout.toString('utf8').trim())
})
return function (fn) {
done = fn
}
}