4 * Copyright 2014 IBM Corp.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 var util = require("util");
20 var request = require("request");
21 var colors = require('colors');
22 var apiRequest = require("./lib/request");
23 var config = require("./lib/config");
26 "target": function() {
27 var target = process.argv[3];
29 if (!/^https?:\/\/.+/.test(target)) {
30 console.warn("Invalid target url");
33 if (target.slice(-1) == "/") {
34 target = target.slice(0,target.length-1);
36 var oldTarget = config.target;
37 config.target = target;
39 console.log("Target: ".yellow+config.target);
44 apiRequest('/nodes',{}).then(logNodeList).otherwise(logFailure);
47 apiRequest('/nodes/'+process.argv[3],{}).then(logNodeList).otherwise(logFailure);
49 "enable-node": function() {
50 apiRequest('/nodes/'+process.argv[3],{
52 body: JSON.stringify({enabled:true})
53 }).then(logNodeList).otherwise(logFailure);
55 "disable-node": function() {
56 apiRequest('/nodes/'+process.argv[3],{
58 body: JSON.stringify({enabled:false})
59 }).then(logNodeList).otherwise(logFailure);
61 "install": function() {
64 body: JSON.stringify({module:process.argv[3]})
65 }).then(logNodeList).otherwise(logFailure);
67 "remove": function() {
68 apiRequest('/nodes/'+process.argv[3],{
70 }).then(logNodeList).otherwise(logFailure);
72 "search": function() {
75 url: 'https://registry.npmjs.org/-/_view/byKeyword?startkey=["node-red"]&endkey=["node-red",{}]&group_level=3' ,
77 'Accept': 'application/json',
80 request(options, function (error, response, body) {
81 if (!error && response.statusCode == 200) {
82 var info = (JSON.parse(body)).rows;
84 if (process.argv[3]) {
85 filter = new RegExp(process.argv[3]);
87 for (var i=0;i<info.length;i++) {
89 if (!filter || filter.test(n.key[1]) || filter.test(n.key[2])) {
90 console.log(n.key[1] + (" - "+ n.key[2]).grey);
94 console.log(error.toString().red);
96 console.log((response.statusCode+": "+body).red);
102 function logNodeList(nodes) {
103 if (!util.isArray(nodes)) {
106 for (var i=0;i<nodes.length;i++) {
108 console.log(formatNodeInfo(n))
112 function logFailure(msg) {
113 console.log(msg.red);
116 function formatBoolean(v,c) {
124 function formatNodeInfo(n) {
125 var inError = n.hasOwnProperty("err");
127 var str = formatBoolean(n.enabled,"X")+formatBoolean(n.loaded,"L")+" ";
129 if (n.enabled && n.loaded) {
131 } else if (n.enabled && n.err) {
137 str += " ["+n.module+"]";
139 str += " "+n.types.join(", ");
146 if (commands[process.argv[2]]) {
147 commands[process.argv[2]].call();