Skip to content

Commit

Permalink
Pr issue6 (#11)
Browse files Browse the repository at this point in the history
* fix: issue #6 search

* chore: release testing and settings done

* chore: fix missed grunt checks

* chore: changes to focus on issue #6
  • Loading branch information
biancode authored May 18, 2020
1 parent 93554fd commit ece2c05
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 38 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ matrix:
include:
- node_js: 8
- node_js: 10
- node_js: 12
before_install:
- npm i -g npm
script:
Expand Down
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Node-RED Command Line Tool
# Node-RED Admin

The Node-RED admin command line interface.

[![Build Status](https://travis-ci.org/node-red/node-red-admin.svg?branch=master)](https://travis-ci.org/node-red/node-red-admin) [![Coverage Status](https://coveralls.io/repos/node-red/node-red-admin/badge.svg?branch=master)](https://coveralls.io/r/node-red/node-red-admin?branch=master)


Install this globally to make the `node-red-admin` command available on
your path:
A command line tool for Node-RED administrations.

Install this globally to make the `node-red-admin` command available on your path:

npm install -g node-red-admin

Expand All @@ -22,13 +25,14 @@ You may also need to add `--unsafe-perm` to the command if you hit permissions e
Node-RED command-line client

Commands:
target - Set or view the target URL
login - Log user in to the targetted Node-RED admin api
list - List all of the installed nodes
info - Display more information about the module or node
enable - Enable the specified module or node set
target - Set or view the target URL and port like https://localhost:1880
login - Log user in to the target of the Node-RED admin API
list - List all of the installed nodes
info - Display more information about the module or node
enable - Enable the specified module or node set
disable - Disable the specified module or node set
search - Search NPM for Node-RED modules relating to the search-term given
search - Search NPM for Node-RED modules by matching name, description or keywords with the term
install - Install the module from NPM
remove - Remove the NPM module
remove - Remove the NPM module
hash-pw - creates a hash to use for Node-RED settings like "adminAuth"

28 changes: 20 additions & 8 deletions lib/commands/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,45 @@
**/

var httpRequest = require("request");

var when = require("when");
var defaultUrl = 'https://registry.npmjs.org/-/v1/search?text=keywords:node-red';

function command(argv,result) {
var module = argv._[1];

if (!module) {
return result.help(command);
}

var options = {
method: "GET",
url: 'https://registry.npmjs.org/-/_view/byKeyword?startkey=["node-red"]&endkey=["node-red",{}]&group_level=3',
url: defaultUrl,
headers: {
'Accept': 'application/json',
}
};

return when.promise(function(resolve) {
options.url = defaultUrl + '+' + module;
httpRequest.get(options, function(error, response, body) {
if (!error && response.statusCode == 200) {
var info = (JSON.parse(body)).rows;
var objectsInfo = (JSON.parse(body));
var filter = new RegExp(module);
var found = false;
for (var i = 0; i < info.length; i++) {
var n = info[i];
if (!filter || filter.test(n.key[1]) || filter.test(n.key[2])) {
result.log(n.key[1].cyan.bold + (" - " + n.key[2]).grey);
found = true;
result.log('total: ' + objectsInfo.total + ' objects: ' + objectsInfo.objects.length + ' found');
var objectsEntry = {};
for (var i = 0; i < objectsInfo.objects.length; i++) {
objectsEntry = objectsInfo.objects[i];
if(objectsEntry && objectsEntry.package) {
result.log(objectsEntry.package.name);
if (!filter ||
filter.test(objectsEntry.package.name) ||
filter.test(objectsEntry.package.description) ||
filter.test(objectsEntry.package.keywords)) {

result.log((objectsEntry.package.name).cyan.bold + (" - " + objectsEntry.package.keywords).grey);
found = true;
}
}
}
if (!found) {
Expand Down
20 changes: 10 additions & 10 deletions node-red-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ function help() {
"Description:".bold + "\n" +
" Node-RED command-line client\n\n" +
"Commands:\n".bold +
" target\n" +
" login\n" +
" list\n" +
" info\n" +
" enable\n" +
" disable\n" +
" search\n" +
" install\n" +
" remove\n" +
" hash-pw\n"
" target (Set or view the target URL and port like https://localhost:1880)\n" +
" login (Log user in to the target of the Node-RED admin API)\n" +
" list (List all of the installed nodes)\n" +
" info (Display more information about the module or node)\n" +
" enable (Enable the specified module or node set)\n" +
" disable (Disable the specified module or node set)\n" +
" search (Search NPM for Node-RED modules by matching name, description or keywords with the term)\n" +
" install (Install the module from NPM to Node-RED)\n" +
" remove (Remove the NPM module from Node-RED)\n" +
" hash-pw (creates a hash to use for Node-RED settings like \"adminAuth\")\n"
;
console.log(helpText);
}
Expand Down
31 changes: 29 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "node-red-admin",
"version": "0.1.6",
"version": "0.1.7",
"description": "The Node-RED admin command line interface",
"homepage": "https://nodered.org",
"bugs": {
"url": "https://github.com/node-red/node-red-admin/issues/"
},
"license": "Apache-2.0",
"repository": {
"type": "git",
Expand All @@ -20,6 +23,9 @@
}
],
"preferGlobal": "true",
"scripts": {
"test": "./node_modules/.bin/mocha "
},
"dependencies": {
"bcryptjs": "^2.4.3",
"cli-table": "^0.3.1",
Expand All @@ -34,7 +40,7 @@
"grunt-cli": "^1.3.2",
"grunt-contrib-jshint": "^2.1.0",
"grunt-simple-mocha": "^0.4.1",
"mocha": "^6.1.4",
"mocha": "^7.1.0",
"should": "^13.2.3",
"sinon": "^1.17.7"
},
Expand All @@ -43,5 +49,26 @@
},
"bin": {
"node-red-admin": "node-red-admin.js"
},
"mocha": {
"spec:": "test/**/*.spec.js",
"diff": true,
"extension": [
"js"
],
"opts": false,
"package": "./package.json",
"reporter": "spec",
"slow": 75,
"timeout": 2000,
"ui": "bdd",
"recursive": "true",
"watch-files": [
"lib/**/*.js",
"test/**/*.js"
],
"watch-ignore": [
"lib/vendor"
]
}
}
19 changes: 11 additions & 8 deletions test/lib/commands/search_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,27 @@ describe("commands/install", function() {
result.reset();
});


it('reports no results when none match',function(done) {
sinon.stub(httpRequest,"get").yields(null,{statusCode:200},JSON.stringify({rows:[]}));
sinon.stub(httpRequest,"get").yields(null,{statusCode:200},JSON.stringify({"objects":[],"total":0,"time":"Thu Feb 27 2020 11:27:22 GMT+0000 (UTC)"}));

command({_:[null,"testnode"]},result).then(function() {
result.log.called.should.be.true;
result.log.args[0][0].should.eql("No results found");
result.log.args[0][0].should.eql("total: 0 objects: 0 found");
result.log.args[1][0].should.eql("No results found");
done();
}).otherwise(done);

});
it('lists matched modules',function(done) {
sinon.stub(httpRequest,"get").yields(null,{statusCode:200},
JSON.stringify({rows:[
{key:["node-red","testnode","a random node"]},
{key:["node-red","another","a testnode match"]},
{key:["node-red","not a match","not a match"]}
]})
JSON.stringify({
"objects":[
{ "package":{"name": "testnode", "description": "a random node", "keywords":["testnode", "node-red", "test"]} },
{ "package":{"name": "testnodes", "description": "a random nodes test", "keywords":["testnodes", "node-red", "tests"]} }
],
"total":2,
"time":"Thu Feb 27 2020 11:27:22 GMT+0000 (UTC)"
})
);

command({_:[null,"testnode"]},result).then(function() {
Expand Down

0 comments on commit ece2c05

Please sign in to comment.