-
Notifications
You must be signed in to change notification settings - Fork 39
/
npm.js
97 lines (67 loc) · 2.11 KB
/
npm.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
/* Copyright (c) 2014-2017 Richard Rodger and other contributors, MIT License */
var Wreck = require('wreck')
module.exports = function npm (options) {
var seneca = this
options = seneca.util.deepextend({
registry: 'http://registry.npmjs.org/'
},options)
seneca.add( 'role:npm,cmd:get', cmd_get )
seneca.add( 'role:npm,cmd:query', cmd_query )
seneca.add( 'role:npm,cmd:extract', cmd_extract )
seneca.add('role:entity,cmd:save,name:npm', override_index)
function cmd_get (msg, reply) {
var seneca = this
seneca
.make$('npm')
.load$(msg.name, function (err, out) {
if (err) return reply(err)
if (!out || msg.update) {
return seneca.act('role:npm,cmd:query', {name: msg.name}, reply)
}
return reply(out)
})
}
function cmd_query (msg, reply) {
var seneca = this
Wreck.get( options.registry + msg.name, function (err, res, payload) {
if(err) return reply(err)
var data = JSON.parse(payload.toString())
seneca.act('role:npm,cmd:extract', {data: data}, function (err, data) {
if(err) return reply(err)
this
.make$('npm')
.load$(msg.name, function (err, npm) {
if (err) return reply(err)
if (!npm) {
data.id$ = msg.name
npm = this.make$('npm')
}
npm
.data$(data)
.save$(reply)
})
})
})
}
function cmd_extract( msg, reply ) {
var seneca = this
var data = msg.data
var dist_tags = data['dist-tags'] || {}
var latest = ((data.versions||{})[dist_tags.latest]) || {}
var repository = latest.repository || {}
var out = {
name: data._id,
version: dist_tags.latest,
giturl: repository.url,
desc: data.description || '',
readme: data.readme || ''
}
reply(null,out)
}
function override_index( msg, reply ) {
this.prior(msg, function(err, npm) {
reply(err,npm)
this.act('role:search,cmd:insert',{data:seneca.util.clean(npm.data$())})
})
}
}