Skip to content

Commit 6da0554

Browse files
committed
feat: calling APIs of swagger-codegen / openapi-generator to list templates
1 parent 5958bca commit 6da0554

File tree

4 files changed

+89
-6
lines changed

4 files changed

+89
-6
lines changed

cg.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ const swagger2openapi = require('swagger2openapi');
1414
const stools = require('swagger-tools');
1515
const admzip = require('adm-zip');
1616

17-
const processor = require('./index.js');
17+
const processor = require('./local.js');
18+
const remote = require('./remote.js');
19+
20+
async function list(provider) {
21+
await remote.list(provider);
22+
process.exit(1);
23+
}
1824

1925
var argv = require('yargs')
2026
.usage('cg [options] {[path]configName} {openapi-definition}')
@@ -27,6 +33,8 @@ var argv = require('yargs')
2733
.boolean('lint')
2834
.alias('l','lint')
2935
.describe('lint','Lint input definition')
36+
.string('list')
37+
.describe('list','List available templates for provider (og or sc)')
3038
.string('output')
3139
.alias('o','output')
3240
.describe('output','Specify output directory')
@@ -46,14 +54,21 @@ var argv = require('yargs')
4654
.version()
4755
.argv;
4856

57+
if (argv.list) {
58+
list(argv.list);
59+
}
60+
4961
let configStr = argv._[0] || 'nodejs';
5062
let configName = path.basename(configStr);
63+
let remoteConfig = configName.indexOf(':')>-1;
5164
let configPath = path.dirname(configStr);
5265
if (!configPath || (configPath === '.')) configPath = './configs';
5366
let configFile = path.resolve(configPath,configName)+'.json';
5467
let config = require(configFile);
5568
let defName = argv._[1] || './defs/petstore3.json';
5669

70+
let finish = remoteConfig ? finishRemote : finishLocal;
71+
5772
config.outputDir = argv.output;
5873
config.templateDir = argv.templates;
5974

@@ -70,7 +85,7 @@ function zipFile(filename,contents,encoding) {
7085
zipFiles[filename] = contents;
7186
}
7287

73-
function finish(err,result) {
88+
function finishLocal(err,result) {
7489
if (argv.zip) {
7590
// create archive
7691
var zip = new admzip();
@@ -84,6 +99,24 @@ function finish(err,result) {
8499
}
85100
}
86101

102+
function finishRemote(err,result) {
103+
if (argv.zip) {
104+
// just save the zip file
105+
}
106+
else {
107+
// unpack the zip file
108+
}
109+
}
110+
111+
function despatch(obj, config, configName, callback) {
112+
if (remoteConfig) {
113+
remote.main(obj, config, configName, callback);
114+
}
115+
else {
116+
processor.main(obj, config, configName, callback);
117+
}
118+
}
119+
87120
function convert20(obj){
88121
if (argv.verbose) console.log('Converting OpenAPI 2.0 definition');
89122
swagger2openapi.convertObj(obj,{patch:true,warnOnly:true,direct:true},function(err,openapi){
@@ -92,7 +125,7 @@ function convert20(obj){
92125
}
93126
else {
94127
config.defaults.swagger = obj;
95-
processor.main(openapi,config,configName,finish);
128+
despatch(openapi,config,configName,finish);
96129
}
97130
});
98131
}
@@ -176,14 +209,19 @@ function main(s) {
176209
if (argv.verbose) console.log('Loaded definition '+defName);
177210

178211
if (o && o.openapi) {
179-
processor.main(o,config,configName,finish);
212+
despatch(o,config,configName,finish);
180213
}
181214
else {
182215
if (o && o.swaggerVersion && o.swaggerVersion === '1.2') {
183216
convert12(o);
184217
}
185218
else if (o && o.swagger && o.swagger === '2.0') {
186-
convert20(o);
219+
if (remoteConfig) {
220+
despatch(o,config,configName,finish);
221+
}
222+
else {
223+
convert20(o);
224+
}
187225
}
188226
else {
189227
console.error('Unrecognised OpenAPI/Swagger version');
File renamed without changes.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "openapi-codegen",
33
"version": "1.5.6",
44
"description": "OpenAPI 3.0 CodeGen",
5-
"main": "index.js",
5+
"main": "local.js",
66
"bin": {
77
"cg": "./cg.js"
88
},

remote.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// @ts-check
2+
'use strict';
3+
4+
const fetch = require('node-fetch');
5+
const util = require('util');
6+
7+
function main(obj, config, configName, callback) {
8+
}
9+
10+
function format(templates, prefix, type) {
11+
for (let template of templates) {
12+
console.log(prefix+':'+type+':'+template);
13+
}
14+
}
15+
16+
async function slurp(server, prefix, type) {
17+
await fetch(server+type+'s')
18+
.then(res => {
19+
return res.text();
20+
})
21+
.then(data => {
22+
format(JSON.parse(data), prefix, type);
23+
})
24+
.catch(err => {
25+
console.error(util.inspect(err));
26+
});
27+
}
28+
29+
async function list(prefix) {
30+
let server = '';
31+
if (prefix === 'og') {
32+
server = 'https://api.openapi-generator.tech/api/gen/';
33+
}
34+
else if (prefix === 'sc') {
35+
server = 'https://generator.swagger.io/api/gen/';
36+
}
37+
await slurp(server, prefix, 'client');
38+
await slurp(server, prefix, 'server');
39+
}
40+
41+
module.exports = {
42+
main,
43+
list
44+
};
45+

0 commit comments

Comments
 (0)