This repository has been archived by the owner on Nov 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
npmbro.js
290 lines (261 loc) · 7.95 KB
/
npmbro.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
var jerk = require('jerk')
, async = require('async')
, request = require('request')
, EventEmitter = require('events').EventEmitter
, http = require('http')
, _ = require('underscore')
, moment = require('moment')
, SEARCH = 'http://eirikb.github.com/nipster/#'
, THROTTLE_DELAY = 4 * 1000
, options =
{ server: 'irc.freenode.net'
, nick: 'npmbro'
, channels:
[ '#dtrejo'
, '#node.js'
, '#nodejitsu'
, '#robotjs'
, '#voxel.js'
, '#nodecopter'
]
}
, bro = new EventEmitter()
//
// helpers
//
function identity (e) {
return (e || '').trim()
}
function isUrl (s) {
return s.indexOf('http') > -1
}
function noop () {}
//
// Routes
//
function search (m) {
if (!m.match_data[1]) return
m.match_data[1] = m.match_data[1].replace('search', '').trim()
var reply = m.user + ': Please see ' + SEARCH
+ encodeURIComponent(m.match_data[1])
m.say(reply)
return reply
}
function docs (m, cb) {
cb = cb || noop
if (!m.match_data[1]) return cb()
var names =
m.match_data[1].replace('docs', '')
.split(' ')
.filter(identity)
if (!names.length) {
var reply = m.user + ': Usage is: npm docs <packagename>'
m.say(reply)
return cb(null, reply)
}
return async.map(names, function(name, callback) {
var n = name.split("@").shift()
fetchPackageJSON(n, false, function (er, packageJSON) {
if (er) return callback(er)
callback(null, docsUrl(packageJSON))
})
}, onUrls)
function onUrls(err, urls) {
if (err) return cb(err)
urls = urls
.filter(identity)
.filter(isUrl)
if (!urls.length) {
var reply = m.user + ': No results';
m.say(reply)
return cb(err, reply)
}
var reply = m.user + ': Please see ' + urls.join(' ')
m.say(reply)
return cb(err, reply)
}
}
function stats (m, cb) {
cb = cb || noop
if (!m.match_data[1]) return cb
var lib = m.match_data[1].replace('stats', '').trim().split(' ')[0]
if (!lib) {
m.say(m.user + ': Usage is: npm stats <packagename>')
return cb()
}
lib = lib.split('@')[0]
fetchStats(lib, function (err, stats) {
if (err || !stats) {
m.say(m.user + ': Sorry, I can\'t work that one out')
return cb(err)
}
m.say(m.user
+ ': Stats for '
+ lib
+ ': Latest v'
+ stats.version
+ ' published '
+ moment(stats.published).fromNow()
+ ', last pushed to GitHub '
+ moment(stats.ghlastPush).fromNow()
+ ', '
+ stats.ghforks + ' forks, '
+ stats.ghwatchers + ' watchers, '
+ stats.tweets + ' tweets'
)
cb()
})
}
function help (m) {
if (!m.match_data[1]) return
m.match_data[1] = m.match_data[1].replace(/help search|help/, '').trim()
var reply = m.user + ': Please see npm\'s documentation at'
+ ' https://duckduckgo.com/?q='
+ encodeURIComponent('site:http://npmjs.org/doc/ ' + m.match_data[1])
m.say(reply)
return reply
}
function node (m) {
var reply = m.user + ': Please ctrl-f or cmd-f through the node docs at'
+ ' http://nodejs.org/docs/latest/api/all.html'
m.say(reply)
return reply
}
function credits (m) {
m.say(m.user + ': npmbro was written by http://twitter.com/ddtrejo, and'
+ ' named by http://twitter.com/maxogden. The original npm was'
+ ' written by http://twitter.com/izs. Contribute to npmbro at'
+ ' https://github.com/DTrejo/npmbro.')
}
function dtrejosaysyoushouldreallydie (m) {
if (m.user === "DTrejo") {
m.say("Goodbye master.")
return process.exit(0)
}
m.say("Sorry, you're not special.")
}
var routes =
{ search : search
, docs : docs
, install : docs // somewhat handy alias
, stats : stats
, help : help
, node : node
, credits : credits
, dtrejosaysyoushouldreallydie : dtrejosaysyoushouldreallydie
}
function fetchPackageJSON (lib, full, callback) {
request.get("https://registry.npmjs.org/" + lib + (full ? '' : '/latest'), { json: true }, function (err, response, data) {
if (err || response.statusCode != 200) return callback(err || 'not found')
return callback(null, data)
})
}
var repoRegex = /github.com\/([\.\-\w]+)\/([^$\/#]+)/
function repoMatch (s) {
if (!s) return null
if (Array.isArray(s)) s = s.shift()
return s && typeof s == 'string' && s.match(repoRegex)
}
function fetchStats (lib, callback) {
fetchPackageJSON(lib, true, function (err, packageJSON) {
if (err) return callback(err)
var latestVersion = packageJSON['dist-tags'].latest
, latest = packageJSON.versions[latestVersion]
var match = repoMatch(latest.homepage)
if (!match) match = repoMatch(latest.repository)
if (!match) match = latest.repository && repoMatch(latest.repository.url)
if (!match) return callback('not found')
var githubUser = match[1]
, githubRepo = match[2].replace(/\.git$/, '')
, homepage = typeof latest.homepage == 'string' ? latest.homepage : 'https://' + match[0].replace(/\.git$/, '')
, fetchTwitter = function fetchTwitter (callback) {
request(
'http://urls.api.twitter.com/1/urls/count.json?url=' + encodeURIComponent(homepage)
, { json: true }
, function (err, response, data) {
if (err || typeof data.count != 'number')
return callback('Error requesting tweet count from Twitter')
callback(null, data.count)
}
)
}
, fetchGithub = function fetchGithub (callback) {
request(
'https://api.github.com/repos/' + githubUser + '/' + githubRepo
, { json: true }
, function (err, response, data) {
if (err || typeof data.forks != 'number' || typeof data.watchers != 'number')
return callback('Error requesting repo data from GitHub')
callback(null, {
forks : data.forks
, watchers : data.watchers
, url : data.html_url
, lastPush : data.pushed_at
})
}
)
}
, compileStats = function compileStats (err, stats) {
if (err) return callback(err)
callback(null, {
version : latestVersion
, published : packageJSON.time[latestVersion]
, tweets : stats.twitter
, ghforks : stats.github.forks
, ghwatchers : stats.github.watchers
, ghlastPush : stats.github.lastPush
})
}
async.parallel({ twitter: fetchTwitter, github: fetchGithub }, compileStats)
})
}
// mostly lifted from @izs
// https://github.com/isaacs/npm/blob/master/lib/docs.js
function docsUrl (packageJSON) {
var homepage = packageJSON.homepage
, repo = packageJSON.repository || packageJSON.repositories
if (homepage) return homepage
if (repo) {
if (Array.isArray(repo)) repo = repo.shift()
if (repo.hasOwnProperty("url")) repo = repo.url
if (repo) {
return repo.replace(/^git(@|:\/\/)/, 'http://').replace(/\.git$/, '') + "#readme"
}
}
return "http://search.npmjs.org/#/" + packageJSON.name
}
bro.routes = routes
bro.router = router
module.exports = bro
function router (m, cb) {
m.match_data = m.text[0].match(/(?:npmbro) (((?:[a-z0-9A-Z_ -]*)))/)
if (!m.match_data) {
var reply = 'Powered by http://jit.su/ '
+ ' Available commands: '
+ Object.keys(routes)
.filter(function (r) {
return r.indexOf('dtrejo') === -1
})
.map(function(r) {
return 'npm ' + r
}).join(' | ')
m.say(reply)
return cb()
}
var cmd = ((m.match_data[1] || '').split(' ') || [])[0]
var route = routes[cmd]
if (route) return route(m, cb)
else return routes.help(m, cb)
}
if (!module.parent) {
jerk(function (j) {
j.watch_for(/^(?:npmbro)/, function (m) {
// console.log(m)
m.say = _.throttle(m.say, THROTTLE_DELAY);
router(m, noop)
})
}).connect(options)
}
http.createServer(function (req, res) {
res.end('Yes, I am alive and well!')
}).listen(8000)