Skip to content

Commit 051251c

Browse files
committed
Update search to use flow library api
1 parent ece2c05 commit 051251c

File tree

2 files changed

+48
-54
lines changed

2 files changed

+48
-54
lines changed

lib/commands/search.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,43 @@
1515
**/
1616

1717
var httpRequest = require("request");
18+
1819
var when = require("when");
19-
var defaultUrl = 'https://registry.npmjs.org/-/v1/search?text=keywords:node-red';
2020

2121
function command(argv,result) {
2222
var module = argv._[1];
23-
2423
if (!module) {
2524
return result.help(command);
2625
}
27-
26+
2827
var options = {
2928
method: "GET",
30-
url: defaultUrl,
29+
url: 'https://flows.nodered.org/things?format=json&per_page=50&type=node&term='+module,
3130
headers: {
3231
'Accept': 'application/json',
3332
}
3433
};
35-
3634
return when.promise(function(resolve) {
37-
options.url = defaultUrl + '+' + module;
3835
httpRequest.get(options, function(error, response, body) {
3936
if (!error && response.statusCode == 200) {
40-
var objectsInfo = (JSON.parse(body));
41-
var filter = new RegExp(module);
42-
var found = false;
43-
result.log('total: ' + objectsInfo.total + ' objects: ' + objectsInfo.objects.length + ' found');
44-
var objectsEntry = {};
45-
for (var i = 0; i < objectsInfo.objects.length; i++) {
46-
objectsEntry = objectsInfo.objects[i];
47-
if(objectsEntry && objectsEntry.package) {
48-
result.log(objectsEntry.package.name);
49-
if (!filter ||
50-
filter.test(objectsEntry.package.name) ||
51-
filter.test(objectsEntry.package.description) ||
52-
filter.test(objectsEntry.package.keywords)) {
53-
54-
result.log((objectsEntry.package.name).cyan.bold + (" - " + objectsEntry.package.keywords).grey);
55-
found = true;
56-
}
37+
var info = JSON.parse(body);
38+
var matches = [];
39+
if (info.data && info.data.length > 0) {
40+
for (var i = 0; i < info.data.length; i++) {
41+
var n = info.data[i];
42+
var label = info.data[i].name.cyan.bold + (" - " + info.data[i].description).grey;
43+
var index = label.indexOf(module);
44+
matches.push({
45+
label: label,
46+
index: index===-1?1000:index
47+
});
5748
}
58-
}
59-
if (!found) {
49+
matches.sort(function(A,B) { return A.index - B.index; });
50+
matches.forEach(function(m) {
51+
result.log(m.label);
52+
});
53+
54+
} else {
6055
result.log("No results found");
6156
}
6257
} else if (error) {

test/lib/commands/search_spec.js

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,66 +30,65 @@ describe("commands/install", function() {
3030
}
3131
result.reset();
3232
});
33-
33+
3434
it('reports no results when none match',function(done) {
35-
sinon.stub(httpRequest,"get").yields(null,{statusCode:200},JSON.stringify({"objects":[],"total":0,"time":"Thu Feb 27 2020 11:27:22 GMT+0000 (UTC)"}));
36-
35+
sinon.stub(httpRequest,"get").yields(null,{statusCode:200},JSON.stringify({"data":[]} ));
36+
3737
command({_:[null,"testnode"]},result).then(function() {
38-
result.log.called.should.be.true;
39-
result.log.args[0][0].should.eql("total: 0 objects: 0 found");
40-
result.log.args[1][0].should.eql("No results found");
38+
result.log.called.should.be.true();
39+
result.log.args[0][0].should.eql("No results found");
4140
done();
4241
}).otherwise(done);
43-
42+
4443
});
45-
it('lists matched modules',function(done) {
44+
it('lists results ordered by relevance',function(done) {
4645
sinon.stub(httpRequest,"get").yields(null,{statusCode:200},
4746
JSON.stringify({
48-
"objects":[
49-
{ "package":{"name": "testnode", "description": "a random node", "keywords":["testnode", "node-red", "test"]} },
50-
{ "package":{"name": "testnodes", "description": "a random nodes test", "keywords":["testnodes", "node-red", "tests"]} }
51-
],
52-
"total":2,
53-
"time":"Thu Feb 27 2020 11:27:22 GMT+0000 (UTC)"
47+
"data":[
48+
{ "name":"another-node", "description":"a testnode - THREE" },
49+
{ "name":"testnode", "description":"a test node - ONE" },
50+
{ "name":"@scoped/testnode", "description":"once more - TWO" }
51+
]
5452
})
5553
);
56-
54+
5755
command({_:[null,"testnode"]},result).then(function() {
58-
result.log.calledTwice.should.be.true;
59-
/testnode/.test(result.log.args[0][0]).should.be.true;
60-
/testnode/.test(result.log.args[1][0]).should.be.true;
56+
result.log.args.length.should.equal(3);
57+
/ONE/.test(result.log.args[0][0]).should.be.true();
58+
/TWO/.test(result.log.args[1][0]).should.be.true();
59+
/THREE/.test(result.log.args[2][0]).should.be.true();
6160
done();
6261
}).otherwise(done);
63-
62+
6463
});
65-
64+
6665
it('reports error response',function(done) {
6766
sinon.stub(httpRequest,"get").yields("testError",{statusCode:200},JSON.stringify({rows:[]}));
68-
67+
6968
command({_:[null,"testnode"]},result).then(function() {
7069
result.log.called.should.be.false;
71-
result.warn.called.should.be.true;
70+
result.warn.called.should.be.true();
7271
result.warn.args[0][0].should.eql("testError");
7372
done();
7473
}).otherwise(done);
75-
74+
7675
});
77-
76+
7877
it('reports unexpected http response',function(done) {
7978
sinon.stub(httpRequest,"get").yields(null,{statusCode:101},"testError");
80-
79+
8180
command({_:[null,"testnode"]},result).then(function() {
8281
result.log.called.should.be.false;
83-
result.warn.called.should.be.true;
82+
result.warn.called.should.be.true();
8483
result.warn.args[0][0].should.eql("101: testError");
8584
done();
8685
}).otherwise(done);
8786
});
88-
87+
8988
it('displays command help if node not specified', function(done) {
9089
command({_:{}},result);
91-
result.help.called.should.be.true;
90+
result.help.called.should.be.true();
9291
done();
9392
});
94-
93+
9594
});

0 commit comments

Comments
 (0)