Skip to content

Commit ca9e577

Browse files
committed
Move more generator specific logic into individual modules
1 parent a6f226a commit ca9e577

File tree

8 files changed

+669
-673
lines changed

8 files changed

+669
-673
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Generated files
2-
node-red-contrib-*
3-
node-red-node-*
4-
./nodegen
2+
nodegen/*
53

64
# Dependency directories
75
node_modules

Gruntfile.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
module.exports = function (grunt) {
22
grunt.initConfig({
33
shell: {
4-
generateNode_lowerCase: {
4+
generateNode_Function: {
55
command: 'node bin/node-red-nodegen.js samples/lower-case.js -o ./nodegen'
66
},
7-
generateNode_swaggerPetstore: {
7+
generateNode_Swagger: {
88
command: 'node bin/node-red-nodegen.js samples/swagger.json -o ./nodegen'
9+
},
10+
generateNode_WebOfThings: {
11+
command: 'node bin/node-red-nodegen.js samples/MyLampThing.jsonld -o ./nodegen'
912
}
1013
},
1114
simplemocha: {
@@ -29,5 +32,4 @@ module.exports = function (grunt) {
2932
grunt.loadNpmTasks('grunt-shell');
3033
grunt.loadNpmTasks('grunt-simple-mocha');
3134
grunt.registerTask('default', ['shell', 'simplemocha']);
32-
grunt.registerTask('coverage', 'Run Istanbul code test coverage task', ['shell', 'mocha_istanbul:all']);
3335
};

bin/node-red-nodegen.js

Lines changed: 31 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
var fs = require('fs');
2020
var path = require('path');
2121
var request = require('request');
22-
var yamljs = require('yamljs');
22+
2323
var argv = require('minimist')(process.argv.slice(2));
2424
var colors = require('colors');
25-
var Converter = require('api-spec-converter');
2625
var nodegen = require('../lib/nodegen.js');
2726

2827
// Command options
@@ -39,7 +38,8 @@ var data = {
3938
category: argv.category || argv.c,
4039
icon: argv.icon,
4140
color: argv.color,
42-
dst: argv.output || argv.o || '.'
41+
dst: argv.output || argv.o || '.',
42+
lang: argv.lang
4343
};
4444

4545
function help() {
@@ -108,115 +108,42 @@ if (argv.help || argv.h) {
108108
} else if (argv.v) {
109109
version();
110110
} else {
111+
var promise;
111112
var sourcePath = argv._[0];
112113
if (sourcePath) {
113-
if (!argv.wottd && (sourcePath.startsWith('http://') || sourcePath.startsWith('https://'))) {
114-
request(sourcePath, function (error, response, body) {
115-
if (!error) {
116-
data.src = JSON.parse(body);
117-
Converter.convert({
118-
from: data.src.openapi && data.src.openapi.startsWith('3.0') ? 'openapi_3' : 'swagger_2',
119-
to: 'swagger_2',
120-
source: data.src,
121-
}).then(function (convertedData) {
122-
data.src = convertedData.spec;
123-
nodegen.swagger2node(data, options).then(function (result) {
124-
console.log('Success: ' + result);
125-
}).catch(function (error) {
126-
console.log('Error: ' + error);
127-
});
128-
});
129-
} else {
130-
console.error(error);
131-
}
132-
});
133-
} else if (argv.wottd && (sourcePath.startsWith('http://') || sourcePath.startsWith('https://'))) {
134-
const req = {
135-
url: sourcePath,
136-
}
137-
if (argv.lang) {
138-
req.headers = {
139-
'Accept-Language': argv.lang
140-
}
141-
}
142-
request(req, function (error, response, body) {
143-
if (!error) {
144-
data.src = JSON.parse(skipBom(body));
145-
nodegen.wottd2node(data, options).then(function (result) {
146-
console.log('Success: ' + result);
147-
}).catch(function (error) {
148-
console.log('Error: ' + error);
149-
});
150-
} else {
151-
console.error(error);
152-
}
153-
});
154-
} else if (sourcePath.endsWith('.json') && !argv.wottd) {
155-
data.src = JSON.parse(fs.readFileSync(sourcePath));
156-
// if it's a .json flow file with one function node in...
157-
if (Array.isArray(data.src) && data.src[0].hasOwnProperty("type") && data.src[0].type == "function") {
158-
var f = data.src[0];
159-
if (!f.name || f.name.length ==0) { console.log('Error: No function name supplied.'); return; }
160-
data.name = f.name.toLowerCase();
161-
data.icon = f.icon;
162-
data.info = f.info;
163-
data.outputs = f.outputs;
164-
data.inputLabels = f.inputLabels;
165-
data.outputLabels = f.outputLabels;
166-
data.src = Buffer.from(f.func);
167-
nodegen.function2node(data, options).then(function (result) {
168-
console.log('Success: ' + result);
169-
}).catch(function (error) {
170-
console.log('Error: ' + error);
171-
});
114+
data.src = sourcePath;
115+
if (argv.wottd || /\.jsonld$/.test(sourcePath)) {
116+
// Explicitly a Web Of Things request
117+
promise = nodegen.wottd2node(data, options);
118+
} else if (/^https?:/.test(sourcePath) || /.yaml$/.test(sourcePath)) {
119+
// URL/yaml -> swagger
120+
promise = nodegen.swagger2node(data, options);
121+
} else if (/\.json$/.test(sourcePath)) {
122+
// JSON could be a Function node, or Swagger
123+
var content = JSON.parse(fs.readFileSync(sourcePath));
124+
if (Array.isArray(content)) {
125+
data.src = content;
126+
promise = nodegen.function2node(data, options);
127+
} else {
128+
promise = nodegen.swagger2node(data, options);
172129
}
173-
else {
174-
Converter.convert({
175-
from: data.src.openapi && data.src.openapi.startsWith('3.0') ? 'openapi_3' : 'swagger_2',
176-
to: 'swagger_2',
177-
source: data.src,
178-
}).then(function (convertedData) {
179-
data.src = convertedData.spec;
180-
nodegen.swagger2node(data, options).then(function (result) {
181-
console.log('Success: ' + result);
182-
}).catch(function (error) {
183-
console.log('Error: ' + error);
184-
});
185-
});
186-
}
187-
} else if (sourcePath.endsWith('.yaml')) {
188-
data.src = yamljs.load(sourcePath);
189-
console.log(JSON.stringify(data.src, null, 4)); // hoge
190-
Converter.convert({
191-
from: data.src.openapi && data.src.openapi.startsWith('3.0') ? 'openapi_3' : 'swagger_2',
192-
to: 'swagger_2',
193-
source: data.src,
194-
}).then(function (convertedData) {
195-
data.src = convertedData.spec;
196-
nodegen.swagger2node(data, options).then(function (result) {
197-
console.log('Success: ' + result);
198-
}).catch(function (error) {
199-
console.log('Error: ' + error);
200-
});
201-
});
202-
} else if (sourcePath.endsWith('.js')) {
203-
data.src = fs.readFileSync(sourcePath);
204-
nodegen.function2node(data, options).then(function (result) {
205-
console.log('Success: ' + result);
206-
}).catch(function (error) {
207-
console.log('Error: ' + error);
208-
});
209-
} else if (sourcePath.endsWith('.jsonld') || argv.wottd) {
210-
data.src = JSON.parse(skipBom(fs.readFileSync(sourcePath)));
211-
nodegen.wottd2node(data, options).then(function (result) {
130+
} else if (/\.js$/.test(sourcePath)) {
131+
// .js -> Function node
132+
promise = nodegen.function2node(data, options);
133+
} else {
134+
console.error('error: Unsupported file type');
135+
help();
136+
return;
137+
}
138+
if (promise) {
139+
promise.then(function (result) {
212140
console.log('Success: ' + result);
213141
}).catch(function (error) {
214142
console.log('Error: ' + error);
143+
console.log(error.stack);
215144
});
216-
} else {
217-
console.error('error: Unsupported file type');
218145
}
219146
} else {
220147
help();
221148
}
222-
}
149+
}

0 commit comments

Comments
 (0)