Skip to content

Commit 99564bf

Browse files
authored
fix: non-zero value on failure and print errors #92 (#99)
* fix #92 * throw error instead of exit process * cover non-zero exit code cases
1 parent b66e2e8 commit 99564bf

File tree

5 files changed

+102
-40
lines changed

5 files changed

+102
-40
lines changed

build/cli.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ function Cli(args) {
3030
console.log(output);
3131
})["catch"](function (err) {
3232
console.log('An error occured:');
33-
console.log(err);
33+
console.error(err);
34+
process.exit(-1);
3435
});
3536
}
3637

build/swagger-inline.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function swaggerInline(globPatterns, providedOptions) {
8585
});
8686
var endpoints = [];
8787
var schemas = [];
88-
successfulFiles.forEach(function (fileInfo) {
88+
return Promise.all(successfulFiles.map(function (fileInfo) {
8989
try {
9090
var newEndpoints = Extractor.extractEndpointsFromCode(fileInfo.fileData, {
9191
filename: fileInfo.fileName,
@@ -104,15 +104,19 @@ function swaggerInline(globPatterns, providedOptions) {
104104
});
105105

106106
schemas = _.concat(schemas, scheme);
107+
return Promise.resolve();
107108
} catch (e) {
108-
throw new Error(e.toString(), fileInfo.fileName);
109+
return Promise.reject(new Error(e.toString(), fileInfo.fileName));
109110
}
111+
})).then(function () {
112+
log("".concat(endpoints.length, " definitions found..."));
113+
log("".concat(schemas.length, " schemas found..."));
114+
var baseObjWithEndpoints = mergeEndpointsWithBase(baseObj, endpoints);
115+
var swagger = mergeSchemasWithBase(baseObjWithEndpoints, schemas);
116+
return outputResult(swagger, options);
117+
})["catch"](function (e) {
118+
return Promise.reject(e);
110119
});
111-
log("".concat(endpoints.length, " definitions found..."));
112-
log("".concat(schemas.length, " schemas found..."));
113-
var baseObjWithEndpoints = mergeEndpointsWithBase(baseObj, endpoints);
114-
var swagger = mergeSchemasWithBase(baseObjWithEndpoints, schemas);
115-
return outputResult(swagger, options);
116120
});
117121
});
118122
});

src/swagger-inline.js

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -74,38 +74,45 @@ function swaggerInline(globPatterns, providedOptions) {
7474
let endpoints = [];
7575
let schemas = [];
7676

77-
successfulFiles.forEach(fileInfo => {
78-
try {
79-
let newEndpoints = Extractor.extractEndpointsFromCode(fileInfo.fileData, {
80-
filename: fileInfo.fileName,
81-
scope: options.getScope(),
82-
});
83-
84-
newEndpoints = Loader.addResponse(newEndpoints);
85-
86-
newEndpoints = Loader.expandParams(newEndpoints, swaggerVersion);
87-
endpoints = _.concat(endpoints, newEndpoints);
88-
89-
const scheme = Extractor.extractSchemasFromCode(fileInfo.fileData, {
90-
filename: fileInfo.fileName,
91-
scope: options.getScope(),
92-
});
93-
_.remove(scheme, s => {
94-
return _.isEmpty(s);
95-
});
96-
schemas = _.concat(schemas, scheme);
97-
} catch (e) {
98-
throw new Error(e.toString(), fileInfo.fileName);
99-
}
100-
});
101-
102-
log(`${endpoints.length} definitions found...`);
103-
log(`${schemas.length} schemas found...`);
104-
105-
const baseObjWithEndpoints = mergeEndpointsWithBase(baseObj, endpoints);
106-
const swagger = mergeSchemasWithBase(baseObjWithEndpoints, schemas);
107-
108-
return outputResult(swagger, options);
77+
return Promise.all(
78+
successfulFiles.map(fileInfo => {
79+
try {
80+
let newEndpoints = Extractor.extractEndpointsFromCode(fileInfo.fileData, {
81+
filename: fileInfo.fileName,
82+
scope: options.getScope(),
83+
});
84+
85+
newEndpoints = Loader.addResponse(newEndpoints);
86+
87+
newEndpoints = Loader.expandParams(newEndpoints, swaggerVersion);
88+
endpoints = _.concat(endpoints, newEndpoints);
89+
90+
const scheme = Extractor.extractSchemasFromCode(fileInfo.fileData, {
91+
filename: fileInfo.fileName,
92+
scope: options.getScope(),
93+
});
94+
_.remove(scheme, s => {
95+
return _.isEmpty(s);
96+
});
97+
schemas = _.concat(schemas, scheme);
98+
return Promise.resolve();
99+
} catch (e) {
100+
return Promise.reject(new Error(e.toString(), fileInfo.fileName));
101+
}
102+
})
103+
)
104+
.then(() => {
105+
log(`${endpoints.length} definitions found...`);
106+
log(`${schemas.length} schemas found...`);
107+
108+
const baseObjWithEndpoints = mergeEndpointsWithBase(baseObj, endpoints);
109+
const swagger = mergeSchemasWithBase(baseObjWithEndpoints, schemas);
110+
111+
return outputResult(swagger, options);
112+
})
113+
.catch(e => {
114+
return Promise.reject(e);
115+
});
109116
});
110117
});
111118
});

tests/cli.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
11
const cli = require('../src/cli');
2+
const path = require('path');
3+
const exec = require('child_process').exec;
4+
5+
function runCommand(cmd, cwd) {
6+
return new Promise(resolve => {
7+
exec(cmd, { cwd }, (error, stdout, stderr) => {
8+
resolve({
9+
code: error && error.code ? error.code : 0,
10+
error,
11+
stdout,
12+
stderr,
13+
});
14+
});
15+
});
16+
}
217

318
describe('Cli', () => {
419
it('is a function', () => {
520
expect(typeof cli).toBe('function');
621
});
22+
23+
it('should exit process with non-zero code', () => {
24+
const workDir = path.resolve(__dirname, '../');
25+
const cmd = `node src/index tests/fixtures/code/swagger-api-with-error.js --base tests/fixtures/project/swaggerBase.json`;
26+
return runCommand(cmd, workDir).then(result => {
27+
expect(result.code).not.toBe(0);
28+
});
29+
});
30+
31+
it('should exit process with zero code', () => {
32+
const workDir = path.resolve(__dirname, '../');
33+
const cmd = `node src/index tests/fixtures/code/swagger-api.js --base tests/fixtures/project/swaggerBase.json`;
34+
return runCommand(cmd, workDir).then(result => {
35+
expect(result.code).toBe(0);
36+
});
37+
});
738
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
/**
3+
* @api [post] /pets
4+
* requestBody:
5+
* required: true
6+
* content:
7+
* application/json:
8+
* schema:
9+
* type: object
10+
* required:
11+
* - name
12+
* properties:
13+
* name:
14+
* type: string
15+
* properties there should be an error
16+
*/
17+
router.post('/pets', () => {
18+
19+
});

0 commit comments

Comments
 (0)