Skip to content

feat: yarn run --json #5988

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

Merged
merged 8 commits into from
Jan 16, 2024
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/4db9f07d.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
releases:
"@yarnpkg/cli": minor
"@yarnpkg/plugin-essentials": minor

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@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/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ppath, xfs} from '@yarnpkg/fslib';
import {misc} from 'pkg-tests-core';

describe(`Commands`, () => {
for (const [description, args] of [[`with prefix`, [`run`]], [`without prefix`, []]]) {
Expand Down Expand Up @@ -158,6 +159,27 @@ describe(`Commands`, () => {
},
),
);
test(`it should print the list of available scripts as JSON if no parameters passed to command`,
makeTemporaryEnv(
{
scripts: {
foo: `echo hello`,
bar: `echo hi`,
},
},
async ({path, run, source}) => {
const {stdout} = await run(`run`, `--json`);

expect(misc.parseJsonStream(stdout)).toEqual([{
name: `foo`,
script: `echo hello`,
}, {
name: `bar`,
script: `echo hi`,
}]);
},
),
);

test(`it should normalize scoped bin entries`,
makeTemporaryEnv(
Expand Down
11 changes: 9 additions & 2 deletions packages/plugin-essentials/sources/commands/runIndex.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {BaseCommand, WorkspaceRequiredError} from '@yarnpkg/cli';
import {Configuration, Project, StreamReport} from '@yarnpkg/core';
import {miscUtils} from '@yarnpkg/core';
import {Option} from 'clipanion';
import {inspect} from 'util';

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

json = Option.Boolean(`--json`, false, {
description: `Format the output as an NDJSON stream`,
});

async execute() {
const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
const {project, workspace} = await Project.find(configuration, this.context.cwd);
Expand All @@ -19,6 +24,7 @@ export default class RunIndexCommand extends BaseCommand {
const report = await StreamReport.start({
configuration,
stdout: this.context.stdout,
json: this.json,
}, async report => {
const scripts = workspace!.manifest.scripts;
const keys = miscUtils.sortMap(scripts.keys(), key => key);
Expand All @@ -32,8 +38,9 @@ export default class RunIndexCommand extends BaseCommand {
return Math.max(max, key.length);
}, 0);

for (const [key, value] of scripts.entries()) {
report.reportInfo(null, `${key.padEnd(maxKeyLength, ` `)} ${inspect(value, inspectConfig)}`);
for (const [key, script] of scripts.entries()) {
report.reportInfo(null, `${key.padEnd(maxKeyLength, ` `)} ${inspect(script, inspectConfig)}`);
report.reportJson({name: key, script});
}
});

Expand Down