concurrently 6.0.2 → 6.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -10
- package/bin/concurrently.js +4 -2
- package/bin/concurrently.spec.js +365 -0
- package/bin/epilogue.txt +1 -1
- package/index.js +3 -2
- package/package.json +2 -3
- package/src/command-parser/expand-npm-shortcut.spec.js +36 -0
- package/src/command-parser/expand-npm-wildcard.js +11 -2
- package/src/command-parser/expand-npm-wildcard.spec.js +58 -0
- package/src/command-parser/strip-quotes.spec.js +20 -0
- package/src/command.js +3 -0
- package/src/command.spec.js +183 -0
- package/src/completion-listener.js +1 -1
- package/src/completion-listener.spec.js +88 -0
- package/src/concurrently.js +15 -4
- package/src/concurrently.spec.js +186 -0
- package/src/defaults.js +1 -1
- package/src/flow-control/base-handler.js +16 -0
- package/src/flow-control/input-handler.js +16 -5
- package/src/flow-control/input-handler.spec.js +113 -0
- package/src/flow-control/kill-on-signal.js +17 -12
- package/src/flow-control/kill-on-signal.spec.js +79 -0
- package/src/flow-control/kill-others.js +7 -4
- package/src/flow-control/kill-others.spec.js +66 -0
- package/src/flow-control/log-error.js +3 -5
- package/src/flow-control/log-error.spec.js +40 -0
- package/src/flow-control/log-exit.js +3 -5
- package/src/flow-control/log-exit.spec.js +36 -0
- package/src/flow-control/log-output.js +3 -5
- package/src/flow-control/log-output.spec.js +41 -0
- package/src/flow-control/restart-process.js +19 -14
- package/src/flow-control/restart-process.spec.js +131 -0
- package/src/get-spawn-opts.spec.js +30 -0
- package/src/logger.js +4 -3
- package/src/logger.spec.js +195 -0
- package/src/flow-control/fixtures/fake-command.js +0 -16
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
const { map } = require('rxjs/operators');
|
|
2
2
|
|
|
3
|
+
const BaseHandler = require('./base-handler');
|
|
3
4
|
|
|
4
|
-
module.exports = class KillOnSignal {
|
|
5
|
+
module.exports = class KillOnSignal extends BaseHandler {
|
|
5
6
|
constructor({ process }) {
|
|
7
|
+
super();
|
|
8
|
+
|
|
6
9
|
this.process = process;
|
|
7
10
|
}
|
|
8
11
|
|
|
@@ -15,16 +18,18 @@ module.exports = class KillOnSignal {
|
|
|
15
18
|
});
|
|
16
19
|
});
|
|
17
20
|
|
|
18
|
-
return
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
return {
|
|
22
|
+
commands: commands.map(command => {
|
|
23
|
+
const closeStream = command.close.pipe(map(exitInfo => {
|
|
24
|
+
const exitCode = caughtSignal === 'SIGINT' ? 0 : exitInfo.exitCode;
|
|
25
|
+
return Object.assign({}, exitInfo, { exitCode });
|
|
26
|
+
}));
|
|
27
|
+
return new Proxy(command, {
|
|
28
|
+
get(target, prop) {
|
|
29
|
+
return prop === 'close' ? closeStream : target[prop];
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
})
|
|
33
|
+
};
|
|
29
34
|
}
|
|
30
35
|
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const EventEmitter = require('events');
|
|
2
|
+
|
|
3
|
+
const createFakeCommand = require('./fixtures/fake-command');
|
|
4
|
+
const KillOnSignal = require('./kill-on-signal');
|
|
5
|
+
|
|
6
|
+
let commands, controller, process;
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
process = new EventEmitter();
|
|
9
|
+
commands = [
|
|
10
|
+
createFakeCommand(),
|
|
11
|
+
createFakeCommand(),
|
|
12
|
+
];
|
|
13
|
+
controller = new KillOnSignal({ process });
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('returns commands that keep non-close streams from original commands', () => {
|
|
17
|
+
const { commands: newCommands } = controller.handle(commands);
|
|
18
|
+
newCommands.forEach((newCommand, i) => {
|
|
19
|
+
expect(newCommand.close).not.toBe(commands[i].close);
|
|
20
|
+
expect(newCommand.error).toBe(commands[i].error);
|
|
21
|
+
expect(newCommand.stdout).toBe(commands[i].stdout);
|
|
22
|
+
expect(newCommand.stderr).toBe(commands[i].stderr);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('returns commands that map SIGINT to exit code 0', () => {
|
|
27
|
+
const { commands: newCommands } = controller.handle(commands);
|
|
28
|
+
expect(newCommands).not.toBe(commands);
|
|
29
|
+
expect(newCommands).toHaveLength(commands.length);
|
|
30
|
+
|
|
31
|
+
const callback = jest.fn();
|
|
32
|
+
newCommands[0].close.subscribe(callback);
|
|
33
|
+
process.emit('SIGINT');
|
|
34
|
+
|
|
35
|
+
// A fake command's .kill() call won't trigger a close event automatically...
|
|
36
|
+
commands[0].close.next({ exitCode: 1 });
|
|
37
|
+
|
|
38
|
+
expect(callback).not.toHaveBeenCalledWith({ exitCode: 'SIGINT' });
|
|
39
|
+
expect(callback).toHaveBeenCalledWith({ exitCode: 0 });
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('returns commands that keep non-SIGINT exit codes', () => {
|
|
43
|
+
const { commands: newCommands } = controller.handle(commands);
|
|
44
|
+
expect(newCommands).not.toBe(commands);
|
|
45
|
+
expect(newCommands).toHaveLength(commands.length);
|
|
46
|
+
|
|
47
|
+
const callback = jest.fn();
|
|
48
|
+
newCommands[0].close.subscribe(callback);
|
|
49
|
+
commands[0].close.next({ exitCode: 1 });
|
|
50
|
+
|
|
51
|
+
expect(callback).toHaveBeenCalledWith({ exitCode: 1 });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('kills all commands on SIGINT', () => {
|
|
55
|
+
controller.handle(commands);
|
|
56
|
+
process.emit('SIGINT');
|
|
57
|
+
|
|
58
|
+
expect(process.listenerCount('SIGINT')).toBe(1);
|
|
59
|
+
expect(commands[0].kill).toHaveBeenCalledWith('SIGINT');
|
|
60
|
+
expect(commands[1].kill).toHaveBeenCalledWith('SIGINT');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('kills all commands on SIGTERM', () => {
|
|
64
|
+
controller.handle(commands);
|
|
65
|
+
process.emit('SIGTERM');
|
|
66
|
+
|
|
67
|
+
expect(process.listenerCount('SIGTERM')).toBe(1);
|
|
68
|
+
expect(commands[0].kill).toHaveBeenCalledWith('SIGTERM');
|
|
69
|
+
expect(commands[1].kill).toHaveBeenCalledWith('SIGTERM');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('kills all commands on SIGHUP', () => {
|
|
73
|
+
controller.handle(commands);
|
|
74
|
+
process.emit('SIGHUP');
|
|
75
|
+
|
|
76
|
+
expect(process.listenerCount('SIGHUP')).toBe(1);
|
|
77
|
+
expect(commands[0].kill).toHaveBeenCalledWith('SIGHUP');
|
|
78
|
+
expect(commands[1].kill).toHaveBeenCalledWith('SIGHUP');
|
|
79
|
+
});
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
const _ = require('lodash');
|
|
2
2
|
const { filter, map } = require('rxjs/operators');
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const BaseHandler = require('./base-handler');
|
|
5
|
+
|
|
6
|
+
module.exports = class KillOthers extends BaseHandler {
|
|
5
7
|
constructor({ logger, conditions }) {
|
|
6
|
-
|
|
8
|
+
super({ logger });
|
|
9
|
+
|
|
7
10
|
this.conditions = _.castArray(conditions);
|
|
8
11
|
}
|
|
9
12
|
|
|
@@ -14,7 +17,7 @@ module.exports = class KillOthers {
|
|
|
14
17
|
));
|
|
15
18
|
|
|
16
19
|
if (!conditions.length) {
|
|
17
|
-
return commands;
|
|
20
|
+
return { commands };
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
const closeStates = commands.map(command => command.close.pipe(
|
|
@@ -30,6 +33,6 @@ module.exports = class KillOthers {
|
|
|
30
33
|
}
|
|
31
34
|
}));
|
|
32
35
|
|
|
33
|
-
return commands;
|
|
36
|
+
return { commands };
|
|
34
37
|
}
|
|
35
38
|
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const { createMockInstance } = require('jest-create-mock-instance');
|
|
2
|
+
|
|
3
|
+
const Logger = require('../logger');
|
|
4
|
+
const createFakeCommand = require('./fixtures/fake-command');
|
|
5
|
+
const KillOthers = require('./kill-others');
|
|
6
|
+
|
|
7
|
+
let commands, logger;
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
commands = [
|
|
10
|
+
createFakeCommand(),
|
|
11
|
+
createFakeCommand()
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
logger = createMockInstance(Logger);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const createWithConditions = conditions => new KillOthers({
|
|
18
|
+
logger,
|
|
19
|
+
conditions
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('returns same commands', () => {
|
|
23
|
+
expect(createWithConditions(['foo']).handle(commands)).toMatchObject({ commands });
|
|
24
|
+
expect(createWithConditions(['failure']).handle(commands)).toMatchObject({ commands });
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('does not kill others if condition does not match', () => {
|
|
28
|
+
createWithConditions(['failure']).handle(commands);
|
|
29
|
+
commands[1].killable = true;
|
|
30
|
+
commands[0].close.next({ exitCode: 0 });
|
|
31
|
+
|
|
32
|
+
expect(logger.logGlobalEvent).not.toHaveBeenCalled();
|
|
33
|
+
expect(commands[0].kill).not.toHaveBeenCalled();
|
|
34
|
+
expect(commands[1].kill).not.toHaveBeenCalled();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('kills other killable processes on success', () => {
|
|
38
|
+
createWithConditions(['success']).handle(commands);
|
|
39
|
+
commands[1].killable = true;
|
|
40
|
+
commands[0].close.next({ exitCode: 0 });
|
|
41
|
+
|
|
42
|
+
expect(logger.logGlobalEvent).toHaveBeenCalledTimes(1);
|
|
43
|
+
expect(logger.logGlobalEvent).toHaveBeenCalledWith('Sending SIGTERM to other processes..');
|
|
44
|
+
expect(commands[0].kill).not.toHaveBeenCalled();
|
|
45
|
+
expect(commands[1].kill).toHaveBeenCalled();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('kills other killable processes on failure', () => {
|
|
49
|
+
createWithConditions(['failure']).handle(commands);
|
|
50
|
+
commands[1].killable = true;
|
|
51
|
+
commands[0].close.next({ exitCode: 1 });
|
|
52
|
+
|
|
53
|
+
expect(logger.logGlobalEvent).toHaveBeenCalledTimes(1);
|
|
54
|
+
expect(logger.logGlobalEvent).toHaveBeenCalledWith('Sending SIGTERM to other processes..');
|
|
55
|
+
expect(commands[0].kill).not.toHaveBeenCalled();
|
|
56
|
+
expect(commands[1].kill).toHaveBeenCalled();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('does not try to kill processes already dead', () => {
|
|
60
|
+
createWithConditions(['failure']).handle(commands);
|
|
61
|
+
commands[0].close.next({ exitCode: 1 });
|
|
62
|
+
|
|
63
|
+
expect(logger.logGlobalEvent).not.toHaveBeenCalled();
|
|
64
|
+
expect(commands[0].kill).not.toHaveBeenCalled();
|
|
65
|
+
expect(commands[1].kill).not.toHaveBeenCalled();
|
|
66
|
+
});
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
const { of } = require('rxjs');
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
constructor({ logger }) {
|
|
5
|
-
this.logger = logger;
|
|
6
|
-
}
|
|
3
|
+
const BaseHandler = require('./base-handler');
|
|
7
4
|
|
|
5
|
+
module.exports = class LogExit extends BaseHandler {
|
|
8
6
|
handle(commands) {
|
|
9
7
|
commands.forEach(command => command.error.subscribe(event => {
|
|
10
8
|
this.logger.logCommandEvent(
|
|
@@ -15,6 +13,6 @@ module.exports = class LogExit {
|
|
|
15
13
|
this.logger.logCommandEvent(event.stack || event, command);
|
|
16
14
|
}));
|
|
17
15
|
|
|
18
|
-
return commands;
|
|
16
|
+
return { commands };
|
|
19
17
|
}
|
|
20
18
|
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const { createMockInstance } = require('jest-create-mock-instance');
|
|
2
|
+
const Logger = require('../logger');
|
|
3
|
+
const LogError = require('./log-error');
|
|
4
|
+
const createFakeCommand = require('./fixtures/fake-command');
|
|
5
|
+
|
|
6
|
+
let controller, logger, commands;
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
commands = [
|
|
9
|
+
createFakeCommand(),
|
|
10
|
+
createFakeCommand(),
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
logger = createMockInstance(Logger);
|
|
14
|
+
controller = new LogError({ logger });
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('returns same commands', () => {
|
|
18
|
+
expect(controller.handle(commands)).toMatchObject({ commands });
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('logs the error event of each command', () => {
|
|
22
|
+
controller.handle(commands);
|
|
23
|
+
commands[0].error.next('error from command 0');
|
|
24
|
+
|
|
25
|
+
const error = new Error('some error message');
|
|
26
|
+
commands[1].error.next(error);
|
|
27
|
+
|
|
28
|
+
expect(logger.logCommandEvent).toHaveBeenCalledTimes(4);
|
|
29
|
+
expect(logger.logCommandEvent).toHaveBeenCalledWith(
|
|
30
|
+
`Error occurred when executing command: ${commands[0].command}`,
|
|
31
|
+
commands[0]
|
|
32
|
+
);
|
|
33
|
+
expect(logger.logCommandEvent).toHaveBeenCalledWith('error from command 0', commands[0]);
|
|
34
|
+
|
|
35
|
+
expect(logger.logCommandEvent).toHaveBeenCalledWith(
|
|
36
|
+
`Error occurred when executing command: ${commands[1].command}`,
|
|
37
|
+
commands[1]
|
|
38
|
+
);
|
|
39
|
+
expect(logger.logCommandEvent).toHaveBeenCalledWith(error.stack, commands[1]);
|
|
40
|
+
});
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
constructor({ logger }) {
|
|
3
|
-
this.logger = logger;
|
|
4
|
-
}
|
|
1
|
+
const BaseHandler = require('./base-handler');
|
|
5
2
|
|
|
3
|
+
module.exports = class LogExit extends BaseHandler {
|
|
6
4
|
handle(commands) {
|
|
7
5
|
commands.forEach(command => command.close.subscribe(({ exitCode }) => {
|
|
8
6
|
this.logger.logCommandEvent(`${command.command} exited with code ${exitCode}`, command);
|
|
9
7
|
}));
|
|
10
8
|
|
|
11
|
-
return commands;
|
|
9
|
+
return { commands };
|
|
12
10
|
}
|
|
13
11
|
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const { createMockInstance } = require('jest-create-mock-instance');
|
|
2
|
+
const Logger = require('../logger');
|
|
3
|
+
const LogExit = require('./log-exit');
|
|
4
|
+
const createFakeCommand = require('./fixtures/fake-command');
|
|
5
|
+
|
|
6
|
+
let controller, logger, commands;
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
commands = [
|
|
9
|
+
createFakeCommand(),
|
|
10
|
+
createFakeCommand(),
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
logger = createMockInstance(Logger);
|
|
14
|
+
controller = new LogExit({ logger });
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('returns same commands', () => {
|
|
18
|
+
expect(controller.handle(commands)).toMatchObject({ commands });
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('logs the close event of each command', () => {
|
|
22
|
+
controller.handle(commands);
|
|
23
|
+
|
|
24
|
+
commands[0].close.next({ exitCode: 0 });
|
|
25
|
+
commands[1].close.next({ exitCode: 'SIGTERM' });
|
|
26
|
+
|
|
27
|
+
expect(logger.logCommandEvent).toHaveBeenCalledTimes(2);
|
|
28
|
+
expect(logger.logCommandEvent).toHaveBeenCalledWith(
|
|
29
|
+
`${commands[0].command} exited with code 0`,
|
|
30
|
+
commands[0]
|
|
31
|
+
);
|
|
32
|
+
expect(logger.logCommandEvent).toHaveBeenCalledWith(
|
|
33
|
+
`${commands[1].command} exited with code SIGTERM`,
|
|
34
|
+
commands[1]
|
|
35
|
+
);
|
|
36
|
+
});
|
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
constructor({ logger }) {
|
|
3
|
-
this.logger = logger;
|
|
4
|
-
}
|
|
1
|
+
const BaseHandler = require('./base-handler');
|
|
5
2
|
|
|
3
|
+
module.exports = class LogOutput extends BaseHandler {
|
|
6
4
|
handle(commands) {
|
|
7
5
|
commands.forEach(command => {
|
|
8
6
|
command.stdout.subscribe(text => this.logger.logCommandText(text.toString(), command));
|
|
9
7
|
command.stderr.subscribe(text => this.logger.logCommandText(text.toString(), command));
|
|
10
8
|
});
|
|
11
9
|
|
|
12
|
-
return commands;
|
|
10
|
+
return { commands };
|
|
13
11
|
}
|
|
14
12
|
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const { createMockInstance } = require('jest-create-mock-instance');
|
|
2
|
+
const Logger = require('../logger');
|
|
3
|
+
const LogOutput = require('./log-output');
|
|
4
|
+
const createFakeCommand = require('./fixtures/fake-command');
|
|
5
|
+
|
|
6
|
+
let controller, logger, commands;
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
commands = [
|
|
9
|
+
createFakeCommand(),
|
|
10
|
+
createFakeCommand(),
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
logger = createMockInstance(Logger);
|
|
14
|
+
controller = new LogOutput({ logger });
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('returns same commands', () => {
|
|
18
|
+
expect(controller.handle(commands)).toMatchObject({ commands });
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('logs the stdout of each command', () => {
|
|
22
|
+
controller.handle(commands);
|
|
23
|
+
|
|
24
|
+
commands[0].stdout.next(Buffer.from('foo'));
|
|
25
|
+
commands[1].stdout.next(Buffer.from('bar'));
|
|
26
|
+
|
|
27
|
+
expect(logger.logCommandText).toHaveBeenCalledTimes(2);
|
|
28
|
+
expect(logger.logCommandText).toHaveBeenCalledWith('foo', commands[0]);
|
|
29
|
+
expect(logger.logCommandText).toHaveBeenCalledWith('bar', commands[1]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('logs the stderr of each command', () => {
|
|
33
|
+
controller.handle(commands);
|
|
34
|
+
|
|
35
|
+
commands[0].stderr.next(Buffer.from('foo'));
|
|
36
|
+
commands[1].stderr.next(Buffer.from('bar'));
|
|
37
|
+
|
|
38
|
+
expect(logger.logCommandText).toHaveBeenCalledTimes(2);
|
|
39
|
+
expect(logger.logCommandText).toHaveBeenCalledWith('foo', commands[0]);
|
|
40
|
+
expect(logger.logCommandText).toHaveBeenCalledWith('bar', commands[1]);
|
|
41
|
+
});
|
|
@@ -2,18 +2,21 @@ const Rx = require('rxjs');
|
|
|
2
2
|
const { defaultIfEmpty, delay, filter, mapTo, skip, take, takeWhile } = require('rxjs/operators');
|
|
3
3
|
|
|
4
4
|
const defaults = require('../defaults');
|
|
5
|
+
const BaseHandler = require('./base-handler');
|
|
5
6
|
|
|
6
|
-
module.exports = class RestartProcess {
|
|
7
|
+
module.exports = class RestartProcess extends BaseHandler {
|
|
7
8
|
constructor({ delay, tries, logger, scheduler }) {
|
|
9
|
+
super({ logger });
|
|
10
|
+
|
|
8
11
|
this.delay = +delay || defaults.restartDelay;
|
|
9
12
|
this.tries = +tries || defaults.restartTries;
|
|
10
|
-
this.
|
|
13
|
+
this.tries = this.tries < 0 ? Infinity : this.tries;
|
|
11
14
|
this.scheduler = scheduler;
|
|
12
15
|
}
|
|
13
16
|
|
|
14
17
|
handle(commands) {
|
|
15
18
|
if (this.tries === 0) {
|
|
16
|
-
return commands;
|
|
19
|
+
return { commands };
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
commands.map(command => command.close.pipe(
|
|
@@ -35,17 +38,19 @@ module.exports = class RestartProcess {
|
|
|
35
38
|
}
|
|
36
39
|
}));
|
|
37
40
|
|
|
38
|
-
return
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
return {
|
|
42
|
+
commands: commands.map(command => {
|
|
43
|
+
const closeStream = command.close.pipe(filter(({ exitCode }, emission) => {
|
|
44
|
+
// We let all success codes pass, and failures only after restarting won't happen again
|
|
45
|
+
return exitCode === 0 || emission >= this.tries;
|
|
46
|
+
}));
|
|
43
47
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
return new Proxy(command, {
|
|
49
|
+
get(target, prop) {
|
|
50
|
+
return prop === 'close' ? closeStream : target[prop];
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
})
|
|
54
|
+
};
|
|
50
55
|
}
|
|
51
56
|
};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
const { createMockInstance } = require('jest-create-mock-instance');
|
|
2
|
+
const { TestScheduler } = require('rxjs/testing');
|
|
3
|
+
|
|
4
|
+
const Logger = require('../logger');
|
|
5
|
+
const createFakeCommand = require('./fixtures/fake-command');
|
|
6
|
+
const RestartProcess = require('./restart-process');
|
|
7
|
+
|
|
8
|
+
let commands, controller, logger, scheduler;
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
commands = [
|
|
11
|
+
createFakeCommand(),
|
|
12
|
+
createFakeCommand()
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
logger = createMockInstance(Logger);
|
|
16
|
+
scheduler = new TestScheduler();
|
|
17
|
+
controller = new RestartProcess({
|
|
18
|
+
logger,
|
|
19
|
+
scheduler,
|
|
20
|
+
delay: 100,
|
|
21
|
+
tries: 2
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('does not restart processes that complete with success', () => {
|
|
26
|
+
controller.handle(commands);
|
|
27
|
+
|
|
28
|
+
commands[0].close.next({ exitCode: 0 });
|
|
29
|
+
commands[1].close.next({ exitCode: 0 });
|
|
30
|
+
|
|
31
|
+
scheduler.flush();
|
|
32
|
+
|
|
33
|
+
expect(commands[0].start).toHaveBeenCalledTimes(0);
|
|
34
|
+
expect(commands[1].start).toHaveBeenCalledTimes(0);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('restarts processes that fail after delay has passed', () => {
|
|
38
|
+
controller.handle(commands);
|
|
39
|
+
|
|
40
|
+
commands[0].close.next({ exitCode: 1 });
|
|
41
|
+
commands[1].close.next({ exitCode: 0 });
|
|
42
|
+
|
|
43
|
+
scheduler.flush();
|
|
44
|
+
|
|
45
|
+
expect(logger.logCommandEvent).toHaveBeenCalledTimes(1);
|
|
46
|
+
expect(logger.logCommandEvent).toHaveBeenCalledWith(
|
|
47
|
+
`${commands[0].command} restarted`,
|
|
48
|
+
commands[0]
|
|
49
|
+
);
|
|
50
|
+
expect(commands[0].start).toHaveBeenCalledTimes(1);
|
|
51
|
+
expect(commands[1].start).not.toHaveBeenCalled();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('restarts processes up to tries', () => {
|
|
55
|
+
controller.handle(commands);
|
|
56
|
+
|
|
57
|
+
commands[0].close.next({ exitCode: 1 });
|
|
58
|
+
commands[0].close.next({ exitCode: 'SIGTERM' });
|
|
59
|
+
commands[0].close.next({ exitCode: 'SIGTERM' });
|
|
60
|
+
commands[1].close.next({ exitCode: 0 });
|
|
61
|
+
|
|
62
|
+
scheduler.flush();
|
|
63
|
+
|
|
64
|
+
expect(logger.logCommandEvent).toHaveBeenCalledTimes(2);
|
|
65
|
+
expect(logger.logCommandEvent).toHaveBeenCalledWith(
|
|
66
|
+
`${commands[0].command} restarted`,
|
|
67
|
+
commands[0]
|
|
68
|
+
);
|
|
69
|
+
expect(commands[0].start).toHaveBeenCalledTimes(2);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it.todo('restart processes forever, if tries is negative');
|
|
73
|
+
|
|
74
|
+
it('restarts processes until they succeed', () => {
|
|
75
|
+
controller.handle(commands);
|
|
76
|
+
|
|
77
|
+
commands[0].close.next({ exitCode: 1 });
|
|
78
|
+
commands[0].close.next({ exitCode: 0 });
|
|
79
|
+
commands[1].close.next({ exitCode: 0 });
|
|
80
|
+
|
|
81
|
+
scheduler.flush();
|
|
82
|
+
|
|
83
|
+
expect(logger.logCommandEvent).toHaveBeenCalledTimes(1);
|
|
84
|
+
expect(logger.logCommandEvent).toHaveBeenCalledWith(
|
|
85
|
+
`${commands[0].command} restarted`,
|
|
86
|
+
commands[0]
|
|
87
|
+
);
|
|
88
|
+
expect(commands[0].start).toHaveBeenCalledTimes(1);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('returned commands', () => {
|
|
92
|
+
it('are the same if 0 tries are to be attempted', () => {
|
|
93
|
+
controller = new RestartProcess({ logger, scheduler });
|
|
94
|
+
expect(controller.handle(commands)).toMatchObject({ commands });
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('are not the same, but with same length if 1+ tries are to be attempted', () => {
|
|
98
|
+
const { commands: newCommands } = controller.handle(commands);
|
|
99
|
+
expect(newCommands).not.toBe(commands);
|
|
100
|
+
expect(newCommands).toHaveLength(commands.length);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('skip close events followed by restarts', () => {
|
|
104
|
+
const { commands: newCommands } = controller.handle(commands);
|
|
105
|
+
|
|
106
|
+
const callback = jest.fn();
|
|
107
|
+
newCommands[0].close.subscribe(callback);
|
|
108
|
+
newCommands[1].close.subscribe(callback);
|
|
109
|
+
|
|
110
|
+
commands[0].close.next({ exitCode: 1 });
|
|
111
|
+
commands[0].close.next({ exitCode: 1 });
|
|
112
|
+
commands[0].close.next({ exitCode: 1 });
|
|
113
|
+
commands[1].close.next({ exitCode: 1 });
|
|
114
|
+
commands[1].close.next({ exitCode: 0 });
|
|
115
|
+
|
|
116
|
+
scheduler.flush();
|
|
117
|
+
|
|
118
|
+
// 1 failure from commands[0], 1 success from commands[1]
|
|
119
|
+
expect(callback).toHaveBeenCalledTimes(2);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('keep non-close streams from original commands', () => {
|
|
123
|
+
const { commands: newCommands } = controller.handle(commands);
|
|
124
|
+
newCommands.forEach((newCommand, i) => {
|
|
125
|
+
expect(newCommand.close).not.toBe(commands[i].close);
|
|
126
|
+
expect(newCommand.error).toBe(commands[i].error);
|
|
127
|
+
expect(newCommand.stdout).toBe(commands[i].stdout);
|
|
128
|
+
expect(newCommand.stderr).toBe(commands[i].stderr);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const getSpawnOpts = require('./get-spawn-opts');
|
|
2
|
+
|
|
3
|
+
it('sets detached mode to false for Windows platform', () => {
|
|
4
|
+
expect(getSpawnOpts({ process: { platform: 'win32', cwd: jest.fn() } }).detached).toBe(false);
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
it('sets stdio to inherit when raw', () => {
|
|
8
|
+
expect(getSpawnOpts({ raw: true }).stdio).toBe('inherit');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('merges FORCE_COLOR into env vars if color supported', () => {
|
|
12
|
+
const process = { env: { foo: 'bar' }, cwd: jest.fn() };
|
|
13
|
+
expect(getSpawnOpts({ process, colorSupport: false }).env).toEqual(process.env);
|
|
14
|
+
expect(getSpawnOpts({ process, colorSupport: { level: 1 } }).env).toEqual({
|
|
15
|
+
FORCE_COLOR: 1,
|
|
16
|
+
foo: 'bar'
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('sets default cwd to process.cwd()', () => {
|
|
21
|
+
const process = { cwd: jest.fn().mockReturnValue('process-cwd') };
|
|
22
|
+
expect(getSpawnOpts({
|
|
23
|
+
process,
|
|
24
|
+
}).cwd).toBe('process-cwd');
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('overrides default cwd', () => {
|
|
28
|
+
const cwd = 'foobar';
|
|
29
|
+
expect(getSpawnOpts({ cwd }).cwd).toBe(cwd);
|
|
30
|
+
});
|
package/src/logger.js
CHANGED
|
@@ -61,7 +61,8 @@ module.exports = class Logger {
|
|
|
61
61
|
if (command.prefixColor && command.prefixColor.startsWith('#')) {
|
|
62
62
|
color = chalk.hex(command.prefixColor);
|
|
63
63
|
} else {
|
|
64
|
-
|
|
64
|
+
const defaultColor = _.get(chalk, defaults.prefixColors, chalk.reset);
|
|
65
|
+
color = _.get(chalk, command.prefixColor, defaultColor);
|
|
65
66
|
}
|
|
66
67
|
return color(text);
|
|
67
68
|
}
|
|
@@ -71,7 +72,7 @@ module.exports = class Logger {
|
|
|
71
72
|
return;
|
|
72
73
|
}
|
|
73
74
|
|
|
74
|
-
this.logCommandText(chalk.
|
|
75
|
+
this.logCommandText(chalk.reset(text) + '\n', command);
|
|
75
76
|
}
|
|
76
77
|
|
|
77
78
|
logCommandText(text, command) {
|
|
@@ -84,7 +85,7 @@ module.exports = class Logger {
|
|
|
84
85
|
return;
|
|
85
86
|
}
|
|
86
87
|
|
|
87
|
-
this.log(chalk.
|
|
88
|
+
this.log(chalk.reset('-->') + ' ', chalk.reset(text) + '\n');
|
|
88
89
|
}
|
|
89
90
|
|
|
90
91
|
log(prefix, text) {
|