Skip to content

Commit

Permalink
Make --raw compatible with --hide (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
PoQuatre authored Jul 3, 2024
1 parent 3dc909a commit e52d984
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
16 changes: 16 additions & 0 deletions bin/concurrently.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,22 @@ describe('--hide', () => {
expect(lines).toContainEqual(expect.stringContaining('foo'));
expect(lines).not.toContainEqual(expect.stringContaining('bar'));
});

it('hides the output of a process by its index in raw mode', async () => {
const lines = await run('--hide 1 --raw "echo foo" "echo bar"').getLogLines();

expect(lines).toHaveLength(1);
expect(lines).toContainEqual(expect.stringContaining('foo'));
expect(lines).not.toContainEqual(expect.stringContaining('bar'));
});

it('hides the output of a process by its name in raw mode', async () => {
const lines = await run('-n foo,bar --hide bar --raw "echo foo" "echo bar"').getLogLines();

expect(lines).toHaveLength(1);
expect(lines).toContainEqual(expect.stringContaining('foo'));
expect(lines).not.toContainEqual(expect.stringContaining('bar'));
});
});

describe('--group', () => {
Expand Down
22 changes: 21 additions & 1 deletion src/concurrently.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import { cpus } from 'os';
import { Writable } from 'stream';
import treeKill from 'tree-kill';

import { CloseEvent, Command, CommandInfo, KillProcess, SpawnCommand } from './command';
import {
CloseEvent,
Command,
CommandIdentifier,
CommandInfo,
KillProcess,
SpawnCommand,
} from './command';
import { CommandParser } from './command-parser/command-parser';
import { ExpandArguments } from './command-parser/expand-arguments';
import { ExpandNpmShortcut } from './command-parser/expand-npm-shortcut';
Expand Down Expand Up @@ -94,6 +101,11 @@ export type ConcurrentlyOptions = {
*/
raw?: boolean;

/**
* Which command(s) should have their output hidden.
*/
hide?: CommandIdentifier | CommandIdentifier[];

/**
* The current working directory of commands which didn't specify one.
* Defaults to `process.cwd()`.
Expand Down Expand Up @@ -169,6 +181,13 @@ export function concurrently(
commandParsers.push(new ExpandArguments(options.additionalArguments));
}

// 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.
const hide = _.castArray(options.hide)
.filter((name) => name || name === 0)
.map(String);

let commands = _(baseCommands)
.map(mapToCommandInfo)
.flatMap((command) => parseCommand(command, commandParsers))
Expand All @@ -183,6 +202,7 @@ export function concurrently(
raw: command.raw ?? options.raw,
env: command.env,
cwd: command.cwd || options.cwd,
hide: hide.includes(String(index)) || hide.includes(command.name),
}),
options.spawn,
options.kill,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export function concurrently(
raw: options.raw,
successCondition: options.successCondition,
cwd: options.cwd,
hide: options.hide,
logger,
outputStream: options.outputStream || process.stdout,
group: options.group,
Expand Down
4 changes: 4 additions & 0 deletions src/spawn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ describe('getSpawnOpts()', () => {
expect(getSpawnOpts({ raw: true }).stdio).toBe('inherit');
});

it('unsets stdio when raw and hide', () => {
expect(getSpawnOpts({ raw: true, hide: true }).stdio).toBeUndefined();
});

it('merges FORCE_COLOR into env vars if color supported', () => {
const process = { ...baseProcess, env: { foo: 'bar' } };
expect(getSpawnOpts({ process, colorSupport: false }).env).toEqual(process.env);
Expand Down
9 changes: 8 additions & 1 deletion src/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const getSpawnOpts = ({
process = global.process,
raw = false,
env = {},
hide = false,
}: {
/**
* What the color support of the spawned processes should be.
Expand Down Expand Up @@ -58,9 +59,15 @@ export const getSpawnOpts = ({
* Map of custom environment variables to include in the spawn options.
*/
env?: Record<string, unknown>;

/**
* Whether to hide the standard output.
* Defaults to false.
*/
hide?: boolean;
}): SpawnOptions => ({
cwd: cwd || process.cwd(),
...(raw && { stdio: 'inherit' as const }),
...(raw && !hide && { stdio: 'inherit' as const }),
...(/^win/.test(process.platform) && { detached: false }),
env: {
...(colorSupport ? { FORCE_COLOR: colorSupport.level.toString() } : {}),
Expand Down

0 comments on commit e52d984

Please sign in to comment.