-
Notifications
You must be signed in to change notification settings - Fork 2
/
npmregistry.coffee
90 lines (81 loc) · 2.57 KB
/
npmregistry.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
81
82
83
84
85
86
87
88
89
90
Promise = require 'bluebird'
prequest = require 'request-promise'
request = require 'request'
exec = require('child_process').exec
JSONStream = require('JSONStream')
es = require('event-stream')
fs = require('fs')
Promise.promisifyAll(fs)
module.exports = {
getInfo: (name) ->
prequest("https://registry.npmjs.org/#{name}").then( (res) ->
info = JSON.parse(res)
if info.error?
throw new Error("getting info about #{name} failed: #{info.reason}")
return fs.writeFileAsync("cache/#{name}.json", res)
.then( () => info )
)
getInfoCached: (name) ->
return fs.readFileAsync("cache/#{name}.json").then( (data) =>
return JSON.parse(data)
)
getAllPlugins: () ->
console.log "Fetching plugins"
last = null
count = 0
return new Promise( (resolve, reject) =>
packages = []
request(url: 'https://registry.npmjs.org/-/v1/search?text=pimatic-&size=250', (error, response, body) =>
if error
console.error(error, response.statusCode)
reject(error)
)
.pipe(JSONStream.parse('objects..name'))
.on('error', (err) ->
console.error(err)
reject(err)
)
.pipe es.mapSync (data) ->
count++
last = data
if data.indexOf("pimatic-") is 0
return data
else
return
.pipe es.writeArray (err, data) ->
if(err)
reject(err)
else
if data.length > 10
data.sort()
resolve(data)
else
reject(new Error('Incomplete data from npm registry: ' + data))
)
getPluginListCached: () ->
return fs.readFileAsync('cache/pluginlist.json').then( (data) =>
return JSON.parse(data)
).catch( (err) ->
if(err.code == 'NOENT')
return []
throw err
)
getPluginList: () ->
return this.getAllPlugins().then( (allPlugins) =>
blacklist = [
'pimatic-plugin-template', 'pimatic-rest-api', 'pimatic-speak-api',
"pimatic-datalogger", "pimatic-redirect", "pimatic-datalogger",
"pimatic-homeduino-dst-dev", "pimatic-iwy-light-master", "pimatic-weather",
"pimatic-pilight", "pimatic-plugin-iwy-light-master", "pimatic-dhtxx",
"pimatic-plugin-commons"
]
plugins = (
for p in allPlugins
continue if p.length is 0 or p in blacklist
p
)
plugins = ["pimatic"].concat plugins
return fs.writeFileAsync('cache/pluginlist.json', JSON.stringify(plugins))
.then( () => plugins )
)
}