-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
128 lines (108 loc) · 3.3 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
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
const fs = require('fs')
const cors = require('cors')
const path = require('path')
const http = require('http')
const morgan = require('morgan')
const express = require('express')
const bodyParser = require('body-parser')
const superagent = require('superagent')
const nodeRedModule = require('node-red-module-parser')
const port = (process.env.PORT || 80)
const listenHost = (process.env.HOST || '0.0.0.0')
const registryHost = (process.env.REGISTRY || 'http:https://registry:4873')
const keyword = (process.env.KEYWORD || "node-red")
const url = registryHost + "/-/all"
const catalogue = {
"name":"Ben's custom catalogue",
"updated_at": new Date().toISOString(),
"modules": [
// {
// "id": "@ben/ben-node-random",
// "version": "1.0.0",
// "description": "A node-red node that generates random numbers",
// "keywords": [
// "node-red",
// "random"
// ],
// "updated_at": "2020-09-21T18:37:50.673Z",
// "url": "http:https://flows.hardill.me.uk/node/ben-red-random"
// }
]
}
function update() {
//reset list
catalogue.modules = [];
superagent.get(url)
.end(async (err, res) => {
if (!err) {
const nodes = res.body;
var nodeNames = Object.keys(nodes);
const index = nodeNames.indexOf("_updated");
if (index > -1) {
nodeNames.splice(index, 1);
}
for (const node in nodeNames) {
var n = nodes[nodeNames[node]];
if (n.keywords) {
if (n.keywords.indexOf(keyword) != -1) {
try {
let details = await superagent
.get("http:https://" + registryHost + "/" + nodeNames[node])
.set('accept', 'json')
let latest = details.body['dist-tags'].latest
let version = details.body.versions[latest]
let tar = version.dist.tarball
let tarDir = path.join("temp", nodeNames[node])
console.log(tarDir)
fs.mkdirSync(tarDir,{recursive: true})
let tarPath = path.join(tarDir, nodeNames[node].split('/').slice(-1) + ".tgz")
console.log(tarPath)
let tarRes = await superagent.get(tar).responseType('blob')
fs.writeFileSync(tarPath, tarRes.body)
let moduleDetails = nodeRedModule.examinTar(tarPath, "temp")
fs.rmSync(tarPath)
var entry = {
id: n.name,
version: n["dist-tags"].latest,
description: n.description,
keywords: n.keywords,
updated_at: n.time.modified,
url: "http:https://" + registryHost + "/-/web/details/" + n.name
}
if (moduleDetails.types) {
entry.types = moduleDetails.types
}
if (moduleDetails["node-red"]) {
catalogue.modules.push(entry)
}
} catch (e) {
console.log("err",e)
}
}
}
}
console.log(JSON.stringify(catalogue, null, 2));
} else {
console.log(err);
}
});
}
const app = express()
app.use(morgan("combined"))
app.use(bodyParser.json())
app.post('/update', (req, res, next) => {
const updateRequest = req.body
console.log(JSON.stringify(updateRequest,null, 2))
update()
res.status(200).send();
})
app.get('/catalogue.json', cors(), (req, res, next) => {
res.send(catalogue)
})
// app.head('/catalogue.json', (req,res,next) => {
// })
update()
const server = http.Server(app);
server.listen(port, listenHost, function(){
console.log('App listening on %s:%d!', listenHost, port);
});