Skip to content

Commit 2d090e7

Browse files
authored
fix(reject): fix reject and exit with code 1 if error (#295)
* fix(reject): fix reject and exit with code 1 if error
1 parent 4b24048 commit 2d090e7

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

index.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,20 @@ program
3333
)
3434
.option(
3535
"-r, --responses",
36-
"generate additional information about request responses\n" +
37-
"also add typings for bad responses",
36+
"generate additional information about request responses\n" + "also add typings for bad responses",
3837
false,
3938
)
4039
.option("--union-enums", 'generate all "enum" types as union types (T1 | T2 | TN)', false)
4140
.option("--route-types", "generate type definitions for API routes", false)
4241
.option("--no-client", "do not generate an API class", false)
43-
.option(
44-
"--enum-names-as-values",
45-
"use values in 'x-enumNames' as enum values (not only as keys)",
46-
false,
47-
)
42+
.option("--enum-names-as-values", "use values in 'x-enumNames' as enum values (not only as keys)", false)
4843
.option(
4944
"--extract-request-params",
5045
"extract request params to data contract (Also combine path params and query params into one object)",
5146
false,
5247
)
5348
.option("--extract-request-body", "extract request body type to data contract", false)
54-
.option(
55-
"--modular",
56-
"generate separated files for http client, data contracts, and routes",
57-
false,
58-
)
49+
.option("--modular", "generate separated files for http client, data contracts, and routes", false)
5950
.option("--js", "generate js api module with declaration file", false)
6051
.option(
6152
"--module-name-index <number>",
@@ -72,11 +63,7 @@ program
7263
.option("--default-response <type>", "default type for empty response schema", TS_KEYWORDS.VOID)
7364
.option("--type-prefix <string>", "data contract name prefix", "")
7465
.option("--type-suffix <string>", "data contract name suffix", "")
75-
.option(
76-
"--clean-output",
77-
"clean output folder before generate api. WARNING: May cause data loss",
78-
false,
79-
)
66+
.option("--clean-output", "clean output folder before generate api. WARNING: May cause data loss", false)
8067
.option("--patch", "fix up small errors in the swagger source definition", false);
8168

8269
program.parse(process.argv);
@@ -140,4 +127,9 @@ generateApi({
140127
typePrefix,
141128
typeSuffix,
142129
patch: !!patch,
130+
}).catch((err) => {
131+
// NOTE collect all errors on top level and shows to users in any case
132+
console.error(err);
133+
134+
process.exit(1);
143135
});

src/swagger.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const parseSwaggerFile = (file) => {
1818
};
1919

2020
const getSwaggerFile = (pathToSwagger, urlToSwagger, disableStrictSSL, disableProxy) =>
21-
new Promise((resolve) => {
21+
new Promise((resolve, reject) => {
2222
if (pathIsExist(pathToSwagger)) {
2323
logger.log(`try to get swagger by path "${pathToSwagger}"`);
2424
resolve(getFileContent(pathToSwagger));
@@ -38,7 +38,13 @@ const getSwaggerFile = (pathToSwagger, urlToSwagger, disableStrictSSL, disablePr
3838
axios
3939
.get(urlToSwagger, axiosOptions)
4040
.then((res) => resolve(res.data))
41-
.catch((err) => logger.error(`error while getting swagger by URL ${urlToSwagger}:`, err));
41+
.catch(() => {
42+
const message = `error while getting swagger by URL ${urlToSwagger}`;
43+
44+
logger.error(message);
45+
46+
reject(message);
47+
});
4248
}
4349
});
4450

0 commit comments

Comments
 (0)