Skip to content

Commit f79eb76

Browse files
jplukasarcanis
andauthored
feat: yarn run --json (#5988)
**What's the problem this PR addresses?** Adds a `--json` option to the `yarn run` CLI command Closes #5539 **How did you fix it?** Changed the command object defined in berry/packages/plugin-essentials/sources/commands/runindex.ts in order to add a `--json` option, and format the output. **Checklist** - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). - [x] I have set the packages that need to be released for my changes to be effective. - [x] I will check that all automated PR checks pass before the PR gets reviewed. --------- Co-authored-by: Maël Nison <[email protected]>
1 parent bd545d8 commit f79eb76

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

.yarn/versions/4db9f07d.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
releases:
2+
"@yarnpkg/cli": minor
3+
"@yarnpkg/plugin-essentials": minor
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-constraints"
8+
- "@yarnpkg/plugin-dlx"
9+
- "@yarnpkg/plugin-init"
10+
- "@yarnpkg/plugin-interactive-tools"
11+
- "@yarnpkg/plugin-nm"
12+
- "@yarnpkg/plugin-npm-cli"
13+
- "@yarnpkg/plugin-pack"
14+
- "@yarnpkg/plugin-patch"
15+
- "@yarnpkg/plugin-pnp"
16+
- "@yarnpkg/plugin-pnpm"
17+
- "@yarnpkg/plugin-stage"
18+
- "@yarnpkg/plugin-typescript"
19+
- "@yarnpkg/plugin-version"
20+
- "@yarnpkg/plugin-workspace-tools"
21+
- "@yarnpkg/builder"
22+
- "@yarnpkg/core"
23+
- "@yarnpkg/doctor"

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {ppath, xfs} from '@yarnpkg/fslib';
2+
import {misc} from 'pkg-tests-core';
23

34
describe(`Commands`, () => {
45
for (const [description, args] of [[`with prefix`, [`run`]], [`without prefix`, []]]) {
@@ -158,6 +159,27 @@ describe(`Commands`, () => {
158159
},
159160
),
160161
);
162+
test(`it should print the list of available scripts as JSON if no parameters passed to command`,
163+
makeTemporaryEnv(
164+
{
165+
scripts: {
166+
foo: `echo hello`,
167+
bar: `echo hi`,
168+
},
169+
},
170+
async ({path, run, source}) => {
171+
const {stdout} = await run(`run`, `--json`);
172+
173+
expect(misc.parseJsonStream(stdout)).toEqual([{
174+
name: `foo`,
175+
script: `echo hello`,
176+
}, {
177+
name: `bar`,
178+
script: `echo hi`,
179+
}]);
180+
},
181+
),
182+
);
161183

162184
test(`it should normalize scoped bin entries`,
163185
makeTemporaryEnv(

packages/plugin-essentials/sources/commands/runIndex.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {BaseCommand, WorkspaceRequiredError} from '@yarnpkg/cli';
22
import {Configuration, Project, StreamReport} from '@yarnpkg/core';
33
import {miscUtils} from '@yarnpkg/core';
4+
import {Option} from 'clipanion';
45
import {inspect} from 'util';
56

67
// eslint-disable-next-line arca/no-default-export
@@ -9,6 +10,10 @@ export default class RunIndexCommand extends BaseCommand {
910
[`run`],
1011
];
1112

13+
json = Option.Boolean(`--json`, false, {
14+
description: `Format the output as an NDJSON stream`,
15+
});
16+
1217
async execute() {
1318
const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
1419
const {project, workspace} = await Project.find(configuration, this.context.cwd);
@@ -19,6 +24,7 @@ export default class RunIndexCommand extends BaseCommand {
1924
const report = await StreamReport.start({
2025
configuration,
2126
stdout: this.context.stdout,
27+
json: this.json,
2228
}, async report => {
2329
const scripts = workspace!.manifest.scripts;
2430
const keys = miscUtils.sortMap(scripts.keys(), key => key);
@@ -32,8 +38,9 @@ export default class RunIndexCommand extends BaseCommand {
3238
return Math.max(max, key.length);
3339
}, 0);
3440

35-
for (const [key, value] of scripts.entries()) {
36-
report.reportInfo(null, `${key.padEnd(maxKeyLength, ` `)} ${inspect(value, inspectConfig)}`);
41+
for (const [key, script] of scripts.entries()) {
42+
report.reportInfo(null, `${key.padEnd(maxKeyLength, ` `)} ${inspect(script, inspectConfig)}`);
43+
report.reportJson({name: key, script});
3744
}
3845
});
3946

0 commit comments

Comments
 (0)