1 | const ArgumentParser = require('argparse').ArgumentParser, |
||
2 | fs = require('fs'), |
||
3 | path = require('path'), |
||
4 | DocGenerator = require('./DocGenerator'), |
||
5 | SwaggerParser = require('./SwaggerParser'), |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
6 | GenerateApi = require('./GenerateApi') |
||
7 | |||
8 | /** |
||
9 | * @class InitCli |
||
10 | * Init command line interface options |
||
11 | * |
||
12 | * @property {ArgumentParser} parser Command line arguments parser |
||
13 | * @property {Object} arguments Arguments from cli |
||
14 | * @property {String} workingDir Working directory |
||
15 | */ |
||
16 | class InitCli { |
||
17 | /** |
||
18 | * |
||
19 | * @param {String} version Command script version |
||
20 | * @param {String} description Command script description |
||
21 | */ |
||
22 | constructor (version, description) { |
||
23 | this.parser = new ArgumentParser({ |
||
24 | version : version, |
||
25 | description: description, |
||
26 | addHelp : true |
||
27 | }) |
||
28 | |||
29 | this.workingDir = process.cwd() |
||
30 | |||
31 | this.arguments = {} |
||
32 | |||
33 | this._defaultArguments() |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Init default cli arguments |
||
38 | * |
||
39 | * @private |
||
40 | */ |
||
41 | _defaultArguments () { |
||
42 | this.addArgument( |
||
43 | ['-s', '--source'], |
||
44 | { |
||
45 | help : 'Source swagger file destination', |
||
46 | required: true |
||
47 | } |
||
48 | ) |
||
49 | |||
50 | this.addArgument( |
||
51 | ['-f', '--flag-version'], |
||
52 | { |
||
53 | help : 'Set swagger version (2.0 or openapi). 2.0 only supported | [optional]. Default value is 2.0', |
||
54 | required : false, |
||
55 | defaultValue: '2.0' |
||
56 | } |
||
57 | ) |
||
58 | |||
59 | this.addArgument( |
||
60 | ['-m', '--moduleName'], |
||
61 | { |
||
62 | help : 'Swagger generator module name', |
||
63 | required: true |
||
64 | } |
||
65 | ) |
||
66 | |||
67 | this.addArgument( |
||
68 | ['-c', '--className'], |
||
69 | { |
||
70 | help : 'Swagger generator class name', |
||
71 | defaultValue: 'API', |
||
72 | } |
||
73 | ) |
||
74 | |||
75 | this.addArgument( |
||
76 | ['-d', '--destination'], |
||
77 | { |
||
78 | help : 'Destination for output generate client ', |
||
79 | required: true |
||
80 | } |
||
81 | ) |
||
82 | |||
83 | this.addArgument( |
||
84 | ['-n', '--package-name'], |
||
85 | { |
||
86 | help : 'Package name', |
||
87 | defaultValue: 'vue-swagger-api' |
||
88 | } |
||
89 | ) |
||
90 | |||
91 | this.addArgument( |
||
92 | ['--package-version'], |
||
93 | { |
||
94 | help : 'Package version', |
||
95 | defaultValue: '0.2' |
||
96 | } |
||
97 | ) |
||
98 | |||
99 | this.addArgument( |
||
100 | ['-p', '--repo-path'], |
||
101 | { |
||
102 | help: 'Path to repository for auto detect version' |
||
103 | } |
||
104 | ) |
||
105 | |||
106 | this.addArgument( |
||
107 | ['-t', '--tag-command'], |
||
108 | { |
||
109 | help : 'Command for get tags list', |
||
110 | defaultValue: 'git tag -l' |
||
111 | } |
||
112 | ) |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Add options for documentation generator |
||
117 | */ |
||
118 | addDocumentGeneratorArguments () { |
||
119 | this.addArgument( |
||
120 | ['--doc-path'], |
||
121 | { |
||
122 | help : 'Destination for methods descriptions', |
||
123 | required : false, |
||
124 | defaultValue: 'docs', |
||
125 | } |
||
126 | ) |
||
127 | |||
128 | this.addArgument( |
||
129 | ['--model-path'], |
||
130 | { |
||
131 | help : 'Destination for models description', |
||
132 | required : false, |
||
133 | defaultValue: 'docs/models', |
||
134 | } |
||
135 | ) |
||
136 | |||
137 | this.parser.addArgument( |
||
138 | ['--html'], |
||
139 | { |
||
140 | help : 'Generate in Html format', |
||
141 | action: 'storeTrue' |
||
142 | } |
||
143 | ) |
||
144 | |||
145 | this.parser.addArgument( |
||
146 | ['--md'], |
||
147 | { |
||
148 | help : 'Generate in md format', |
||
149 | action: 'storeFalse' |
||
150 | } |
||
151 | ) |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Add argument definition |
||
156 | * |
||
157 | * @param {Array.<String>} command Argument definition |
||
158 | * @param {Object} options Argument options |
||
159 | */ |
||
160 | addArgument (command, options) { |
||
161 | this.parser.addArgument(command, options) |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Generate api file |
||
166 | */ |
||
167 | generateApi () { |
||
168 | |||
169 | this.parse() |
||
170 | |||
171 | let jsonFile = fs.existsSync(this.arguments.source) ? this.arguments.source : path.join(process.cwd(), this.arguments.source), |
||
172 | generator = new GenerateApi(jsonFile, { |
||
173 | outFile : this.arguments.destination, |
||
174 | moduleName: this.arguments.moduleName, |
||
175 | className : this.arguments.className, |
||
176 | }) |
||
177 | |||
178 | generator.generate() |
||
179 | } |
||
180 | |||
181 | _getDocOutFormat () { |
||
182 | if (this.arguments.md) { |
||
183 | return DocGenerator.Markdown |
||
184 | } |
||
185 | |||
186 | return DocGenerator.HTML |
||
187 | } |
||
188 | |||
189 | generateDoc () { |
||
190 | this.addDocumentGeneratorArguments() |
||
191 | |||
192 | this.parse() |
||
193 | |||
194 | let jsonFile = this._getJsonFile(), |
||
195 | docs = new DocGenerator(this._getDocOutFormat(), { |
||
196 | inputJson : jsonFile, |
||
197 | moduleName : this.arguments.moduleName, |
||
198 | modelPath : this.arguments.model_path, |
||
199 | docsPath : this.arguments.doc_path, |
||
200 | className : this.arguments.className, |
||
201 | packageName : this.arguments.package_name, |
||
202 | packageVersion: this.arguments.package_version, |
||
203 | destination : this.arguments.destination |
||
204 | }, this.arguments.flag_version) |
||
205 | |||
206 | docs.generate() |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Get json file path |
||
211 | * |
||
212 | * @return {String} |
||
213 | * @private |
||
214 | */ |
||
215 | _getJsonFile () { |
||
216 | return fs.existsSync(this.parser.source) ? this.arguments.source : path.join(process.cwd(), this.arguments.source) |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Parse cli options & arguments |
||
221 | */ |
||
222 | parse () { |
||
223 | this.arguments = this.parser.parseArgs() |
||
224 | |||
225 | return this.arguments |
||
226 | } |
||
227 | } |
||
228 | |||
229 | module.exports = InitCli |