-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (69 loc) · 1.83 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
var fs = require('fs')
var Joke = require('joke')
var follow = require('follow')
var request = require('request')
var SequenceFile = require('seq-file')
var config = JSON.parse(fs.readFileSync(process.argv[2]))
var log = new Joke()
log.pipe(Joke.stringify()).pipe(process.stdout)
log.info('npmco.de-follower starting')
var sequence = new SequenceFile(config.sequenceFile)
var feed
var seq = sequence.readSync()
log.info('following', { registry: config.registry, since: seq })
feed = follow({
db: config.registry,
since: seq,
include_docs: true
})
feed.on('change', function (change) {
sequence.save(change.seq)
var doc = change.doc
// TODO: find out which version was actually published
var version = doc['dist-tags'] && doc['dist-tags'].latest
if (!version) return
log.info('change', {
id: change.id,
seq: change.seq,
name: doc.name,
version: version
})
request({
url: config.indexer + '/unindex',
method: 'POST',
json: true,
body: { name: doc.name }
}, function (err, res, body) {
if (err) {
log.error('error while unindexing', err)
return
}
if (res.statusCode === 201)
log.info('unindexed', { name: doc.name })
else {
log.error('error while unindexing', { statusCode: res.statusCode, body: body })
return
}
request({
url: config.indexer + '/index',
method: 'POST',
json: true,
body: {
name: doc.name,
version: version
}
}, function (err, res, body) {
if (err) {
log.error('error while indexing', err)
return
}
if (res.statusCode === 201)
log.info('indexed', { name: doc.name, version: version })
else
log.error('error while indexing', { statusCode: res.statusCode, body: body })
})
})
})
feed.on('error', function (err) {
throw err
})