diff --git a/.yarn/versions/4db9f07d.yml b/.yarn/versions/4db9f07d.yml new file mode 100644 index 000000000000..54642a287609 --- /dev/null +++ b/.yarn/versions/4db9f07d.yml @@ -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" diff --git a/packages/acceptance-tests/pkg-tests-specs/sources/commands/run.test.js b/packages/acceptance-tests/pkg-tests-specs/sources/commands/run.test.js index ece9fc2d82df..e68d6e9f2318 100644 --- a/packages/acceptance-tests/pkg-tests-specs/sources/commands/run.test.js +++ b/packages/acceptance-tests/pkg-tests-specs/sources/commands/run.test.js @@ -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`, []]]) { @@ -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( diff --git a/packages/plugin-essentials/sources/commands/runIndex.ts b/packages/plugin-essentials/sources/commands/runIndex.ts index 3d09493e3638..48f47a93f218 100644 --- a/packages/plugin-essentials/sources/commands/runIndex.ts +++ b/packages/plugin-essentials/sources/commands/runIndex.ts @@ -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 @@ -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); @@ -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); @@ -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}); } });