Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(foreach)!: automatically enable --verbose in tty environments #4595

Merged
merged 2 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .yarn/versions/63ea7bc1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
releases:
"@yarnpkg/cli": major
"@yarnpkg/plugin-workspace-tools": major

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Yarn now accepts sponsorships! Please give a look at our [OpenCollective](https:
- Plugins cannot access the internal copy of Yup anymore (use [Typanion](https://github.com/arcanis/typanion) instead)
- The network settings configuration option has been renamed from `caFilePath` to `httpsCaFilePath`.
- Set `nmMode` to `hardlinks-local` by default.
- `yarn workspaces foreach` now automatically enables the `-v,--verbose` flag in interactive terminal environments.

### **API Changes**

Expand Down
16 changes: 10 additions & 6 deletions packages/plugin-workspace-tools/sources/commands/foreach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import micromatch from 'micro
import {cpus} from 'os';
import pLimit from 'p-limit';
import {Writable} from 'stream';
import {WriteStream} from 'tty';
import * as t from 'typanion';

// eslint-disable-next-line arca/no-default-export
Expand Down Expand Up @@ -38,7 +39,7 @@ export default class WorkspacesForeachCommand extends BaseCommand {

- The command may apply to only some workspaces through the use of \`--include\` which acts as a whitelist. The \`--exclude\` flag will do the opposite and will be a list of packages that mustn't execute the script. Both flags accept glob patterns (if valid Idents and supported by [micromatch](https://github.com/micromatch/micromatch)). Make sure to escape the patterns, to prevent your own shell from trying to expand them.

Adding the \`-v,--verbose\` flag will cause Yarn to print more information; in particular the name of the workspace that generated the output will be printed at the front of each line.
Adding the \`-v,--verbose\` flag (automatically enabled in interactive terminal environments) will cause Yarn to print more information; in particular the name of the workspace that generated the output will be printed at the front of each line.

If the command is \`run\` and the script being run does not exist the child workspace will be skipped without error.
`,
Expand Down Expand Up @@ -70,7 +71,7 @@ export default class WorkspacesForeachCommand extends BaseCommand {
description: `Run the command on all workspaces of a project`,
});

verbose = Option.Boolean(`-v,--verbose`, false, {
verbose = Option.Boolean(`-v,--verbose`, {
description: `Prefix each output line with the name of the originating workspace`,
});

Expand Down Expand Up @@ -193,6 +194,9 @@ export default class WorkspacesForeachCommand extends BaseCommand {
workspaces.push(workspace);
}

// --verbose is automatically enabled in TTYs
const verbose = this.verbose ?? (this.context.stdout as WriteStream).isTTY;

const concurrency = this.parallel ?
(this.jobs === `unlimited`
? Infinity
Expand Down Expand Up @@ -222,16 +226,16 @@ export default class WorkspacesForeachCommand extends BaseCommand {
if (abortNextCommands)
return -1;

if (!parallel && this.verbose && commandIndex > 1)
if (!parallel && verbose && commandIndex > 1)
report.reportSeparator();

const prefix = getPrefix(workspace, {configuration, verbose: this.verbose, commandIndex});
const prefix = getPrefix(workspace, {configuration, verbose, commandIndex});

const [stdout, stdoutEnd] = createStream(report, {prefix, interlaced});
const [stderr, stderrEnd] = createStream(report, {prefix, interlaced});

try {
if (this.verbose)
if (verbose)
report.reportInfo(null, `${prefix} Process started`);

const start = Date.now();
Expand All @@ -249,7 +253,7 @@ export default class WorkspacesForeachCommand extends BaseCommand {
await stderrEnd;

const end = Date.now();
if (this.verbose) {
if (verbose) {
const timerMessage = configuration.get(`enableTimers`) ? `, completed in ${formatUtils.pretty(configuration, end - start, formatUtils.Type.DURATION)}` : ``;
report.reportInfo(null, `${prefix} Process exited (exit code ${exitCode})${timerMessage}`);
}
Expand Down