-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathng-swagger-gen
executable file
·188 lines (180 loc) · 5.33 KB
/
ng-swagger-gen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env node
'use strict';
function parseJSON(file) {
return JSON.parse(fs.readFileSync(file, "utf8"));
}
const DEFAULT_CONFIG = 'ng-swagger-gen.json';
const SCHEMA = 'ng-swagger-gen-schema.json';
const fs = require('fs');
const path = require('path');
const ArgumentParser = require('argparse').ArgumentParser;
const pkg = parseJSON(path.join(__dirname, "package.json"));
const schema = parseJSON(path.join(__dirname, SCHEMA));
var argParser = new ArgumentParser({
version: pkg.version,
addHelp: true,
description: "Swagger API client generator for Angular 2+ projects." +
" Either uses a configuration file (defaults to " + DEFAULT_CONFIG + ")" +
" in the current directory, but the file can be specified)" +
" allowing the program to be called without arguments, or the swagger input" +
" file must be specified. A configuration file can be generated with all" +
" default values if --gen-config is specified, together with the swagger" +
" input file, which is also required in this case."
});
argParser.addArgument(
["-i", "--input", "-s", "--swagger"],
{
help: "The swagger input file or URL. If not specified, it is required" +
" that a configuration file called " + DEFAULT_CONFIG +
" exist in the current directory, or a configuration file can be" +
" specified with the -c or --config option.",
dest: "swagger",
metavar: "SWAGGER"
}
);
argParser.addArgument(
["-o", "--output"],
{
help: "The root directory where files will be generated.",
dest: "output"
}
);
argParser.addArgument(
["-c", "--config"],
{
help: "Specifies the configuration file name.",
defaultValue: DEFAULT_CONFIG,
dest: "config"
}
);
argParser.addArgument(
["--gen-config"],
{
help: "Generates the configuration file " + DEFAULT_CONFIG +
" in the current directory, or the file specified with -c or --config." +
" No Swagger client classes are generated." +
" If input and output are specified, their values are stored in the" +
" generated file as well.",
action: "storeTrue",
dest: "genConfig"
}
);
argParser.addArgument(
["--timeout"],
{
help: "The amount of time (in milliseconds) to wait for a response from the" +
" server when downloading files. The default is 20 seconds.",
type: "int",
dest: "timeout"
}
);
argParser.addArgument(
["--skip-proxy-setup"],
{
help: "Skip the proxy setup when unable to generate from localhost.",
action: "storeTrue",
dest: "skipProxySetup"
}
);
var args = argParser.parseArgs();
var config = args.config;
// Check the action
var configExists = fs.existsSync(config);
if (args.genConfig) {
if (configExists) {
// Ask for confirmation
askThenGenerateConfig();
} else {
// Write the configuration file
generateConfig();
}
} else {
if (configExists) {
// The configuration file exists, so read it
const options = parseJSON(config);
// Allow overriding both swagger and output via arguments
if (args.swagger) {
options.swagger = args.swagger;
}
if (args.output) {
options.output = args.output;
}
if (args.timeout) {
options.timeout = args.timeout;
}
if (args.skipProxySetup) {
options.skipProxySetup = args.skipProxySetup;
}
run(options);
} else if (args.swagger) {
// The args variables are the same one as the options
run(args);
} else {
// No configuration file. Show the usage and exit.
argParser.parseArgs(["--help"]);
}
}
/**
* Generates a configuration file in the current directory
*/
function generateConfig() {
var options = {
"$schema": "./node_modules/ng-swagger-gen/ng-swagger-gen-schema.json"
};
options.swagger = args.swagger ? args.swagger : "<swagger file>";
if (args.output) {
options.output = args.output;
}
var properties = schema.properties;
for (var propName in properties) {
var propDef = properties[propName];
if (propDef.default && !options.hasOwnProperty(propName)) {
var value = propDef.default;
if (propDef.type == "boolean") {
value = value === 'true';
} else if (propDef.type == "integer") {
value = parseInt(value, 10);
}
options[propName] = value;
}
}
var json = JSON.stringify(options, null, 2);
fs.writeFileSync(config, json, {encoding: "utf8"});
console.info("Wrote " + config);
}
/**
* Asks if the configuration file should be overridden, then, if so, do it
*/
function askThenGenerateConfig() {
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("The configuration file " + config + " already exists.\n" +
"Do you want to override it? [y/N]", (answer) => {
if (answer == 'y' || answer == 'Y') {
generateConfig();
}
rl.close();
});
}
/**
* Runs the ng-swagger-gen generation
*/
function run(options) {
var ngSwaggerGen = require("./ng-swagger-gen.js");
if (options.output == null) {
options.output = schema.properties.output.default;
}
if (options.customFileSuffix == null) {
options.customFileSuffix = schema.properties.customFileSuffix.default;
}
if (options.timeout == null) {
options.timeout = schema.properties.timeout.default;
}
if (options.skipProxySetup == null) {
options.skipProxySetup = schema.properties.skipProxySetup.default;
}
ngSwaggerGen(options);
}