Skip to content

Commit

Permalink
Add option and CLI flag to hide command output (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
firoxer committed Oct 2, 2021
1 parent 08eda4f commit 88b8d19
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 16 deletions.
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,25 @@ Help:
concurrently [options] <command ...>
General
-m, --max-processes How many processes should run at once.
New processes only spawn after all restart tries of a
process. [number]
-n, --names List of custom names to be used in prefix template.
Example names: "main,browser,server" [string]
--name-separator The character to split <names> on. Example usage:
concurrently -n "styles|scripts|server" --name-separator
"|" [default: ","]
-r, --raw Output only raw output of processes, disables prettifying
and concurrently coloring. [boolean]
-s, --success Return exit code of zero or one based on the success or
failure of the "first" child to terminate, the "last
child", or succeed only if "all" child processes succeed.
-m, --max-processes How many processes should run at once.
New processes only spawn after all restart tries of a
process. [number]
-n, --names List of custom names to be used in prefix template.
Example names: "main,browser,server" [string]
--name-separator The character to split <names> on. Example usage:
concurrently -n "styles|scripts|server" --name-separator
"|" [default: ","]
-r, --raw Output only raw output of processes, disables
prettifying and concurrently coloring. [boolean]
-s, --success Return exit code of zero or one based on the success or
failure of the "first" child to terminate, the "last
child", or succeed only if "all" child processes
succeed.
[choices: "first", "last", "all"] [default: "all"]
--no-color Disables colors from logging [boolean]
--no-color Disables colors from logging [boolean]
--hide Comma-separated list of processes to hide the output.
The processes can be identified by their name or index.
[string] [default: ""]
Prefix styling
-p, --prefix Prefix used in logging for each process.
Expand Down
10 changes: 9 additions & 1 deletion bin/concurrently.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ const args = yargs
describe: 'Disables colors from logging',
type: 'boolean'
},
'hide': {
describe:
'Comma-separated list of processes to hide the output.\n' +
'The processes can be identified by their name or index.',
default: defaults.hide,
type: 'string'
},

// Kill others
'k': {
Expand Down Expand Up @@ -135,7 +142,7 @@ const args = yargs
'Can be either the index or the name of the process.'
}
})
.group(['m', 'n', 'name-separator', 'raw', 's', 'no-color'], 'General')
.group(['m', 'n', 'name-separator', 'raw', 's', 'no-color', 'hide'], 'General')
.group(['p', 'c', 'l', 't'], 'Prefix styling')
.group(['i', 'default-input-target'], 'Input handling')
.group(['k', 'kill-others-on-fail'], 'Killing other processes')
Expand All @@ -157,6 +164,7 @@ concurrently(args._.map((command, index) => ({
: (args.killOthersOnFail ? ['failure'] : []),
maxProcesses: args.maxProcesses,
raw: args.raw,
hide: args.hide.split(','),
prefix: args.prefix,
prefixColors: args.prefixColors.split(','),
prefixLength: args.prefixLength,
Expand Down
20 changes: 20 additions & 0 deletions bin/concurrently.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,26 @@ describe('--raw', () => {
});
});

describe('--hide', () => {
it('hides the output of a process by its index', done => {
const child = run('--hide 1 "echo foo" "echo bar"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines).toContainEqual(expect.stringContaining('foo'));
expect(lines).not.toContainEqual(expect.stringContaining('bar'));
done();
}, done);
});

it('hides the output of a process by its name', done => {
const child = run('-n foo,bar --hide bar "echo foo" "echo bar"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines).toContainEqual(expect.stringContaining('foo'));
expect(lines).not.toContainEqual(expect.stringContaining('bar'));
done();
}, done);
});
});

describe('--names', () => {
it('is aliased to -n', done => {
const child = run('-n foo,bar "echo foo" "echo bar"');
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Logger = require('./src/logger');

module.exports = exports = (commands, options = {}) => {
const logger = new Logger({
hide: options.hide,
outputStream: options.outputStream || process.stdout,
prefixFormat: options.prefix,
prefixLength: options.prefixLength,
Expand Down
2 changes: 2 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module.exports = {
handleInput: false,
// How many processes to run at once
maxProcesses: 0,
// Indices and names of commands whose output to be not logged
hide: '',
nameSeparator: ',',
// Which prefix style to use when logging processes output.
prefix: '',
Expand Down
10 changes: 9 additions & 1 deletion src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ const formatDate = require('date-fns/format');
const defaults = require('./defaults');

module.exports = class Logger {
constructor({ outputStream, prefixFormat, prefixLength, raw, timestampFormat }) {
constructor({ hide, outputStream, prefixFormat, prefixLength, raw, timestampFormat }) {
// To avoid empty strings from hiding the output of commands that don't have a name,
// keep in the list of commands to hide only strings with some length.
// This might happen through the CLI when no `--hide` argument is specified, for example.
this.hide = _.castArray(hide).filter(name => name || name === 0).map(String);
this.raw = raw;
this.outputStream = outputStream;
this.prefixFormat = prefixFormat;
Expand Down Expand Up @@ -76,6 +80,10 @@ module.exports = class Logger {
}

logCommandText(text, command) {
if (this.hide.includes(String(command.index)) || this.hide.includes(command.name)) {
return;
}

const prefix = this.colorText(command, this.getPrefix(command));
return this.log(prefix + (prefix ? ' ' : ''), text);
}
Expand Down
28 changes: 28 additions & 0 deletions src/logger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ describe('#logCommandText()', () => {

expect(logger.log).toHaveBeenCalledWith(chalk.hex(prefixColor)('[1]') + ' ', 'foo');
});

it('does nothing if command is hidden by name', () => {
const logger = createLogger({ hide: ['abc'] });
logger.logCommandText('foo', { name: 'abc' });

expect(logger.log).not.toHaveBeenCalled();
});

it('does nothing if command is hidden by index', () => {
const logger = createLogger({ hide: [3] });
logger.logCommandText('foo', { index: 3 });

expect(logger.log).not.toHaveBeenCalled();
});
});

describe('#logCommandEvent()', () => {
Expand All @@ -186,6 +200,20 @@ describe('#logCommandEvent()', () => {
expect(logger.log).not.toHaveBeenCalled();
});

it('does nothing if command is hidden by name', () => {
const logger = createLogger({ hide: ['abc'] });
logger.logCommandEvent('foo', { name: 'abc' });

expect(logger.log).not.toHaveBeenCalled();
});

it('does nothing if command is hidden by index', () => {
const logger = createLogger({ hide: [3] });
logger.logCommandEvent('foo', { index: 3 });

expect(logger.log).not.toHaveBeenCalled();
});

it('logs text in gray dim', () => {
const logger = createLogger();
logger.logCommandEvent('foo', { index: 1 });
Expand Down

0 comments on commit 88b8d19

Please sign in to comment.