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
15 changes: 15 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
To help us understand the issue, please fill-in as much of the following information as you can:

### What are the steps to reproduce?

### What happens?

### What do you expect to happen?

### Please tell us about your environment:

- [ ] Node-RED version:
- [ ] node.js version:
- [ ] npm version:
- [ ] Platform/OS:
- [ ] Browser:
12 changes: 12 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Types of changes

What types of changes does your code introduce?
_Put an `x` in the boxes that apply_

- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)

## Proposed changes

Describe the nature of this change. What problem does it address?

6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Generated nodes
node-red-contrib-*
node-red-node-*

# Dependency directory
node_modules
54 changes: 53 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
# node-red-nodegen
# Node generator for Node-RED

Install node generator globally to make the `node-red-nodegen` command available on your path:

npm install -g git+http://github.com/node-red/node-red-nodegen.git

You may need to run this with `sudo`, or from within an Administrator command shell.

## 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> [--tgz] [--help]

Description:
Node generator for Node-RED

Supported source:
- Function node (js file in library, "~/.node-red/lib/function/")
- Swagger definition

Options:
-o : Destination path to save generated node (default: current directory)
--prefix : Prefix of npm module (default: "node-red-contrib-")
--name : Node name (default: name defined in source)
--module : Module name (default: "node-red-contrib-<node name>")
--version : Node version (format: "number.number.number" like "4.5.1")
--tgz : Save node as tgz file
--help : Show help

### Example 1. Create original node from function node (JavaScript code)

- On Node-RED flow editor, save function node to library with file name (lower-case.js).
- node-red-nodegen ~/.node-red/lib/function/lower-case.js
- cd node-red-contrib-lower-case
- sudo npm link
- cd ~/.node-red
- npm link node-red-contrib-lower-case
- node-red

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

### Example 2. Create original node from Swagger definition

- 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
- node-red

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

Note: Currently node generator supports GET and POST methods using JSON format without authentication.
107 changes: 107 additions & 0 deletions bin/node-red-nodegen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env node

/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

var fs = require('fs');
var request = require('request');
var yamljs = require('yamljs');
var argv = require('minimist')(process.argv.slice(2));
var colors = require('colors');
var nodegen = require('../lib/nodegen.js');

// Command options
var options = {};
options.tgz = argv.tgz;
options.obfuscate = argv.obfuscate;

var data = {
prefix: argv.prefix || argv.p,
name: argv.name || argv.n,
module: argv.module,
version: argv.version || argv.v,
dst: argv.output || argv.o || '.'
};

function help() {
var helpText = 'Usage:'.bold + '\n' +
' node-red-nodegen <source file or URL>' +
' [-o <path to save>]' +
' [--prefix <prefix string>]' +
' [--name <node name>]' +
' [--module <module name>]' +
' [--version <version number>' +
//' [--icon <png or gif file>' +
//' [--color <node color>' +
' [--tgz]' +
' [--help]\n' +
'\n' +
'Description:'.bold + '\n' +
' Node generator for Node-RED\n' +
'\n' +
'Supported source:'.bold + '\n' +
' - Function node (js file in library, "~/.node-red/lib/function/")\n' +
// ' - Subflow node (json file of subflow)\n' +
' - Swagger definition\n' +
'\n' +
'Options:\n'.bold +
' -o : Destination path to save generated node (default: current directory)\n' +
' --prefix : Prefix of npm module (default: "node-red-contrib-")\n' +
' --name : Node name (default: name defined in source)\n' +
' --module : Module name (default: "node-red-contrib-<node name>")\n' +
' --version : Node version (format: "number.number.number" like "4.5.1")\n' +
//' --icon : png or gif file for node appearance (image size should be 10x20)\n';
//' --color : color for node appearance (format: color hexadecimal numbers like "#A6BBCF")\n';
' --tgz : Save node as tgz file\n' +
' --help : Show help\n';
console.log(helpText);
}

if (!argv.h && !argv.help) {
var sourcePath = argv._[0];
if (sourcePath) {
if (sourcePath.startsWith('http://') || sourcePath.startsWith('https://')) {
request(sourcePath, function (error, response, body) {
if (!error) {
data.src = JSON.parse(body);
var filename = nodegen.swagger2node(data, options);
console.log('Success: ' + filename);
} else {
console.error(error);
}
});
} else if (sourcePath.endsWith('.json')) {
data.src = JSON.parse(fs.readFileSync(sourcePath));
var filename = nodegen.swagger2node(data, options);
console.log('Success: ' + filename);
} else if (sourcePath.endsWith('.yaml')) {
data.src = yamljs.load(sourcePath);
var filename = nodegen.swagger2node(data, options);
console.log('Success: ' + filename);
} else if (sourcePath.endsWith('.js')) {
data.src = fs.readFileSync(sourcePath);
var filename = nodegen.function2node(data, options);
console.log('Success: ' + filename);
} else {
console.error('error: Unsupported file type');
}
} else {
help();
}
} else {
help();
}

Loading