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
44 changes: 33 additions & 11 deletions lib/nodegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function runNpmPack(data) {

function extractKeywords(keywordsStr) {
var keywords = ["node-red-nodegen"];
keywords = keywordsStr ? keywords.concat(csv.parse(keywordsStr)[0]) : keywords;
keywords = keywordsStr ? keywords.concat(csv.parse(keywordsStr)[0]) : keywords;
keywords = keywords.map(k => ({ name: k }));
keywords[keywords.length - 1].last = true;
return keywords;
Expand Down Expand Up @@ -200,7 +200,7 @@ function swagger2node(data, options) {
} else if (key === 'default') { // 3. Handle swagger-js-codegen bug
return undefined;
} else if (key === 'operationId') { // 4. Sanitize 'operationId' (remove special chars)
return value.replace(/(?!\w|\s)./g, '');
return value.replace(/(?!\w|\s)./g, '');
} else {
return value;
}
Expand Down Expand Up @@ -257,7 +257,7 @@ function swagger2node(data, options) {
nodejsSourceCode = obfuscator.obfuscate(nodejsSourceCode, { stringArrayEncoding: 'rc4' });
}
fs.writeFileSync(data.dst + '/' + data.module + '/lib.js', nodejsSourceCode);

// Create package.json
var packageSourceCode = CodeGen.getCustomCode({
className: className,
Expand Down Expand Up @@ -286,6 +286,24 @@ function swagger2node(data, options) {
});
fs.writeFileSync(data.dst + '/' + data.module + '/package.json', packageSourceCode);

// Mustache helpers
var isNotBodyParam = function () {
return function (content, render) {
return render('{{camelCaseName}}') !== 'body' ? render(content) : '';
}
};
var isBodyParam = function () {
return function (content, render) {
return render('{{camelCaseName}}') === 'body' ? render(content) : '';
}
};
var hasOptionalParams = function () {
return function (content, render) {
var params = render('{{#parameters}}{{^required}}{{camelCaseName}},{{/required}}{{/parameters}}');
return params.split(',').filter(p => p).some(p => p !== 'body') ? render(content) : '';
}
};

// Create node.js
var nodeSourceCode = CodeGen.getCustomCode({
className: className,
Expand All @@ -297,6 +315,8 @@ function swagger2node(data, options) {
},
mustache: {
nodeName: data.name,
isBodyParam: isBodyParam,
isNotBodyParam: isNotBodyParam
},
lint: false,
beautify: false
Expand All @@ -317,7 +337,9 @@ function swagger2node(data, options) {
},
mustache: {
nodeName: data.name,
category: data.category || 'function'
category: data.category || 'function',
isNotBodyParam: isNotBodyParam,
hasOptionalParams: hasOptionalParams
},
lint: false,
beautify: false
Expand Down Expand Up @@ -426,7 +448,7 @@ function widget2node(data, options) {
}
}
else {
new_src = new_src +part +"\n";
new_src = new_src + part + "\n";
}
});

Expand Down Expand Up @@ -468,33 +490,33 @@ function widget2node(data, options) {
params[name] = conf.vars[name];
}
}

var basedir = __dirname + '/../templates/widget';

createCommonFiles(basedir, data);

// Create package.json
var packageTemplate = fs.readFileSync(basedir+'/package.json.mustache', 'utf-8');
var packageTemplate = fs.readFileSync(basedir + '/package.json.mustache', 'utf-8');
var packageSourceCode = mustache.render(packageTemplate, params);
fs.writeFileSync(data.dst + '/' + data.module + '/package.json', packageSourceCode);

// Create node.js
var nodeTemplate = fs.readFileSync(basedir+'/node.js.mustache', 'utf-8');
var nodeTemplate = fs.readFileSync(basedir + '/node.js.mustache', 'utf-8');
var nodeSourceCode = mustache.render(nodeTemplate, params);
fs.writeFileSync(data.dst + '/' + data.module + '/node.js', nodeSourceCode);

// Create node.html
var htmlTemplate = fs.readFileSync(basedir+'/node.html.mustache', 'utf-8');
var htmlTemplate = fs.readFileSync(basedir + '/node.html.mustache', 'utf-8');
var htmlSourceCode = mustache.render(htmlTemplate, params);
fs.writeFileSync(data.dst + '/' + data.module + '/node.html', htmlSourceCode);

// Create README.md
var readmeTemplate = fs.readFileSync(basedir+'/README.md.mustache', 'utf-8');
var readmeTemplate = fs.readFileSync(basedir + '/README.md.mustache', 'utf-8');
var readmeSourceCode = mustache.render(readmeTemplate, params);
fs.writeFileSync(data.dst + '/' + data.module + '/README.md', readmeSourceCode);

// Create LICENSE file
var licenseTemplate = fs.readFileSync(basedir+'/LICENSE.mustache', 'utf-8');
var licenseTemplate = fs.readFileSync(basedir + '/LICENSE.mustache', 'utf-8');
var licenseSourceCode = mustache.render(licenseTemplate, params);
fs.writeFileSync(data.dst + '/' + data.module + '/LICENSE', licenseSourceCode);

Expand Down
2 changes: 1 addition & 1 deletion templates/swagger/locales/en-US/node.json.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"{{&camelCaseName}}": "{{&camelCaseName}}",
{{/parameters}}
{{/methods}}
"optionalParameters": "Option"
"optionalParameters": "Options"
}
}
}
49 changes: 25 additions & 24 deletions templates/swagger/node.html.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
{{#methods}}
{{#parameters}}
{{&methodName}}_{{&camelCaseName}}: { value: "" },
{{&methodName}}_{{&camelCaseName}}Type: {value: "str"},
{{/parameters}}
{{/methods}}

name: { value: "" }
},
inputs:1,
outputs:1,
inputs: 1,
outputs: 1,
icon: "icon.png",
label: function() {
return this.name || "{{&nodeName}}";
Expand All @@ -31,32 +32,34 @@
var showParameters = function() {
{{#methods}}
{{#parameters}}
$("#node-input-{{&methodName}}_{{&camelCaseName}}").typedInput({
default: 'str',
typeField: $("#node-input-{{&methodName}}_{{&camelCaseName}}Type"),
types: ['str', 'msg']
});
$("#{{&methodName}}_{{&camelCaseName}}").hide();

{{/parameters}}
{{/methods}}

$("#optional-parameters").hide();
$("#optional-parameters-label").hide();
{{#methods}}
if ($("#node-input-method").val() === '{{&methodName}}') {
{{#parameters}}
{{^required}}
if ('{{&camelCaseName}}' !== 'body') {
$("#optional-parameters").show();
$("#optional-parameters-label").show();
}
{{/required}}
{{/parameters}}
{{#hasOptionalParams}}
$("#optional-parameters").show();
$("#optional-parameters-label").show();
{{/hasOptionalParams}}
}
{{/methods}}

if ($("#optional-parameters").prop('checked')) {
{{#methods}}
if ($("#node-input-method").val() === '{{&methodName}}') {
{{#parameters}}
if ('{{&camelCaseName}}' !== 'body') {
$("#{{&methodName}}_{{&camelCaseName}}").show();
}
{{#isNotBodyParam}}
$("#{{&methodName}}_{{&camelCaseName}}").show();
{{/isNotBodyParam}}
{{/parameters}}
}
{{/methods}}
Expand All @@ -65,9 +68,9 @@
if ($("#node-input-method").val() === '{{&methodName}}') {
{{#parameters}}
{{#required}}
if ('{{&camelCaseName}}' !== 'body') {
$("#{{&methodName}}_{{&camelCaseName}}").show();
}
{{#isNotBodyParam}}
$("#{{&methodName}}_{{&camelCaseName}}").show();
{{/isNotBodyParam}}
{{/required}}
{{/parameters}}
}
Expand Down Expand Up @@ -115,25 +118,22 @@
{{/methods}}
</select>
&nbsp;
<input type="checkbox" id="optional-parameters" style="width: auto;">
<input type="checkbox" id="optional-parameters" style="margin-left: 10px; vertical-align: text-bottom; width: auto;">
<label for="optional-parameters" id="optional-parameters-label" style="width: auto;"> <span data-i18n="{{&className}}.parameters.optionalParameters"></span></label>
</div>

{{#methods}}
{{#parameters}}
<div class="form-row" id="{{&methodName}}_{{&camelCaseName}}">
<label for="node-input-{{&methodName}}_{{&camelCaseName}}"><i class="fa fa-list"></i> <span data-i18n="{{&className}}.parameters.{{&camelCaseName}}"></span></label>

{{#tsType}}
{{#isEnum}}
<select id="node-input-{{&methodName}}_{{&camelCaseName}}">
{{/isEnum}}
{{/tsType}}

{{#enum}}
<option value="{{.}}">{{.}}</option>
{{/enum}}

{{#tsType}}
{{#isEnum}}
</select>
Expand All @@ -142,13 +142,15 @@

{{#tsType}}
{{^isEnum}}
<input type="text" id="node-input-{{&methodName}}_{{&camelCaseName}}" placeholder="{{&description}}">
<input type="text" id="node-input-{{&methodName}}_{{&camelCaseName}}" placeholder="{{&description}}" style="width:70%">
<input type="hidden" id="node-input-{{&methodName}}_{{&camelCaseName}}Type">
{{/isEnum}}
{{/tsType}}
</div>

{{/parameters}}
{{/methods}}

<hr/>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
Expand Down Expand Up @@ -301,4 +303,3 @@
{{/isSecureBasic}}
{{/isSecure}}
</script>

26 changes: 15 additions & 11 deletions templates/swagger/node.js.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = function (RED) {
{{#methods}}
{{#parameters}}
this.{{&methodName}}_{{&camelCaseName}} = config.{{&methodName}}_{{&camelCaseName}};
this.{{&methodName}}_{{&camelCaseName}}Type = config.{{&methodName}}_{{&camelCaseName}}Type || 'str';
{{/parameters}}
{{/methods}}

Expand Down Expand Up @@ -61,22 +62,26 @@ module.exports = function (RED) {
var errorFlag = false;
{{#methods}}
if (node.method === '{{&methodName}}') {
var parameters = [];
var parameters = [], nodeParam, nodeParamType;
{{#parameters}}
if ('{{&camelCaseName}}' === 'body') {
if (typeof msg.payload === 'object') {
parameters.{{&camelCaseName}} = msg.payload;
} else {
node.error('Unsupported type: \'' + (typeof msg.payload) + '\', '
+ 'msg.payload must be JSON object or buffer.');
errorFlag = true;
}
{{#isBodyParam}}
if (typeof msg.payload === 'object') {
parameters.{{&camelCaseName}} = msg.payload;
} else {
parameters.{{&camelCaseName}} = node.{{&methodName}}_{{&camelCaseName}};
node.error('Unsupported type: \'' + (typeof msg.payload) + '\', '
+ 'msg.payload must be JSON object or buffer.');
errorFlag = true;
}
{{/isBodyParam}}
{{#isNotBodyParam}}
nodeParam = node.{{&methodName}}_{{&camelCaseName}};
nodeParamType = node.{{&methodName}}_{{&camelCaseName}}Type;
parameters.{{&camelCaseName}} = nodeParamType === 'str' ? nodeParam || '' : RED.util.getMessageProperty(msg, nodeParam);
{{/isNotBodyParam}}
{{/parameters}}
result = client.{{&methodName}}(parameters);
}

{{/methods}}
if (!errorFlag) {
node.status({ fill: "blue", shape: "dot", text: "{{&className}}.status.requesting" });
Expand Down Expand Up @@ -140,4 +145,3 @@ module.exports = function (RED) {
}
});
};