Skip to content

Commit 1c495de

Browse files
Merge pull request #122 from node-red-hitachi/subflow-support
Add support for converting subflow definition
2 parents aba00fb + 52532dd commit 1c495de

File tree

13 files changed

+650
-114
lines changed

13 files changed

+650
-114
lines changed

README.md

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Node generator for Node-RED
32

43
Node generator is a command line tool to generate Node-RED nodes based on various sources such as an OpenAPI (Swagger) document, a Node-RED Function node, or a Web Of Things Thing description.
@@ -15,16 +14,17 @@ You may need to run this with `sudo`, or from within an Administrator command sh
1514
## Usage
1615

1716
Usage:
18-
node-red-nodegen <source file or URL> [-o <path to save>] [--prefix <prefix string>] [--name <node name>] [--module <module name>] [--version <version number>] [--keywords <keywords list>] [--category <node category>] [--icon <png or gif file>] [--color <node color>] [--tgz] [--help] [--wottd] [--lang <accept-language>] [-v]
19-
17+
node-red-nodegen <source file or URL> [-o <path to save>] [--prefix <prefix string>] [--name <node name>] [--module <module name>] [--version <version number>] [--keywords <keywords list>] [--category <node category>] [--icon <png or gif file>] [--color <node color>] [--tgz] [--help] [--wottd] [--encoding <encoding>] [--encodekey <encoding key>] [--lang <accept-language>] [-v]
18+
2019
Description:
2120
Node generator for Node-RED
22-
21+
2322
Supported source:
2423
- OpenAPI document
2524
- Function node (js file in library, "~/.node-red/lib/function/")
25+
- Subflow node (json file of subflow)
2626
- (Beta) Thing Description of W3C Web of Things (jsonld file or URL that points jsonld file)
27-
27+
2828
Options:
2929
-o : Destination path to save generated node (default: current directory)
3030
--prefix : Prefix of npm module (default: "node-red-contrib-")
@@ -38,16 +38,16 @@ You may need to run this with `sudo`, or from within an Administrator command sh
3838
--tgz : Save node as tgz file
3939
--help : Show help
4040
--wottd : explicitly instruct source file/URL points a Thing Description
41+
--encoding : Encoding scheme of subflow (none or AES)
42+
--encodekey : Encoding key of subflow
4143
--lang : Language negotiation information when retrieve a Thing Description
4244
-v : Show node generator version
4345

4446
### Example 1. Create an original node from OpenAPI document
4547

4648
- node-red-nodegen http://petstore.swagger.io/v2/swagger.json
47-
- cd node-red-contrib-swagger-petstore
48-
- sudo npm link
4949
- cd ~/.node-red
50-
- npm link node-red-contrib-swagger-petstore
50+
- npm install *<path-to>*/node-red-contrib-swagger-petstore
5151
- node-red
5252

5353
-> You can use swagger-petstore node on Node-RED flow editor.
@@ -56,41 +56,48 @@ You may need to run this with `sudo`, or from within an Administrator command sh
5656

5757
- In Node-RED flow editor, edit the function node and to the right of the 'name' option, click on the book icon and select 'Save to library'. Then fill in the 'Export as' with the file name (lower-case.js).
5858
- node-red-nodegen ~/.node-red/lib/functions/lower-case.js
59-
- cd node-red-contrib-lower-case
60-
- sudo npm link
6159
- cd ~/.node-red
62-
- npm link node-red-contrib-lower-case
60+
- npm install *<path-to>*/node-red-contrib-lower-case
6361
- node-red
6462

6563
-> You can use lower-case node on Node-RED flow editor.
6664

6765
### Example 3. Create original node from Thing Description
6866

6967
- node-red-nodegen example.jsonld
70-
- cd node-red-contrib-example-thing
71-
- sudo npm link
7268
- cd ~/.node-red
73-
- npm link node-red-contrib-example-thing
69+
- npm install *<path-to>*/node-red-contrib-example-thing
7470
- node-red
7571

7672
-> You can use Example Thing node on Node-RED flow editor.
7773

7874
### Example 4. Create original node from Thing Description via HTTP
7975

8076
- node-red-nodegen http://example.com/td.jsonld --wottd --lang "en-US,en;q=0.5"
81-
- cd node-red-contrib-example-thing
82-
- sudo npm link
8377
- cd ~/.node-red
84-
- npm link node-red-contrib-example-thing
78+
- npm install *<path-to>*/node-red-contrib-example-thing
8579
- node-red
8680

8781
-> You can use Example Thing node on Node-RED flow editor.
8882

83+
### Example 5. Create an original node from SUBFLOW definition
84+
85+
- In Node-RED flow editor, create the SUBFLOW template with module properties filled. Here, we assume module name for the SUBFLOW template is node-red-contrib-qrcode.
86+
- Export JSON definition of the SUBFLOW template from Export menu. We assume it is saved to a file named qrcode.json.
87+
- node-red-nodegen qrcode.json
88+
- cd ~/.node-red
89+
- npm install *<path-to>*/node-red-contrib-qrcode
90+
- node-red
91+
92+
-> You can use qrcode node on Node-RED flow editor.
93+
8994
## Documentation
95+
9096
- [Use cases](https://github.com/node-red/node-red-nodegen/blob/0.1.1/docs/index.md#use-cases) ([Japanese](https://github.com/node-red/node-red-nodegen/blob/0.1.1/docs/index_ja.md#use-cases))
9197
- [How to use Node generator](https://github.com/node-red/node-red-nodegen/blob/0.1.1/docs/index.md#how-to-use-node-generator) ([Japanese](https://github.com/node-red/node-red-nodegen/blob/0.1.1/docs/index_ja.md#how-to-use-node-generator))
9298
- [Generated files which node package contains](https://github.com/node-red/node-red-nodegen/blob/0.1.1/docs/index.md#generated-files-which-node-package-contains) ([Japanese](https://github.com/node-red/node-red-nodegen/blob/0.1.1/docs/index_ja.md#generated-files-which-node-package-contains))
9399
- [How to create a node from OpenAPI document](https://github.com/node-red/node-red-nodegen/blob/0.1.1/docs/index.md#how-to-create-a-node-from-openapi-document) ([Japanese](https://github.com/node-red/node-red-nodegen/blob/0.1.1/docs/index_ja.md#how-to-create-a-node-from-openapi-document))
94100
- [How to create a node from function node](https://github.com/node-red/node-red-nodegen/blob/0.1.1/docs/index.md#how-to-create-a-node-from-function-node) ([Japanese](https://github.com/node-red/node-red-nodegen/blob/0.1.1/docs/index_ja.md#how-to-create-a-node-from-function-node))
101+
- [How to create a node from subflow](https://github.com/node-red/node-red-nodegen/blob/0.1.1/docs/index.md#how-to-create-a-node-from-subflow) ([Japanese](https://github.com/node-red/node-red-nodegen/blob/0.1.1/docs/index_ja.md#how-to-create-a-node-from-subflow))
95102

96103
Note: Currently node generator supports GET and POST methods using JSON format without authentication.

bin/node-red-nodegen.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var nodegen = require('../lib/nodegen.js');
2727
var options = {};
2828
options.tgz = argv.tgz;
2929
options.obfuscate = argv.obfuscate;
30+
options.encoding = argv.encoding;
31+
options.encodekey = argv.encodekey;
3032

3133
var data = {
3234
prefix: argv.prefix || argv.p,
@@ -57,6 +59,8 @@ function help() {
5759
' [--tgz]' +
5860
' [--help]' +
5961
' [--wottd]' +
62+
' [--encoding <encoding>]' +
63+
' [--encodekey <encoding key>]' +
6064
' [--lang <accept-language>]' +
6165
' [-v]\n' +
6266
'\n' +
@@ -66,7 +70,7 @@ function help() {
6670
'Supported source:'.bold + '\n' +
6771
' - OpenAPI document\n' +
6872
' - Function node (js file in library, "~/.node-red/lib/function/")\n' +
69-
// ' - Subflow node (json file of subflow)\n' +
73+
' - Subflow node (json file of subflow)\n' +
7074
' - (Beta) Thing Description of W3C Web of Things (jsonld file or URL that points jsonld file)\n' +
7175
'\n' +
7276
'Options:\n'.bold +
@@ -82,6 +86,8 @@ function help() {
8286
' --tgz : Save node as tgz file\n' +
8387
' --help : Show help\n' +
8488
' --wottd : explicitly instruct source file/URL points a Thing Description\n' +
89+
' --encoding : Encoding scheme of subflow (none or AES)\n' +
90+
' --encodekey : Encoding key of subflow\n' +
8591
' --lang : Language negotiation information when retrieve a Thing Description\n' +
8692
' -v : Show node generator version\n';
8793
console.log(helpText);
@@ -102,6 +108,13 @@ function skipBom(body) {
102108
}
103109
}
104110

111+
function isSubflowDefinition(data) {
112+
return data.find((item) => {
113+
return ((item.type === "subflow") &&
114+
(item.hasOwnProperty("meta")));
115+
});
116+
}
117+
105118
if (argv.help || argv.h) {
106119
help();
107120
} else if (argv.v) {
@@ -122,7 +135,12 @@ if (argv.help || argv.h) {
122135
var content = JSON.parse(fs.readFileSync(sourcePath));
123136
if (Array.isArray(content)) {
124137
data.src = content;
125-
promise = nodegen.FunctionNodeGenerator(data, options);
138+
if (isSubflowDefinition(content)) {
139+
promise = nodegen.SubflowNodeGenerator(data, options);
140+
}
141+
else {
142+
promise = nodegen.FunctionNodeGenerator(data, options);
143+
}
126144
} else {
127145
promise = nodegen.SwaggerNodeGenerator(data, options);
128146
}
@@ -145,4 +163,4 @@ if (argv.help || argv.h) {
145163
} else {
146164
help();
147165
}
148-
}
166+
}

0 commit comments

Comments
 (0)