Skip to content

Commit 4fc0117

Browse files
Merge pull request #114 from node-red/refactor
Restructure code layout
2 parents d914f7e + 6e53d62 commit 4fc0117

File tree

20 files changed

+1151
-1171
lines changed

20 files changed

+1151
-1171
lines changed

.gitignore

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

74
# Dependency directories
85
node_modules

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ before_script:
1616
- docker pull swaggerapi/petstore
1717
- docker run -d -e SWAGGER_URL=http://petstore.swagger.io -e SWAGGER_BASE_PATH=/v2 -p 80:8080 swaggerapi/petstore
1818
- npm install -g istanbul coveralls
19+
- jq '.schemes=["http"]' samples/swagger.json > samples/tmp
20+
- mv samples/tmp samples/swagger.json

Gruntfile.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +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-
getSwagger_swaggerPetstore: {
8-
command: 'node bin/getswagger.js'
9-
},
10-
generateNode_swaggerPetstore: {
7+
generateNode_Swagger: {
118
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'
1212
}
1313
},
1414
simplemocha: {
@@ -31,7 +31,5 @@ module.exports = function (grunt) {
3131
grunt.file.mkdir('nodegen');
3232
grunt.loadNpmTasks('grunt-shell');
3333
grunt.loadNpmTasks('grunt-simple-mocha');
34-
grunt.loadNpmTasks('grunt-mocha-istanbul');
35-
grunt.registerTask('default', ['shell', 'mocha_istanbul:all']);
36-
grunt.registerTask('coverage', 'Run Istanbul code test coverage task', ['shell', 'mocha_istanbul:all']);
34+
grunt.registerTask('default', ['shell', 'simplemocha']);
3735
};

bin/getswagger.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

bin/node-red-nodegen.js

Lines changed: 32 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

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

1919
var fs = require('fs');
2020
var path = require('path');
21-
var request = require('request');
22-
var yamljs = require('yamljs');
21+
2322
var argv = require('minimist')(process.argv.slice(2));
2423
var colors = require('colors');
25-
var Converter = require('api-spec-converter');
2624
var nodegen = require('../lib/nodegen.js');
2725

2826
// Command options
@@ -39,7 +37,8 @@ var data = {
3937
category: argv.category || argv.c,
4038
icon: argv.icon,
4139
color: argv.color,
42-
dst: argv.output || argv.o || '.'
40+
dst: argv.output || argv.o || '.',
41+
lang: argv.lang
4342
};
4443

4544
function help() {
@@ -108,115 +107,42 @@ if (argv.help || argv.h) {
108107
} else if (argv.v) {
109108
version();
110109
} else {
110+
var promise;
111111
var sourcePath = argv._[0];
112112
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-
});
113+
data.src = sourcePath;
114+
if (argv.wottd || /\.jsonld$/.test(sourcePath)) {
115+
// Explicitly a Web Of Things request
116+
promise = nodegen.WebOfThingsGenerator(data, options);
117+
} else if (/^https?:/.test(sourcePath) || /.yaml$/.test(sourcePath)) {
118+
// URL/yaml -> swagger
119+
promise = nodegen.SwaggerNodeGenerator(data, options);
120+
} else if (/\.json$/.test(sourcePath)) {
121+
// JSON could be a Function node, or Swagger
122+
var content = JSON.parse(fs.readFileSync(sourcePath));
123+
if (Array.isArray(content)) {
124+
data.src = content;
125+
promise = nodegen.FunctionNodeGenerator(data, options);
126+
} else {
127+
promise = nodegen.SwaggerNodeGenerator(data, options);
172128
}
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) {
129+
} else if (/\.js$/.test(sourcePath)) {
130+
// .js -> Function node
131+
promise = nodegen.FunctionNodeGenerator(data, options);
132+
} else {
133+
console.error('error: Unsupported file type');
134+
help();
135+
return;
136+
}
137+
if (promise) {
138+
promise.then(function (result) {
212139
console.log('Success: ' + result);
213140
}).catch(function (error) {
214141
console.log('Error: ' + error);
142+
console.log(error.stack);
215143
});
216-
} else {
217-
console.error('error: Unsupported file type');
218144
}
219145
} else {
220146
help();
221147
}
222-
}
148+
}

0 commit comments

Comments
 (0)