Skip to content

Commit 3487f83

Browse files
Update src/flow-control/log-timings.js
Co-authored-by: Gustavo Henke <[email protected]>
1 parent 2bc5bcc commit 3487f83

File tree

8 files changed

+51
-43
lines changed

8 files changed

+51
-43
lines changed

bin/concurrently.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ const args = yargs
6262
default: defaults.hide,
6363
type: 'string'
6464
},
65-
'show-timings': {
65+
'timings': {
6666
describe: 'Show timing information for all processes',
6767
type: 'boolean',
68-
default: defaults.showTimings
68+
default: defaults.timings
6969
},
7070

7171
// Kill others
@@ -147,7 +147,7 @@ const args = yargs
147147
'Can be either the index or the name of the process.'
148148
}
149149
})
150-
.group(['m', 'n', 'name-separator', 'raw', 's', 'no-color', 'hide', 'show-timings'], 'General')
150+
.group(['m', 'n', 'name-separator', 'raw', 's', 'no-color', 'hide', 'timings'], 'General')
151151
.group(['p', 'c', 'l', 't'], 'Prefix styling')
152152
.group(['i', 'default-input-target'], 'Input handling')
153153
.group(['k', 'kill-others-on-fail'], 'Killing other processes')
@@ -177,7 +177,7 @@ concurrently(args._.map((command, index) => ({
177177
restartTries: args.restartTries,
178178
successCondition: args.success,
179179
timestampFormat: args.timestampFormat,
180-
showTimings: args.showTimings
180+
timings: args.timings
181181
}).then(
182182
() => process.exit(0),
183183
() => process.exit(1)

bin/concurrently.spec.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,13 @@ describe('--handle-input', () => {
383383
});
384384
});
385385

386-
describe('--show-timings', () => {
387-
const dateRegex = '\\d+\/\\d+\/\\d+';
388-
const timeRegex = '\\d+:\\d+:\\d+(AM|PM|\\s)*';
386+
describe('--timings', () => {
387+
const defaultTimestampFormatRegex = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}/;
389388
const processStartedMessageRegex = (index, command) => {
390-
return new RegExp( `^\\[${ index }] ${ command } started at ${ dateRegex }, ${ timeRegex }$` );
389+
return new RegExp( `^\\[${ index }] ${ command } started at ${ defaultTimestampFormatRegex.source }$` );
391390
};
392391
const processStoppedMessageRegex = (index, command) => {
393-
return new RegExp( `^\\[${ index }] ${ command } stopped at ${ dateRegex }, ${ timeRegex } after (\\d|,)+ms$` );
392+
return new RegExp( `^\\[${ index }] ${ command } stopped at ${ defaultTimestampFormatRegex.source } after (\\d|,)+ms$` );
394393
};
395394
const expectLinesForProcessStartAndStop = (lines, index, command) => {
396395
const escapedCommand = _.escapeRegExp(command);
@@ -401,14 +400,14 @@ describe('--show-timings', () => {
401400
const expectLinesForTimingsTable = (lines) => {
402401
const tableTopBorderRegex = /[]+/g;
403402
expect(lines).toContainEqual(expect.stringMatching(tableTopBorderRegex));
404-
const tableHeaderRowRegex = /(\W+(\(index\)|call-index|name|duration|exit-code|killed|command)\W+){7}/g;
403+
const tableHeaderRowRegex = /(\W+(\(index\)|name|duration|exit code|killed|command)\W+){6}/g;
405404
expect(lines).toContainEqual(expect.stringMatching(tableHeaderRowRegex));
406405
const tableBottomBorderRegex = /[]+/g;
407406
expect(lines).toContainEqual(expect.stringMatching(tableBottomBorderRegex));
408407
};
409408

410409
it('shows timings on success', done => {
411-
const child = run('--show-timings "sleep 0.5" "exit 0"');
410+
const child = run('--timings "sleep 0.5" "exit 0"');
412411
child.log.pipe(buffer(child.close)).subscribe(lines => {
413412
expectLinesForProcessStartAndStop(lines, 0, 'sleep 0.5');
414413
expectLinesForProcessStartAndStop(lines, 1, 'exit 0');
@@ -418,7 +417,7 @@ describe('--show-timings', () => {
418417
});
419418

420419
it('shows timings on failure', done => {
421-
const child = run('--show-timings "sleep 0.75" "exit 1"');
420+
const child = run('--timings "sleep 0.75" "exit 1"');
422421
child.log.pipe(buffer(child.close)).subscribe(lines => {
423422
expectLinesForProcessStartAndStop(lines, 0, 'sleep 0.75');
424423
expectLinesForProcessStartAndStop(lines, 1, 'exit 1');

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ module.exports = exports = (commands, options = {}) => {
4646
conditions: options.killOthers
4747
}),
4848
new LogTimings({
49-
logger: options.showTimings ? logger: null
49+
logger: options.timings ? logger: null,
50+
timestampFormat: options.timestampFormat,
5051
})
5152
],
5253
prefixColors: options.prefixColors || [],
53-
showTimings: options.showTimings
54+
timings: options.timings
5455
});
5556
};
5657

src/completion-listener.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ const Rx = require('rxjs');
22
const { bufferCount, switchMap, take } = require('rxjs/operators');
33

44
module.exports = class CompletionListener {
5-
constructor({ successCondition, scheduler, showTimings}) {
5+
constructor({ successCondition, scheduler }) {
66
this.successCondition = successCondition;
77
this.scheduler = scheduler;
8-
this.showTimings = showTimings;
98
}
109

1110
isSuccess(exitCodes) {

src/concurrently.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const defaults = {
1818
raw: false,
1919
controllers: [],
2020
cwd: undefined,
21-
showTimings: false
21+
timings: false
2222
};
2323

2424
module.exports = (commands, options) => {
@@ -51,7 +51,7 @@ module.exports = (commands, options) => {
5151
prefixColor: lastColor,
5252
killProcess: options.kill,
5353
spawn: options.spawn,
54-
showTimings: options.showTimings,
54+
timings: options.timings,
5555
}, command)
5656
);
5757
})
@@ -77,10 +77,9 @@ module.exports = (commands, options) => {
7777

7878
return new CompletionListener({
7979
successCondition: options.successCondition,
80-
showTimings: options.showTimings
8180
})
8281
.listen(commands)
83-
.finally((...args) => {
82+
.finally(() => {
8483
handleResult.onFinishCallbacks.forEach((onFinish) => onFinish());
8584
});
8685
};

src/defaults.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ module.exports = {
3131
// Current working dir passed as option to spawn command. Default: process.cwd()
3232
cwd: undefined,
3333
// Whether to show timing information for processes in console output
34-
showTimings: false,
34+
timings: false,
3535
};

src/flow-control/log-timings.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
const Rx = require('rxjs');
22
const _ = require('lodash');
3+
const formatDate = require('date-fns/format');
34
const { bufferCount, take } = require('rxjs/operators');
45
const BaseHandler = require('./base-handler');
56

67
module.exports = class LogTimings extends BaseHandler {
8+
constructor({ logger, timestampFormat }) {
9+
super({ logger });
10+
11+
this.timestampFormat = timestampFormat;
12+
}
13+
714
printExitInfoTimingTable(exitInfos) {
815
const exitInfoTable = _(exitInfos)
9-
.map(({ command, timings, index, killed, exitCode }) => {
16+
.sortBy(({timings}) => timings.durationSeconds)
17+
.reverse()
18+
.map(({ command, timings, killed, exitCode }) => {
1019
const readableDurationMs = (timings.endDate - timings.startDate).toLocaleString();
1120
return {
12-
'call-index': index,
1321
name: command.name,
14-
duration: `~${readableDurationMs}ms`,
15-
'exit-code': exitCode,
22+
duration: `${readableDurationMs}ms`,
23+
'exit code': exitCode,
1624
killed,
1725
command: command.command,
1826
};
1927
})
20-
.sortBy('duration')
2128
.value();
2229

2330
console.log('\nTimings:');
@@ -28,16 +35,18 @@ module.exports = class LogTimings extends BaseHandler {
2835
handle(commands) {
2936
if (!this.logger) { return {commands}; }
3037

38+
const controllerInstance = this;
39+
3140
// individual process timings
3241
commands.forEach(command => {
3342
command.timer.subscribe( {
3443
next: ({startDate, endDate}) => {
3544
if (!endDate) {
36-
this.logger.logCommandEvent( `${ command.command } started at ${ startDate.toLocaleString() }`, command );
45+
controllerInstance.logger.logCommandEvent( `${ command.command } started at ${ formatDate(startDate, controllerInstance.timestampFormat) }`, command );
3746
} else {
3847
const durationMs = (endDate.getTime() - startDate.getTime());
3948

40-
this.logger.logCommandEvent( `${ command.command } stopped at ${ endDate.toLocaleString() } after ${durationMs.toLocaleString()}ms`, command );
49+
controllerInstance.logger.logCommandEvent( `${ command.command } stopped at ${ formatDate(endDate, controllerInstance.timestampFormat) } after ${durationMs.toLocaleString()}ms`, command );
4150
}
4251
},
4352
} );

src/flow-control/log-timings.spec.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { createMockInstance } = require('jest-create-mock-instance');
2+
const formatDate = require('date-fns/format');
23
const Logger = require('../logger');
34
const LogTimings = require( './log-timings' );
45
const createFakeCommand = require('./fixtures/fake-command');
@@ -9,17 +10,17 @@ const startDate1 = new Date(startDate0.getTime() + 1000);
910
const endDate1 = new Date(startDate0.getTime() + 3000);
1011
const endDate0 = new Date(startDate0.getTime() + 5000);
1112

12-
const getDurationText = (startDate, endDate) => (endDate.getTime() - startDate.getTime()).toLocaleString();
13+
const timestampFormat = 'yyyy-MM-dd HH:mm:ss.SSS';
14+
const getDurationText = (startDate, endDate) => `${(endDate.getTime() - startDate.getTime()).toLocaleString()}ms`;
1315
const command0DurationTextMs = getDurationText(startDate0, endDate0);
1416
const command1DurationTextMs = getDurationText(startDate1, endDate1);
1517

16-
const exitInfoToTimingInfo = ({ command, timings, index, killed, exitCode }) => {
17-
const readableDurationMs = (timings.endDate - timings.startDate).toLocaleString();
18+
const exitInfoToTimingInfo = ({ command, timings, killed, exitCode }) => {
19+
const readableDurationMs = getDurationText(timings.startDate, timings.endDate);
1820
return {
19-
'call-index': index,
2021
name: command.name,
21-
duration: `~${readableDurationMs}ms`,
22-
'exit-code': exitCode,
22+
duration: readableDurationMs,
23+
'exit code': exitCode,
2324
killed,
2425
command: command.command,
2526
};
@@ -56,7 +57,7 @@ beforeEach(() => {
5657
};
5758

5859
logger = createMockInstance(Logger);
59-
controller = new LogTimings({ logger });
60+
controller = new LogTimings({ logger, timestampFormat });
6061
});
6162

6263
it('returns same commands', () => {
@@ -69,8 +70,8 @@ it('does not log timings and doesn\'t throw if no logger is provided', () => {
6970

7071
commands[0].timer.next({ startDate: startDate0 });
7172
commands[1].timer.next({ startDate: startDate1 });
72-
commands[1].timer.next({startDate: startDate1, endDate: endDate1 });
73-
commands[0].timer.next({startDate: startDate0, endDate: endDate0 });
73+
commands[1].timer.next({ startDate: startDate1, endDate: endDate1 });
74+
commands[0].timer.next({ startDate: startDate0, endDate: endDate0 });
7475

7576
expect(logger.logCommandEvent).toHaveBeenCalledTimes(0);
7677
});
@@ -80,24 +81,24 @@ it('logs the timings at the start and end (ie complete or error) event of each c
8081

8182
commands[0].timer.next({ startDate: startDate0 });
8283
commands[1].timer.next({ startDate: startDate1 });
83-
commands[1].timer.next({startDate: startDate1, endDate: endDate1 });
84-
commands[0].timer.next({startDate: startDate0, endDate: endDate0 });
84+
commands[1].timer.next({ startDate: startDate1, endDate: endDate1 });
85+
commands[0].timer.next({ startDate: startDate0, endDate: endDate0 });
8586

8687
expect(logger.logCommandEvent).toHaveBeenCalledTimes(4);
8788
expect(logger.logCommandEvent).toHaveBeenCalledWith(
88-
`${commands[0].command} started at ${startDate0.toLocaleString()}`,
89+
`${commands[0].command} started at ${formatDate(startDate0, timestampFormat)}`,
8990
commands[0]
9091
);
9192
expect(logger.logCommandEvent).toHaveBeenCalledWith(
92-
`${commands[1].command} started at ${startDate1.toLocaleString()}`,
93+
`${commands[1].command} started at ${formatDate(startDate1, timestampFormat)}`,
9394
commands[1]
9495
);
9596
expect(logger.logCommandEvent).toHaveBeenCalledWith(
96-
`${commands[1].command} stopped at ${endDate1.toLocaleString()} after ${command1DurationTextMs}ms`,
97+
`${commands[1].command} stopped at ${formatDate(endDate1, timestampFormat)} after ${command1DurationTextMs}`,
9798
commands[1]
9899
);
99100
expect(logger.logCommandEvent).toHaveBeenCalledWith(
100-
`${commands[0].command} stopped at ${endDate0.toLocaleString()} after ${command0DurationTextMs}ms`,
101+
`${commands[0].command} stopped at ${formatDate(endDate0, timestampFormat)} after ${command0DurationTextMs}`,
101102
commands[0]
102103
);
103104
});

0 commit comments

Comments
 (0)