Skip to content

Commit 6ccb1ab

Browse files
committed
Refactor module to be requirable
``` require("node-red-admin")(["install", "node-red-node-random"]); ```
1 parent 88645ec commit 6ccb1ab

34 files changed

+445
-372
lines changed

Gruntfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2013, 2014 IBM Corp.
2+
* Copyright OpenJS Foundation and other contributors, https://openjsf.org/
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ module.exports = function(grunt) {
3333
jshint: {
3434
options: {
3535
// http://www.jshint.com/docs/options/
36+
esversion: 6,
3637
"curly": true, // require braces
3738
"eqnull": true, // ignore ==null
3839
"forin": true, // require property filtering in "for in" loops

lib/commands/disable.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015 IBM Corp.
2+
* Copyright OpenJS Foundation and other contributors, https://openjsf.org/
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,21 +15,21 @@
1515
**/
1616

1717
var request = require("../request");
18-
18+
1919
function command(argv,result) {
2020
var node = argv._[1];
2121
if (!node) {
2222
return result.help(command);
2323
}
2424
return request.request('/nodes/' + node, {
2525
method: "PUT",
26-
body: JSON.stringify({
26+
data: {
2727
enabled: false
28-
})
29-
}).then(result.logList).otherwise(result.warn);
28+
}
29+
}).then(result.logList).catch(result.warn);
3030
}
31-
command.name = "disable";
32-
command.usage = command.name+" {module|id}";
31+
command.alias = "disable";
32+
command.usage = command.alias+" {module|id}";
3333
command.description = "Disable the specified module or node set";
3434

3535

lib/commands/enable.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015 IBM Corp.
2+
* Copyright OpenJS Foundation and other contributors, https://openjsf.org/
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,21 +15,21 @@
1515
**/
1616

1717
var request = require("../request");
18-
18+
1919
function command(argv,result) {
2020
var node = argv._[1];
2121
if (!node) {
2222
return result.help(command);
2323
}
2424
return request.request('/nodes/' + node, {
2525
method: "PUT",
26-
body: JSON.stringify({
26+
data: {
2727
enabled: true
28-
})
29-
}).then(result.logList).otherwise(result.warn);
28+
}
29+
}).then(result.logList).catch(result.warn);
3030
}
31-
command.name = "enable";
32-
command.usage = command.name+" {module|id}";
31+
command.alias = "enable";
32+
command.usage = command.alias+" {module|id}";
3333
command.description = "Enable the specified module or node set";
3434

3535

lib/commands/hash.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015 IBM Corp.
2+
* Copyright OpenJS Foundation and other contributors, https://openjsf.org/
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,13 +14,12 @@
1414
* limitations under the License.
1515
**/
1616

17-
var when = require("when");
1817
var prompt = require("../prompt");
1918
try { bcrypt = require('bcrypt'); }
2019
catch(e) { bcrypt = require('bcryptjs'); }
2120

2221
function command(argv,result) {
23-
return when.promise(function(resolve) {
22+
return new Promise(resolve => {
2423
prompt.read({prompt:"Password:".bold,silent: true},function(err, password) {
2524
if (password) {
2625
result.log(bcrypt.hashSync(password, 8));
@@ -29,8 +28,8 @@ function command(argv,result) {
2928
});
3029
});
3130
}
32-
command.name = "hash-pwd";
33-
command.usage = command.name;
31+
command.alias = "hash-pwd";
32+
command.usage = command.alias;
3433
command.description = "Creates a password hash suitable for use with adminAuth or httpNodeAuth";
3534

3635
module.exports = command;

lib/commands/info.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015 IBM Corp.
2+
* Copyright OpenJS Foundation and other contributors, https://openjsf.org/
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,16 +15,16 @@
1515
**/
1616

1717
var request = require("../request");
18-
18+
1919
function command(argv,result) {
2020
var node = argv._[1];
2121
if (!node) {
2222
return result.help(command);
2323
}
24-
return request.request('/nodes/' + node, {}).then(result.logDetails).otherwise(result.warn);
24+
return request.request('/nodes/' + node, {}).then(result.logDetails).catch(result.warn);
2525
}
26-
command.name = "info";
27-
command.usage = command.name+" {module|node}";
26+
command.alias = "info";
27+
command.usage = command.alias+" {module|node}";
2828
command.description = "Display more information about the module or node";
2929

3030
module.exports = command;

lib/commands/install.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015 IBM Corp.
2+
* Copyright OpenJS Foundation and other contributors, https://openjsf.org/
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,21 +15,21 @@
1515
**/
1616

1717
var request = require("../request");
18-
18+
1919
function command(argv,result) {
2020
var module = argv._[1];
2121
if (!module) {
2222
return result.help(command);
2323
}
2424
return request.request('/nodes', {
2525
method: "POST",
26-
body: JSON.stringify({
26+
data: {
2727
module: module
28-
})
29-
}).then(result.logDetails).otherwise(result.warn);
28+
}
29+
}).then(result.logDetails).catch(result.warn);
3030
}
31-
command.name = "install";
32-
command.usage = command.name+" <module>";
31+
command.alias = "install";
32+
command.usage = command.alias+" <module>";
3333
command.description = "Install the module from NPM";
3434

3535

lib/commands/list.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015 IBM Corp.
2+
* Copyright OpenJS Foundation and other contributors, https://openjsf.org/
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,12 +15,12 @@
1515
**/
1616

1717
var request = require("../request");
18-
18+
1919
function command(argv,result) {
20-
return request.request('/nodes', {}).then(result.logNodeList).otherwise(result.warn);
20+
return request.request('/nodes', {}).then(result.logNodeList).catch(result.warn);
2121
}
22-
command.name = "list";
23-
command.usage = command.name;
22+
command.alias = "list";
23+
command.usage = command.alias;
2424
command.description = "List all of the installed nodes";
2525

2626

lib/commands/login.js

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015 IBM Corp.
2+
* Copyright OpenJS Foundation and other contributors, https://openjsf.org/
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,52 +18,41 @@ var request = require("../request");
1818
var config = require("../config");
1919
var prompt = require("../prompt");
2020

21-
var when = require("when");
22-
2321
function command(argv,result) {
24-
return when.promise(function(resolve) {
25-
config.tokens(null);
26-
27-
request.request('/auth/login',{}).then(function(resp) {
28-
if (resp.type) {
29-
if (resp.type == "credentials") {
30-
prompt.read({prompt:"Username:".bold},function(err, username) {
31-
prompt.read({prompt:"Password:".bold,silent: true},function(err, password) {
32-
request.request('/auth/token', {
33-
method: "POST",
34-
body: JSON.stringify({
35-
client_id: 'node-red-admin',
36-
grant_type: 'password',
37-
scope: '*',
38-
username: username,
39-
password: password
40-
})
41-
}).then(function(resp) {
42-
config.tokens(resp);
43-
result.log("Logged in".green);
44-
resolve();
45-
}).otherwise(function(resp) {
46-
result.warn("Login failed");
47-
resolve();
48-
});
22+
config.tokens(null);
23+
return request.request('/auth/login',{}).then(function(resp) {
24+
if (resp.type) {
25+
if (resp.type == "credentials") {
26+
prompt.read({prompt:"Username:".bold},function(err, username) {
27+
prompt.read({prompt:"Password:".bold,silent: true},function(err, password) {
28+
request.request('/auth/token', {
29+
method: "POST",
30+
data: {
31+
client_id: 'node-red-admin',
32+
grant_type: 'password',
33+
scope: '*',
34+
username: username,
35+
password: password
36+
}
37+
}).then(function(resp) {
38+
config.tokens(resp);
39+
result.log("Logged in".green);
40+
}).catch(function(resp) {
41+
result.warn("Login failed");
4942
});
5043
});
51-
} else {
52-
result.warn("Unsupported login type");
53-
resolve();
54-
}
44+
});
5545
} else {
56-
resolve();
46+
result.warn("Unsupported login type");
5747
}
58-
}).otherwise(function(resp) {
59-
result.warn("Login failed");
60-
resolve();
61-
});
48+
}
49+
}).catch(function(resp) {
50+
result.warn("Login failed");
6251
});
6352
}
6453

65-
command.name = "login";
66-
command.usage = command.name+"";
54+
command.alias = "login";
55+
command.usage = command.alias+"";
6756
command.description = "Log user in to the targetted Node-RED admin api";
6857

6958
module.exports = command;

lib/commands/projects.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright OpenJS Foundation and other contributors, https://openjsf.org/
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
17+
var request = require("../request");
18+
19+
function command(argv,result) {
20+
return request.request('/projects', {}).then(result.logProjectList).catch(result.warn);
21+
}
22+
command.alias = "projects";
23+
command.usage = command.alias;
24+
command.description = "List available projects";
25+
26+
module.exports = command;

lib/commands/remove.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015 IBM Corp.
2+
* Copyright OpenJS Foundation and other contributors, https://openjsf.org/
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,10 +25,10 @@ function command(argv,result) {
2525
method: "DELETE"
2626
}).then(function() {
2727
result.log("Uninstalled " + module);
28-
}).otherwise(result.warn);
28+
}).catch(result.warn);
2929
}
30-
command.name = "remove";
31-
command.usage = command.name+" <module>";
30+
command.alias = "remove";
31+
command.usage = command.alias+" <module>";
3232
command.description = "Remove the NPM module";
3333

3434

0 commit comments

Comments
 (0)