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
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Generated files
node-red-contrib-*
node-red-node-*
./nodegen
samples
nodegen/*

# Dependency directories
node_modules
Expand Down
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ before_script:
- docker pull swaggerapi/petstore
- docker run -d -e SWAGGER_URL=http://petstore.swagger.io -e SWAGGER_BASE_PATH=/v2 -p 80:8080 swaggerapi/petstore
- npm install -g istanbul coveralls
- jq '.schemes=["http"]' samples/swagger.json > samples/tmp
- mv samples/tmp samples/swagger.json
14 changes: 6 additions & 8 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module.exports = function (grunt) {
grunt.initConfig({
shell: {
generateNode_lowerCase: {
generateNode_Function: {
command: 'node bin/node-red-nodegen.js samples/lower-case.js -o ./nodegen'
},
getSwagger_swaggerPetstore: {
command: 'node bin/getswagger.js'
},
generateNode_swaggerPetstore: {
generateNode_Swagger: {
command: 'node bin/node-red-nodegen.js samples/swagger.json -o ./nodegen'
},
generateNode_WebOfThings: {
command: 'node bin/node-red-nodegen.js samples/MyLampThing.jsonld -o ./nodegen'
}
},
simplemocha: {
Expand All @@ -31,7 +31,5 @@ module.exports = function (grunt) {
grunt.file.mkdir('nodegen');
grunt.loadNpmTasks('grunt-shell');
grunt.loadNpmTasks('grunt-simple-mocha');
grunt.loadNpmTasks('grunt-mocha-istanbul');
grunt.registerTask('default', ['shell', 'mocha_istanbul:all']);
grunt.registerTask('coverage', 'Run Istanbul code test coverage task', ['shell', 'mocha_istanbul:all']);
grunt.registerTask('default', ['shell', 'simplemocha']);
};
35 changes: 0 additions & 35 deletions bin/getswagger.js

This file was deleted.

138 changes: 32 additions & 106 deletions bin/node-red-nodegen.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

/**
* Copyright JS Foundation and other contributors, http://js.foundation
* Copyright OpenJS Foundation and other contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,11 +18,9 @@

var fs = require('fs');
var path = require('path');
var request = require('request');
var yamljs = require('yamljs');

var argv = require('minimist')(process.argv.slice(2));
var colors = require('colors');
var Converter = require('api-spec-converter');
var nodegen = require('../lib/nodegen.js');

// Command options
Expand All @@ -39,7 +37,8 @@ var data = {
category: argv.category || argv.c,
icon: argv.icon,
color: argv.color,
dst: argv.output || argv.o || '.'
dst: argv.output || argv.o || '.',
lang: argv.lang
};

function help() {
Expand Down Expand Up @@ -108,115 +107,42 @@ if (argv.help || argv.h) {
} else if (argv.v) {
version();
} else {
var promise;
var sourcePath = argv._[0];
if (sourcePath) {
if (!argv.wottd && (sourcePath.startsWith('http://') || sourcePath.startsWith('https://'))) {
request(sourcePath, function (error, response, body) {
if (!error) {
data.src = JSON.parse(body);
Converter.convert({
from: data.src.openapi && data.src.openapi.startsWith('3.0') ? 'openapi_3' : 'swagger_2',
to: 'swagger_2',
source: data.src,
}).then(function (convertedData) {
data.src = convertedData.spec;
nodegen.swagger2node(data, options).then(function (result) {
console.log('Success: ' + result);
}).catch(function (error) {
console.log('Error: ' + error);
});
});
} else {
console.error(error);
}
});
} else if (argv.wottd && (sourcePath.startsWith('http://') || sourcePath.startsWith('https://'))) {
const req = {
url: sourcePath,
}
if (argv.lang) {
req.headers = {
'Accept-Language': argv.lang
}
}
request(req, function (error, response, body) {
if (!error) {
data.src = JSON.parse(skipBom(body));
nodegen.wottd2node(data, options).then(function (result) {
console.log('Success: ' + result);
}).catch(function (error) {
console.log('Error: ' + error);
});
} else {
console.error(error);
}
});
} else if (sourcePath.endsWith('.json') && !argv.wottd) {
data.src = JSON.parse(fs.readFileSync(sourcePath));
// if it's a .json flow file with one function node in...
if (Array.isArray(data.src) && data.src[0].hasOwnProperty("type") && data.src[0].type == "function") {
var f = data.src[0];
if (!f.name || f.name.length ==0) { console.log('Error: No function name supplied.'); return; }
data.name = f.name.toLowerCase();
data.icon = f.icon;
data.info = f.info;
data.outputs = f.outputs;
data.inputLabels = f.inputLabels;
data.outputLabels = f.outputLabels;
data.src = Buffer.from(f.func);
nodegen.function2node(data, options).then(function (result) {
console.log('Success: ' + result);
}).catch(function (error) {
console.log('Error: ' + error);
});
data.src = sourcePath;
if (argv.wottd || /\.jsonld$/.test(sourcePath)) {
// Explicitly a Web Of Things request
promise = nodegen.WebOfThingsGenerator(data, options);
} else if (/^https?:/.test(sourcePath) || /.yaml$/.test(sourcePath)) {
// URL/yaml -> swagger
promise = nodegen.SwaggerNodeGenerator(data, options);
} else if (/\.json$/.test(sourcePath)) {
// JSON could be a Function node, or Swagger
var content = JSON.parse(fs.readFileSync(sourcePath));
if (Array.isArray(content)) {
data.src = content;
promise = nodegen.FunctionNodeGenerator(data, options);
} else {
promise = nodegen.SwaggerNodeGenerator(data, options);
}
else {
Converter.convert({
from: data.src.openapi && data.src.openapi.startsWith('3.0') ? 'openapi_3' : 'swagger_2',
to: 'swagger_2',
source: data.src,
}).then(function (convertedData) {
data.src = convertedData.spec;
nodegen.swagger2node(data, options).then(function (result) {
console.log('Success: ' + result);
}).catch(function (error) {
console.log('Error: ' + error);
});
});
}
} else if (sourcePath.endsWith('.yaml')) {
data.src = yamljs.load(sourcePath);
console.log(JSON.stringify(data.src, null, 4)); // hoge
Converter.convert({
from: data.src.openapi && data.src.openapi.startsWith('3.0') ? 'openapi_3' : 'swagger_2',
to: 'swagger_2',
source: data.src,
}).then(function (convertedData) {
data.src = convertedData.spec;
nodegen.swagger2node(data, options).then(function (result) {
console.log('Success: ' + result);
}).catch(function (error) {
console.log('Error: ' + error);
});
});
} else if (sourcePath.endsWith('.js')) {
data.src = fs.readFileSync(sourcePath);
nodegen.function2node(data, options).then(function (result) {
console.log('Success: ' + result);
}).catch(function (error) {
console.log('Error: ' + error);
});
} else if (sourcePath.endsWith('.jsonld') || argv.wottd) {
data.src = JSON.parse(skipBom(fs.readFileSync(sourcePath)));
nodegen.wottd2node(data, options).then(function (result) {
} else if (/\.js$/.test(sourcePath)) {
// .js -> Function node
promise = nodegen.FunctionNodeGenerator(data, options);
} else {
console.error('error: Unsupported file type');
help();
return;
}
if (promise) {
promise.then(function (result) {
console.log('Success: ' + result);
}).catch(function (error) {
console.log('Error: ' + error);
console.log(error.stack);
});
} else {
console.error('error: Unsupported file type');
}
} else {
help();
}
}
}
Loading