3
3
const fs = require ( 'fs' ) ;
4
4
const pkg = require ( '../package.json' ) ;
5
5
const cli = require ( 'commander' ) ;
6
+ const yaml = require ( 'js-yaml' ) . safeLoad ;
6
7
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
+ } ;
7
14
8
15
cli
9
- . command ( 'generate <file>' )
16
+ . version ( pkg . version )
17
+ . command ( 'generate <file> [imports...]' )
18
+ . alias ( 'gen' )
10
19
. 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]' , / ^ ( t y p e s c r i p t | a n g u l a r | n o d e | r e a c t ) $ / 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
17
45
} ) ;
46
+
18
47
console . log ( result ) ;
19
48
} ) ;
20
49
21
- cli . version ( pkg . version ) ;
22
50
cli . parse ( process . argv ) ;
23
51
24
52
if ( ! cli . args . length ) {
25
53
cli . help ( ) ;
26
- }
54
+ }
0 commit comments