Skip to content

Commit e52d984

Browse files
authored
Make --raw compatible with --hide (#486)
1 parent 3dc909a commit e52d984

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

bin/concurrently.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,22 @@ describe('--hide', () => {
234234
expect(lines).toContainEqual(expect.stringContaining('foo'));
235235
expect(lines).not.toContainEqual(expect.stringContaining('bar'));
236236
});
237+
238+
it('hides the output of a process by its index in raw mode', async () => {
239+
const lines = await run('--hide 1 --raw "echo foo" "echo bar"').getLogLines();
240+
241+
expect(lines).toHaveLength(1);
242+
expect(lines).toContainEqual(expect.stringContaining('foo'));
243+
expect(lines).not.toContainEqual(expect.stringContaining('bar'));
244+
});
245+
246+
it('hides the output of a process by its name in raw mode', async () => {
247+
const lines = await run('-n foo,bar --hide bar --raw "echo foo" "echo bar"').getLogLines();
248+
249+
expect(lines).toHaveLength(1);
250+
expect(lines).toContainEqual(expect.stringContaining('foo'));
251+
expect(lines).not.toContainEqual(expect.stringContaining('bar'));
252+
});
237253
});
238254

239255
describe('--group', () => {

src/concurrently.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ import { cpus } from 'os';
44
import { Writable } from 'stream';
55
import treeKill from 'tree-kill';
66

7-
import { CloseEvent, Command, CommandInfo, KillProcess, SpawnCommand } from './command';
7+
import {
8+
CloseEvent,
9+
Command,
10+
CommandIdentifier,
11+
CommandInfo,
12+
KillProcess,
13+
SpawnCommand,
14+
} from './command';
815
import { CommandParser } from './command-parser/command-parser';
916
import { ExpandArguments } from './command-parser/expand-arguments';
1017
import { ExpandNpmShortcut } from './command-parser/expand-npm-shortcut';
@@ -94,6 +101,11 @@ export type ConcurrentlyOptions = {
94101
*/
95102
raw?: boolean;
96103

104+
/**
105+
* Which command(s) should have their output hidden.
106+
*/
107+
hide?: CommandIdentifier | CommandIdentifier[];
108+
97109
/**
98110
* The current working directory of commands which didn't specify one.
99111
* Defaults to `process.cwd()`.
@@ -169,6 +181,13 @@ export function concurrently(
169181
commandParsers.push(new ExpandArguments(options.additionalArguments));
170182
}
171183

184+
// To avoid empty strings from hiding the output of commands that don't have a name,
185+
// keep in the list of commands to hide only strings with some length.
186+
// This might happen through the CLI when no `--hide` argument is specified, for example.
187+
const hide = _.castArray(options.hide)
188+
.filter((name) => name || name === 0)
189+
.map(String);
190+
172191
let commands = _(baseCommands)
173192
.map(mapToCommandInfo)
174193
.flatMap((command) => parseCommand(command, commandParsers))
@@ -183,6 +202,7 @@ export function concurrently(
183202
raw: command.raw ?? options.raw,
184203
env: command.env,
185204
cwd: command.cwd || options.cwd,
205+
hide: hide.includes(String(index)) || hide.includes(command.name),
186206
}),
187207
options.spawn,
188208
options.kill,

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export function concurrently(
110110
raw: options.raw,
111111
successCondition: options.successCondition,
112112
cwd: options.cwd,
113+
hide: options.hide,
113114
logger,
114115
outputStream: options.outputStream || process.stdout,
115116
group: options.group,

src/spawn.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ describe('getSpawnOpts()', () => {
3131
expect(getSpawnOpts({ raw: true }).stdio).toBe('inherit');
3232
});
3333

34+
it('unsets stdio when raw and hide', () => {
35+
expect(getSpawnOpts({ raw: true, hide: true }).stdio).toBeUndefined();
36+
});
37+
3438
it('merges FORCE_COLOR into env vars if color supported', () => {
3539
const process = { ...baseProcess, env: { foo: 'bar' } };
3640
expect(getSpawnOpts({ process, colorSupport: false }).env).toEqual(process.env);

src/spawn.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const getSpawnOpts = ({
2828
process = global.process,
2929
raw = false,
3030
env = {},
31+
hide = false,
3132
}: {
3233
/**
3334
* What the color support of the spawned processes should be.
@@ -58,9 +59,15 @@ export const getSpawnOpts = ({
5859
* Map of custom environment variables to include in the spawn options.
5960
*/
6061
env?: Record<string, unknown>;
62+
63+
/**
64+
* Whether to hide the standard output.
65+
* Defaults to false.
66+
*/
67+
hide?: boolean;
6168
}): SpawnOptions => ({
6269
cwd: cwd || process.cwd(),
63-
...(raw && { stdio: 'inherit' as const }),
70+
...(raw && !hide && { stdio: 'inherit' as const }),
6471
...(/^win/.test(process.platform) && { detached: false }),
6572
env: {
6673
...(colorSupport ? { FORCE_COLOR: colorSupport.level.toString() } : {}),

0 commit comments

Comments
 (0)