This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathswagger-cli.js
201 lines (178 loc) · 4.42 KB
/
swagger-cli.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env node
"use strict";
const yargs = require("yargs");
const chalk = require("chalk");
const api = require("../");
const helpText = require("./help-text.json");
const validTypeOptions = ["json", "yaml"];
(function main () {
let args = parseArgs();
let command = args.command;
let file = args.file;
let options = args.options;
if (options.debug) {
// Enable debug output
process.env.DEBUG = "swagger:*,json-schema-ref-parser";
}
// Check if the output type contains a valid value
if (validTypeOptions.indexOf(options.type) === -1) {
const validValues = validTypeOptions.join(", ");
console.error('Error: type value "' + options.type + '" is invalid. Valid values: ' + validValues);
process.exit(2);
}
if (options.help) {
// Show help text and exit
console.log(getHelpText(command));
process.exit(0);
}
else if (command === "validate" && file) {
// Validate an API
validate(file, options);
}
else if (command === "bundle" && file) {
// Bundle a multi-file API
bundle(file, options);
}
else {
// Invalid args. Show help text and exit with non-zero
console.error("Error: Invalid arguments\n");
console.error(getHelpText(command));
process.exit(1);
}
}());
/**
* Parses the command-line arguments
*
* @returns {object} - The parsed arguments
*/
function parseArgs () {
// Configure the argument parser
yargs
.option("schema", {
type: "boolean",
default: true,
})
.option("spec", {
type: "boolean",
default: true,
})
.option("o", {
alias: "outfile",
type: "string",
normalize: true,
})
.option("r", {
alias: "dereference",
type: "boolean",
})
.option("t", {
alias: "type",
type: "string",
normalize: true,
default: "json",
})
.option("f", {
alias: "format",
type: "number",
default: 2,
})
.option("w", {
alias: "wrap",
type: "number",
default: Infinity,
})
.option("d", {
alias: "debug",
type: "boolean",
})
.option("h", {
alias: "help",
type: "boolean",
});
// Show the version number on "--version" or "-v"
yargs
.version()
.alias("v", "version");
// Disable the default "--help" behavior
yargs.help(false);
// Parse the command-line arguments
let args = yargs.argv;
// Normalize the parsed arguments
let parsed = {
command: args._[0],
file: args._[1],
options: {
schema: args.schema,
spec: args.spec,
outfile: args.outfile,
dereference: args.dereference,
format: args.format || 2,
type: args.type || "json",
wrap: args.wrap || Infinity,
debug: args.debug,
help: args.help,
}
};
if (parsed.options.debug) {
console.log(JSON.stringify(parsed, null, 2));
}
return parsed;
}
/**
* Validates an API definition against the Swagger/OpenAPI schema and spec
*
* @param {string} file - The path of the file to validate
* @param {object} options - Validation options
* @param {boolean} options.schema - Whether to validate against the Swagger/OpenAPI schema
* @param {boolean} options.spec - Whether to validate against the Swagger/OpenAPI specification
*/
async function validate (file, options) {
try {
await api.validate(file, options);
console.log(file, "is valid");
}
catch (error) {
errorHandler(error);
}
}
/**
* Bundles a multi-file API definition
*
* @param {string} file - The path of the file to validate
* @param {object} options - Validation options
*/
async function bundle (file, options) {
try {
let bundled = await api.bundle(file, options);
if (options.outfile) {
console.log("Created %s from %s", options.outfile, file);
}
else {
// Write the bundled API to stdout
console.log(bundled);
}
}
catch (error) {
errorHandler(error);
}
}
/**
* Returns the help text for the specified command
*
* @param {string} [commandName] - The command to show help text for
* @returns {string} - the help text
*/
function getHelpText (commandName) {
let lines = helpText[commandName] || helpText.default;
return lines.join("\n");
}
/**
* Writes error information to stderr and exits with a non-zero code
*
* @param {Error} err
*/
function errorHandler (err) {
let errorMessage = process.env.DEBUG ? err.stack : err.message;
console.error(chalk.red(errorMessage));
process.exit(1);
}