Skip to content

Commit b010342

Browse files
committed
Improve CLI to accept codegen options
- Can now generate all types of js codes except custom Signed-off-by: Pouyan Heyratpour <[email protected]>
1 parent 9092973 commit b010342

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed
File renamed without changes.

lib/cli.js

+37-9
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,52 @@
33
const fs = require('fs');
44
const pkg = require('../package.json');
55
const cli = require('commander');
6+
const yaml = require('js-yaml').safeLoad;
67
const CodeGen = require('./codegen').CodeGen;
8+
const fnMap = {
9+
'typescript': CodeGen.getTypescriptCode,
10+
'angular': CodeGen.getAngularCode,
11+
'node': CodeGen.getNodeCode,
12+
'react': CodeGen.getReactCode,
13+
};
714

815
cli
9-
.command('generate <file>')
16+
.version(pkg.version)
17+
.command('generate <file> [imports...]')
18+
.alias('gen')
1019
.description('Generate from Swagger file')
11-
.action(file => {
12-
const result = CodeGen.getTypescriptCode({
13-
moduleName: 'Test',
14-
className: 'Test',
15-
swagger: JSON.parse(fs.readFileSync(file, 'utf-8')),
16-
lint: false
20+
.option('-t, --type <type>', 'Code type [typescript]', /^(typescript|angular|node|react)$/i, 'typescript')
21+
.option('-m, --module <module>', 'Your AngularJS module name [Test]', 'Test')
22+
.option('-c, --class <class>', 'Class name [Test]', 'Test')
23+
.option('-l, --lint', 'Whether or not to run jslint on the generated code [false]')
24+
.option('-b, --beautify', 'Whether or not to beautify the generated code [false]')
25+
.action((file, imports, options) => {
26+
const fn = fnMap[options.type];
27+
options.lint = options.lint || false;
28+
options.beautify = options.beautify || false;
29+
30+
const content = fs.readFileSync(file, 'utf-8');
31+
32+
var swagger;
33+
try {
34+
swagger = JSON.parse(content);
35+
} catch (e) {
36+
swagger = yaml(content);
37+
}
38+
39+
const result = fn({
40+
moduleName: options.module,
41+
className: options.class,
42+
swagger: swagger,
43+
lint: options.lint,
44+
beautify: options.beautify
1745
});
46+
1847
console.log(result);
1948
});
2049

21-
cli.version(pkg.version);
2250
cli.parse(process.argv);
2351

2452
if (!cli.args.length) {
2553
cli.help();
26-
}
54+
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"clean": "rm -rf tmp-*"
99
},
1010
"bin": {
11-
"swagger2ts": "bin/swagger2ts.js"
11+
"swagger2js": "bin/swagger2js.js"
1212
},
1313
"bugs": {
1414
"url": "https://github.com/wcandillon/swagger-js-codegen/issues"
@@ -31,6 +31,7 @@
3131
"dependencies": {
3232
"commander": "^2.9.0",
3333
"js-beautify": "^1.5.1",
34+
"js-yaml": "^3.10.0",
3435
"jshint": "^2.5.1",
3536
"lodash": "^4.15.0",
3637
"mustache": "^2.0.0",

0 commit comments

Comments
 (0)