Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Node generator for Node-RED

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.
Expand All @@ -15,16 +14,17 @@ You may need to run this with `sudo`, or from within an Administrator command sh
## Usage

Usage:
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]

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]
Description:
Node generator for Node-RED

Supported source:
- OpenAPI document
- Function node (js file in library, "~/.node-red/lib/function/")
- Subflow node (json file of subflow)
- (Beta) Thing Description of W3C Web of Things (jsonld file or URL that points jsonld file)

Options:
-o : Destination path to save generated node (default: current directory)
--prefix : Prefix of npm module (default: "node-red-contrib-")
Expand All @@ -38,16 +38,16 @@ You may need to run this with `sudo`, or from within an Administrator command sh
--tgz : Save node as tgz file
--help : Show help
--wottd : explicitly instruct source file/URL points a Thing Description
--encoding : Encoding scheme of subflow (none or AES)
--encodekey : Encoding key of subflow
--lang : Language negotiation information when retrieve a Thing Description
-v : Show node generator version

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

- node-red-nodegen http://petstore.swagger.io/v2/swagger.json
- cd node-red-contrib-swagger-petstore
- sudo npm link
- cd ~/.node-red
- npm link node-red-contrib-swagger-petstore
- npm install *<path-to>*/node-red-contrib-swagger-petstore
- node-red

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

- 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).
- node-red-nodegen ~/.node-red/lib/functions/lower-case.js
- cd node-red-contrib-lower-case
- sudo npm link
- cd ~/.node-red
- npm link node-red-contrib-lower-case
- npm install *<path-to>*/node-red-contrib-lower-case
- node-red

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

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

- node-red-nodegen example.jsonld
- cd node-red-contrib-example-thing
- sudo npm link
- cd ~/.node-red
- npm link node-red-contrib-example-thing
- npm install *<path-to>*/node-red-contrib-example-thing
- node-red

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

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

- node-red-nodegen http://example.com/td.jsonld --wottd --lang "en-US,en;q=0.5"
- cd node-red-contrib-example-thing
- sudo npm link
- cd ~/.node-red
- npm link node-red-contrib-example-thing
- npm install *<path-to>*/node-red-contrib-example-thing
- node-red

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

### Example 5. Create an original node from SUBFLOW definition

- 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.
- Export JSON definition of the SUBFLOW template from Export menu. We assume it is saved to a file named qrcode.json.
- node-red-nodegen qrcode.json
- cd ~/.node-red
- npm install *<path-to>*/node-red-contrib-qrcode
- node-red

-> You can use qrcode node on Node-RED flow editor.

## Documentation

- [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))
- [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))
- [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))
- [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))
- [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))
- [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))

Note: Currently node generator supports GET and POST methods using JSON format without authentication.
24 changes: 21 additions & 3 deletions bin/node-red-nodegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ var nodegen = require('../lib/nodegen.js');
var options = {};
options.tgz = argv.tgz;
options.obfuscate = argv.obfuscate;
options.encoding = argv.encoding;
options.encodekey = argv.encodekey;

var data = {
prefix: argv.prefix || argv.p,
Expand Down Expand Up @@ -57,6 +59,8 @@ function help() {
' [--tgz]' +
' [--help]' +
' [--wottd]' +
' [--encoding <encoding>]' +
' [--encodekey <encoding key>]' +
' [--lang <accept-language>]' +
' [-v]\n' +
'\n' +
Expand All @@ -66,7 +70,7 @@ function help() {
'Supported source:'.bold + '\n' +
' - OpenAPI document\n' +
' - Function node (js file in library, "~/.node-red/lib/function/")\n' +
// ' - Subflow node (json file of subflow)\n' +
' - Subflow node (json file of subflow)\n' +
' - (Beta) Thing Description of W3C Web of Things (jsonld file or URL that points jsonld file)\n' +
'\n' +
'Options:\n'.bold +
Expand All @@ -82,6 +86,8 @@ function help() {
' --tgz : Save node as tgz file\n' +
' --help : Show help\n' +
' --wottd : explicitly instruct source file/URL points a Thing Description\n' +
' --encoding : Encoding scheme of subflow (none or AES)\n' +
' --encodekey : Encoding key of subflow\n' +
' --lang : Language negotiation information when retrieve a Thing Description\n' +
' -v : Show node generator version\n';
console.log(helpText);
Expand All @@ -102,6 +108,13 @@ function skipBom(body) {
}
}

function isSubflowDefinition(data) {
return data.find((item) => {
return ((item.type === "subflow") &&
(item.hasOwnProperty("meta")));
});
}

if (argv.help || argv.h) {
help();
} else if (argv.v) {
Expand All @@ -122,7 +135,12 @@ if (argv.help || argv.h) {
var content = JSON.parse(fs.readFileSync(sourcePath));
if (Array.isArray(content)) {
data.src = content;
promise = nodegen.FunctionNodeGenerator(data, options);
if (isSubflowDefinition(content)) {
promise = nodegen.SubflowNodeGenerator(data, options);
}
else {
promise = nodegen.FunctionNodeGenerator(data, options);
}
} else {
promise = nodegen.SwaggerNodeGenerator(data, options);
}
Expand All @@ -145,4 +163,4 @@ if (argv.help || argv.h) {
} else {
help();
}
}
}
Loading