Skip to content

Commit 013b935

Browse files
committed
Add --label argument to workspaces foreach.
1 parent f67dda8 commit 013b935

File tree

3 files changed

+102
-10
lines changed

3 files changed

+102
-10
lines changed

packages/acceptance-tests/pkg-tests-specs/sources/commands/workspaces/__snapshots__/foreach.test.js.snap

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,42 @@ Done
186186
}
187187
`;
188188

189+
exports[`Commands workspace foreach should not prefix the output when run with --verbose and --no-label 1`] = `
190+
{
191+
"code": 0,
192+
"stderr": "",
193+
"stdout": "Process started
194+
Test Workspace A
195+
Process exited (exit code 0)
196+
197+
Process started
198+
Test Workspace B
199+
Process exited (exit code 0)
200+
201+
Process started
202+
Test Workspace C
203+
Process exited (exit code 0)
204+
205+
Process started
206+
Test Workspace D
207+
Process exited (exit code 0)
208+
209+
Process started
210+
Test Workspace E
211+
Process exited (exit code 0)
212+
213+
Process started
214+
Test Workspace F
215+
Process exited (exit code 0)
216+
217+
Process started
218+
Test Workspace G
219+
Process exited (exit code 0)
220+
Done
221+
",
222+
}
223+
`;
224+
189225
exports[`Commands workspace foreach should only run the scripts on workspaces that match the --include list 1`] = `
190226
{
191227
"code": 0,
@@ -230,7 +266,7 @@ Done
230266
}
231267
`;
232268

233-
exports[`Commands workspace foreach should prefix the output with run with --verbose 1`] = `
269+
exports[`Commands workspace foreach should prefix the output and including timing information when run with --verbose 1`] = `
234270
{
235271
"code": 0,
236272
"stderr": "",
@@ -266,6 +302,22 @@ Done
266302
}
267303
`;
268304

305+
exports[`Commands workspace foreach should prefix the output when run with --label 1`] = `
306+
{
307+
"code": 0,
308+
"stderr": "",
309+
"stdout": "[workspace-a]: Test Workspace A
310+
[workspace-b]: Test Workspace B
311+
[workspace-c]: Test Workspace C
312+
[workspace-d]: Test Workspace D
313+
[workspace-e]: Test Workspace E
314+
[workspace-f]: Test Workspace F
315+
[workspace-g]: Test Workspace G
316+
Done
317+
",
318+
}
319+
`;
320+
269321
exports[`Commands workspace foreach should run execute global scripts even on workspaces that don't declare them 1`] = `
270322
{
271323
"code": 0,

packages/acceptance-tests/pkg-tests-specs/sources/commands/workspaces/foreach.test.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,25 @@ describe(`Commands`, () => {
216216
),
217217
);
218218

219+
220+
test(
221+
`should prefix the output when run with --label`,
222+
makeTemporaryEnv(
223+
{
224+
private: true,
225+
workspaces: [`packages/*`],
226+
},
227+
async ({path, run}) => {
228+
await setupWorkspaces(path);
229+
await run(`install`);
230+
231+
await expect(run(`workspaces`, `foreach`, `--all`, `--label`, `run`, `print`)).resolves.toMatchSnapshot();
232+
},
233+
),
234+
);
235+
219236
test(
220-
`should prefix the output with run with --verbose`,
237+
`should prefix the output and including timing information when run with --verbose`,
221238
makeTemporaryEnv(
222239
{
223240
private: true,
@@ -232,6 +249,22 @@ describe(`Commands`, () => {
232249
),
233250
);
234251

252+
test(
253+
`should not prefix the output when run with --verbose and --no-label`,
254+
makeTemporaryEnv(
255+
{
256+
private: true,
257+
workspaces: [`packages/*`],
258+
},
259+
async ({path, run}) => {
260+
await setupWorkspaces(path);
261+
await run(`install`);
262+
263+
await expect(run(`workspaces`, `foreach`, `--all`, `--verbose`, `--no-label`, `run`, `print`)).resolves.toMatchSnapshot();
264+
},
265+
),
266+
);
267+
235268
test(
236269
`should not include the prefix or a ➤ character when run with --no-verbose`,
237270
makeTemporaryEnv(

packages/plugin-workspace-tools/sources/commands/foreach.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ export default class WorkspacesForeachCommand extends BaseCommand {
4242
4343
- 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.
4444
45-
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.
45+
The \`-l,--label\` flag will prefix all output with the name of the workspace that generated it.
46+
47+
The \`-v,--verbose\` flag (automatically enabled in interactive terminal environments) implies \`-l,--label\` and will additionally include start/stop and timing information.
4648
4749
If the command is \`run\` and the script being run does not exist the child workspace will be skipped without error.
4850
`,
@@ -83,10 +85,14 @@ export default class WorkspacesForeachCommand extends BaseCommand {
8385
description: `Run the command on all workspaces of the current worktree`,
8486
});
8587

86-
verbose = Option.Boolean(`-v,--verbose`, {
88+
label = Option.Boolean(`-l,--label`, {
8789
description: `Prefix each output line with the name of the originating workspace`,
8890
});
8991

92+
verbose = Option.Boolean(`-v,--verbose`, {
93+
description: `Include start/top and timing information in output; implies \`-l,--label\``,
94+
});
95+
9096
parallel = Option.Boolean(`-p,--parallel`, false, {
9197
description: `Run the commands in parallel`,
9298
});
@@ -277,6 +283,7 @@ export default class WorkspacesForeachCommand extends BaseCommand {
277283

278284
// --verbose is automatically enabled in TTYs
279285
const verbose = this.verbose ?? (this.context.stdout as WriteStream).isTTY;
286+
const label = this.label ?? this.verbose;
280287

281288
const concurrency = this.parallel ?
282289
(this.jobs === `unlimited`
@@ -311,14 +318,14 @@ export default class WorkspacesForeachCommand extends BaseCommand {
311318
if (!parallel && verbose && commandIndex > 1)
312319
report.reportSeparator();
313320

314-
const prefix = getPrefix(workspace, {configuration, verbose, commandIndex});
321+
const prefix = getPrefix(workspace, {configuration, label, commandIndex});
315322

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

319326
try {
320327
if (verbose)
321-
report.reportInfo(null, `${prefix} Process started`);
328+
report.reportInfo(null, `${prefix ? `${prefix} ` : ``}Process started`);
322329

323330
const start = Date.now();
324331

@@ -337,7 +344,7 @@ export default class WorkspacesForeachCommand extends BaseCommand {
337344
const end = Date.now();
338345
if (verbose) {
339346
const timerMessage = configuration.get(`enableTimers`) ? `, completed in ${formatUtils.pretty(configuration, end - start, formatUtils.Type.DURATION)}` : ``;
340-
report.reportInfo(null, `${prefix} Process exited (exit code ${exitCode})${timerMessage}`);
347+
report.reportInfo(null, `${prefix ? `${prefix} ` : ``}Process exited (exit code ${exitCode})${timerMessage}`);
341348
}
342349

343350
if (exitCode === 130) {
@@ -475,11 +482,11 @@ function createStream(report: Report, {prefix, interlaced}: {prefix: string | nu
475482
type GetPrefixOptions = {
476483
configuration: Configuration;
477484
commandIndex: number;
478-
verbose: boolean;
485+
label: boolean;
479486
};
480487

481-
function getPrefix(workspace: Workspace, {configuration, commandIndex, verbose}: GetPrefixOptions) {
482-
if (!verbose)
488+
function getPrefix(workspace: Workspace, {configuration, commandIndex, label}: GetPrefixOptions) {
489+
if (!label)
483490
return null;
484491

485492
const name = structUtils.stringifyIdent(workspace.anchoredLocator);

0 commit comments

Comments
 (0)