Skip to content

Commit b43ceab

Browse files
Merge pull request #107 from node-red/add-function-.json-support
Add function .json support
2 parents 6b5bcbf + e386514 commit b43ceab

File tree

8 files changed

+58
-20
lines changed

8 files changed

+58
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ coverage
1313

1414
# npm install log
1515
npm-debug.log.*
16+
package-lock.json

bin/node-red-nodegen.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,30 @@ if (argv.help || argv.h) {
145145
});
146146
} else if (sourcePath.endsWith('.json') && !argv.wottd) {
147147
data.src = JSON.parse(fs.readFileSync(sourcePath));
148-
nodegen.swagger2node(data, options).then(function (result) {
149-
console.log('Success: ' + result);
150-
}).catch(function (error) {
151-
console.log('Error: ' + error);
152-
});
148+
// if it's a .json flow file with one function node in...
149+
if (Array.isArray(data.src) && data.src[0].hasOwnProperty("type") && data.src[0].type == "function") {
150+
var f = data.src[0];
151+
if (!f.name || f.name.length ==0) { console.log('Error: No function name supplied.'); return; }
152+
data.name = f.name.toLowerCase();
153+
data.icon = f.icon;
154+
data.info = f.info;
155+
data.outputs = f.outputs;
156+
data.inputLabels = f.inputLabels;
157+
data.outputLabels = f.outputLabels;
158+
data.src = Buffer.from(f.func);
159+
nodegen.function2node(data, options).then(function (result) {
160+
console.log('Success: ' + result);
161+
}).catch(function (error) {
162+
console.log('Error: ' + error);
163+
});
164+
}
165+
else {
166+
nodegen.swagger2node(data, options).then(function (result) {
167+
console.log('Success: ' + result);
168+
}).catch(function (error) {
169+
console.log('Error: ' + error);
170+
});
171+
}
153172
} else if (sourcePath.endsWith('.yaml')) {
154173
data.src = yamljs.load(sourcePath);
155174
nodegen.swagger2node(data, options).then(function (result) {

lib/nodegen.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function createCommonFiles(templateDirectory, data) {
3939
}
4040
}
4141

42-
var isStockIcon = data.icon && data.icon.match(/^(alert|arduino|arrow-in|batch|bluetooth|bridge-dash|bridge|cog|comment|db|debug|envelope|feed|file-in|file-out|file|function|hash|inject|join|leveldb|light|link-out|mongodb|mouse|node-changed|node-error|parser-csv|parser-html|parser-json|parser-xml|parser-yaml|range|redis|rpi|serial|sort|split|subflow|swap|switch|template|timer|trigger|twitter|watch|white-globe)\.png$/);
42+
var isStockIcon = data.icon && (data.icon.match(/^(alert|arduino|arrow-in|batch|bluetooth|bridge-dash|bridge|cog|comment|db|debug|envelope|feed|file-in|file-out|file|function|hash|inject|join|leveldb|light|link-out|mongodb|mouse|node-changed|node-error|parser-csv|parser-html|parser-json|parser-xml|parser-yaml|range|redis|rpi|serial|sort|split|subflow|swap|switch|template|timer|trigger|twitter|watch|white-globe)\.png$/) || data.icon.match(/^(node-red|font-awesome)/));
4343
if (!isStockIcon) {
4444
try {
4545
fs.mkdirSync(path.join(data.dst, data.module, 'icons'));
@@ -143,6 +143,8 @@ function extractKeywords(keywordsStr) {
143143
}
144144

145145
function function2node(data, options) {
146+
// console.log("OPT",options);
147+
// console.log("DATA",data);
146148
"use strict";
147149
return when.promise(function (resolve, reject) {
148150
// Read meta data in js file
@@ -181,7 +183,7 @@ function function2node(data, options) {
181183
}
182184

183185
if (data.icon) {
184-
if (!data.icon.match(/\.(png|gif)$/)) {
186+
if (!data.icon.match(/\.(png|gif)$/) && !data.icon.match(/^(node-red|font-awesome)/)) {
185187
data.icon = data.icon + '.png';
186188
}
187189
if (!data.icon.match(/^[a-zA-Z0-9\-\./]+$/)) {
@@ -213,15 +215,22 @@ function function2node(data, options) {
213215
keywords: extractKeywords(data.keywords),
214216
category: data.category || 'function',
215217
icon: function () {
216-
if (data.icon) {
217-
return path.basename(data.icon);
218+
if (data.icon ) {
219+
if (!data.icon.match(/^(node-red|font-awesome)/)) {
220+
return path.basename(data.icon);
221+
}
222+
else { return data.icon; }
218223
} else {
219224
return 'icon.png';
220225
}
221226
},
222227
color: data.color || '#C0DEED',
223228
func: jsStringEscape(data.src),
224-
outputs: meta.outputs
229+
outputs: meta.outputs || data.outputs,
230+
inputLabels: JSON.stringify(data.inputLabels || []),
231+
outputLabels: JSON.stringify(data.outputLabels || []),
232+
nodeInfo: jsStringEscape(data.info || ""),
233+
nodeRead: data.info || ""
225234
};
226235

227236
createCommonFiles(__dirname + '/../templates/function', data);

templates/function/README.md.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ command in your Node-RED user directory, typically `~/.node-red`
1313

1414
npm install {{&projectName}}
1515

16+
## Information
17+
18+
{{&nodeRead}}

templates/function/node.html.mustache

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@
3838
label: function() {
3939
return this.name || '{{&nodeName}}';
4040
},
41+
labelStyle: function() {
42+
return this.name?"node_label_italic":"";
43+
},
44+
inputLabels: {{&inputLabels}},
45+
outputLabels: {{&outputLabels}},
4146
icon: '{{&icon}}',
42-
align: 'left'
47+
align: 'left',
48+
info: '{{nodeInfo}}'
4349
});
4450
</script>

templates/function/package.json.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
{{/keywords}}
1818
],
1919
"devDependencies": {
20-
"mocha": "6.2.0",
21-
"node-red": "1.0.3",
22-
"node-red-node-test-helper": "0.2.3"
20+
"mocha": "^6.2.0",
21+
"node-red": "^1.0.4",
22+
"node-red-node-test-helper": "^0.2.3"
2323
},
2424
"license": "Apache-2.0"
2525
}

templates/swagger/package.json.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
],
1919
"dependencies": {
2020
"q": "1.5.1",
21-
"request": "2.88.0",
21+
"request": "2.88.2",
2222
"file-type": "12.1.0"
2323
},
2424
"devDependencies": {
25-
"mocha": "6.2.0",
26-
"node-red": "1.0.3",
27-
"node-red-node-test-helper": "0.2.3"
25+
"mocha": "^6.2.0",
26+
"node-red": "^1.0.4",
27+
"node-red-node-test-helper": "^0.2.3"
2828
},
2929
"author": "{{&contactName}}",
3030
"license": "{{&licenseName}}"

templates/webofthings/package.json.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
],
1919
"dependencies": {
2020
"https-proxy-agent": "^3.0.1",
21-
"request": "^2.88.0",
21+
"request": "^2.88.2",
2222
"ws": "^7.2.0",
2323
"url-template": "^2.0.8",
2424
"ajv": "^6.10.2",
2525
"node-coap-client": "^1.0.2"
2626
},
2727
"devDependencies": {
28-
"node-red": "^1.0.3",
28+
"node-red": "^1.0.4",
2929
"node-red-node-test-helper": "^0.2.3"
3030
},
3131
"license": "{{&licenseName}}",

0 commit comments

Comments
 (0)