-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmonitor.coffee
80 lines (70 loc) · 2.19 KB
/
monitor.coffee
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
Promise = require 'bluebird'
npmregistry = require('./npmregistry')
events = require('events')
onError = (error) -> console.error error.stack
class PluginMonitor extends events.EventEmitter
pluginsList: null
packageInfos: null
constructor: ->
super()
@pluginsList = npmregistry.getPluginListCached().then( (plugins) =>
@packageInfos = Promise.map(@pluginsList, (name) =>
return npmregistry.getInfoCached(name)
).catch( (error) =>
onError(error)
@packageInfos = null
)
return plugins
)
updatePluginList: =>
oldPluginsList = @pluginsList or Promise.resolve([])
pending = npmregistry.getPluginList()
.then( (plugins) =>
return oldPluginsList.then( (oldPlugins) =>
if plugins.length is 0
# something went wrong
plugins = oldPlugins
@pluginsList = Promise.resolve(plugins)
for p in plugins
unless p in oldPlugins
@emit 'new', p
return @pluginsList
)
).catch(onError)
unless @pluginsList?
@pluginsList = pending
# reschedule next hour to update
setTimeout(@updatePluginList, 60*60*1000)
updateNpmInfos: =>
oldPackageInfos = @packageInfos or Promise.resolve([])
pending = oldPackageInfos.then( (oldPackageInfos) =>
return @pluginsList.map( (name) =>
return npmregistry.getInfo(name).then( (info) =>
@_checkForUpdate(oldPackageInfos, info)
return info
).catch( (error) =>
onError(error)
return Promise.resolve()
)
).filter( (p) => p? ).then( (infos) =>
return @packageInfos = Promise.resolve(infos)
)
).catch(onError)
unless @packageInfos?
@packageInfos = pending
# reschedule next 5min
setTimeout(@updateNpmInfos, 5*60*1000)
start: ->
@updatePluginList()
@updateNpmInfos()
_checkForUpdate: (oldPackageInfos, info) ->
for op in oldPackageInfos
if op.name is info.name
if op.version isnt info.version
@emit 'update', info
return
@emit 'update', info
return
getPluginList: -> @pluginsList
getNpmInfos: -> @packageInfos
module.exports = {PluginMonitor}