Skip to content

Commit 411ec25

Browse files
committedAug 26, 2020
control-c doesn't lose all your progress
1 parent 81e586b commit 411ec25

File tree

6 files changed

+42
-18
lines changed

6 files changed

+42
-18
lines changed
 

‎package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@blackmad/api-diff",
3-
"version": "1.0.29",
3+
"version": "1.0.31",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
@@ -28,6 +28,7 @@
2828
},
2929
"homepage": "https://github.com/radarlabs/api-diff#readme",
3030
"dependencies": {
31+
"@types/bluebird": "^3.5.32",
3132
"@types/cli-progress": "^3.8.0",
3233
"axios": "^0.19.2",
3334
"axios-retry": "^3.1.8",
@@ -45,6 +46,7 @@
4546
"stats-lite": "^2.2.0",
4647
"table": "^5.4.6",
4748
"winston": "^3.3.3",
49+
"wtfnode": "^0.8.1",
4850
"yargs": "^15.1.0"
4951
},
5052
"devDependencies": {

‎src/api-diff/api-diff.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22
/* eslint-disable no-console */
33

4-
import * as Bluebird from 'bluebird';
4+
import Bluebird from 'bluebird';
55
import * as jsondiffpatch from 'jsondiffpatch';
66
import { AxiosResponse } from 'axios';
77
import jp from 'jsonpath';
@@ -112,9 +112,15 @@ async function compareQueries({
112112
const oldStatusCodes: Record<number, number> = {};
113113
const newStatusCodes: Record<number, number> = {};
114114

115-
await Bluebird.map(
115+
let isCancelled = false;
116+
117+
const queriesPromise = Bluebird.map(
116118
queries,
117119
async (query: Query) => {
120+
if (isCancelled) {
121+
return;
122+
}
123+
118124
const change = await compareQuery({
119125
oldApiEnv,
120126
newApiEnv,
@@ -142,6 +148,16 @@ async function compareQueries({
142148
},
143149
{ concurrency: argv.concurrency },
144150
);
151+
152+
process.on('SIGINT', () => {
153+
console.error('Caught interrupt signal, stopping early ...');
154+
isCancelled = true;
155+
});
156+
157+
await queriesPromise.catch(() => {
158+
console.error('Aborted diff early ... saving result ...');
159+
});
160+
145161
formatter.finished({
146162
old: {
147163
responseTimes: oldResponseTimes,
@@ -151,7 +167,7 @@ async function compareQueries({
151167
responseTimes: newResponseTimes,
152168
statusCodes: newStatusCodes,
153169
},
154-
});
170+
}).then(() => process.exit(0));
155171
}
156172

157173
/** Main logic - parses command line, creates a formatter, runs queries and

‎src/api-diff/formatters/compare-formatter.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,17 @@ export abstract class CompareFormatter {
9494
}
9595

9696
/** Called when all queries are finished running */
97-
abstract onFinished(finishedStats: FinishedStats): void;
97+
abstract onFinished(finishedStats: FinishedStats): Promise<void>;
9898

9999
/**
100100
* Called when all queries are finished running
101101
*
102102
* @param finishedStats
103+
* @returns {Promise<void>} fulfilled on output finished
103104
*/
104-
finished(finishedStats: FinishedStats): void {
105+
finished(finishedStats: FinishedStats): Promise<void> {
105106
console.error(`DONE. ${this.numQueriesChanged}/${this.numQueriesRun} changed`);
106-
this.onFinished(finishedStats);
107+
return this.onFinished(finishedStats);
107108
}
108109

109110
/**
@@ -119,13 +120,16 @@ export abstract class CompareFormatter {
119120
* Helper to deal with output redirection
120121
*
121122
* @param {string} s string to output
123+
* @returns {Promise<void>} promise on finished
122124
*/
123-
write(s: string): void {
124-
if (this.outputStream) {
125-
this.outputStream.write(s);
126-
} else {
127-
process.stdout.write(s);
128-
}
125+
write(s: string): Promise<void> {
126+
const stream = this.outputStream || process.stdout;
127+
return new Promise((resolve, reject) => {
128+
stream.write(s);
129+
stream.end();
130+
stream.on('finish', () => { resolve(); });
131+
stream.on('error', reject);
132+
});
129133
}
130134

131135
constructor({

‎src/api-diff/formatters/console-formatter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default class ConsoleFormatter extends CompareFormatter {
4545
(jsondiffpatch.console as any).log(change.delta);
4646
}
4747

48-
onFinished(finishedStats: FinishedStats): void {
48+
onFinished(finishedStats: FinishedStats): Promise<void> {
4949
this.writeln(`Elapsed: ${(Date.now() - this.startDate.getTime()) / 1000} seconds`);
5050

5151
this.writeln('');
@@ -78,5 +78,7 @@ export default class ConsoleFormatter extends CompareFormatter {
7878
});
7979

8080
this.writeln(table(statusCodesTable));
81+
82+
return Promise.resolve();
8183
}
8284
}

‎src/api-diff/formatters/html-formatter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { FinishedStats } from './compare-formatter';
55
import JsonFormatter from './json-formatter';
66

77
export default class HtmlFormatter extends JsonFormatter {
8-
onFinished(finishedStats: FinishedStats): void {
8+
onFinished(finishedStats: FinishedStats): Promise<void> {
99
const filePath = path.join(__dirname, 'compare.template');
1010
const html = fs.readFileSync(filePath).toString();
11-
this.write(
11+
return this.write(
1212
html.replace('let json = {};', `let json = ${JSON.stringify(this.finishedDict(finishedStats), null, 2)};`),
1313
);
1414
}

‎src/api-diff/formatters/json-formatter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default class JsonFormatter extends CompareFormatter {
7070
};
7171
}
7272

73-
onFinished(finishedStats: FinishedStats): void {
74-
this.write(JSON.stringify(this.finishedDict(finishedStats), null, 2));
73+
onFinished(finishedStats: FinishedStats): Promise<void> {
74+
return this.write(JSON.stringify(this.finishedDict(finishedStats), null, 2));
7575
}
7676
}

0 commit comments

Comments
 (0)
Please sign in to comment.