refactor(plugin-commands-installation): optimize cli output after select dependencies when update (#7125)

* refactor(plugin-commands-installation): optimize cli output after select dependencies when update

* docs: add changeset

* chore: set dep group choice to disabled

* fix: test case

* feat: filter out dep group choice in prompt output

* docs: update changesets

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
await-ovo
2023-10-14 03:36:08 +08:00
committed by GitHub
parent 1c4e7817b3
commit bc83798d46
5 changed files with 60 additions and 19 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-installation": patch
"pnpm": patch
---
Optimize selection result output of `pnpm update --interactive` [7109](https://github.com/pnpm/pnpm/issues/7109)

View File

@@ -13,7 +13,9 @@ export interface ChoiceRow {
type ChoiceGroup = Array<{
name: string
message: string
choices: ChoiceRow[]
disabled?: boolean
}>
export function getUpdateChoices (outdatedPkgsOfProjects: OutdatedPackage[], workspacesEnabled: boolean) {
@@ -67,12 +69,16 @@ export function getUpdateChoices (outdatedPkgsOfProjects: OutdatedPackage[], wor
}
}
return {
name: renderedTable[i],
name: outdatedPkg.name,
message: renderedTable[i],
value: outdatedPkg.name,
}
})
finalChoices.push({ name: depGroup, choices })
// To filter out selected "dependencies" or "devDependencies" in the final output,
// we rename it here to "[dependencies]" or "[devDependencies]",
// which will be filtered out in the format function of the prompt.
finalChoices.push({ name: `[${depGroup}]`, choices, message: depGroup })
return finalChoices
}, [])

View File

@@ -225,6 +225,18 @@ async function interactiveUpdate (
result () {
return this.selected
},
format () {
if (!this.state.submitted || this.state.cancelled) return ''
if (Array.isArray(this.selected)) {
return this.selected
// The custom format function is used to filter out "[dependencies]" or "[devDependencies]" from the output.
// https://github.com/enquirer/enquirer/blob/master/lib/prompts/select.js#L98
.filter((choice: ChoiceRow) => !/^\[.+\]$/.test(choice.name))
.map((choice: ChoiceRow) => this.styles.primary(choice.name)).join(', ')
}
return this.styles.primary(this.selected.name)
},
styles: {
dark: chalk.white,
em: chalk.bgBlack.whiteBright,

View File

@@ -77,7 +77,8 @@ test('getUpdateChoices()', () => {
], false))
.toStrictEqual([
{
name: 'dependencies',
name: '[dependencies]',
message: 'dependencies',
choices: [
{
name: 'Package Current Target URL ',
@@ -86,13 +87,15 @@ test('getUpdateChoices()', () => {
value: '',
},
{
name: chalk`foo 1.0.0 {redBright.bold 2.0.0} https://pnpm.io/ `,
message: chalk`foo 1.0.0 {redBright.bold 2.0.0} https://pnpm.io/ `,
value: 'foo',
name: 'foo',
},
],
},
{
name: 'devDependencies',
name: '[devDependencies]',
message: 'devDependencies',
choices: [
{
name: 'Package Current Target URL ',
@@ -101,21 +104,25 @@ test('getUpdateChoices()', () => {
value: '',
},
{
name: chalk`qar 1.0.0 1.{yellowBright.bold 2.0} `,
message: chalk`qar 1.0.0 1.{yellowBright.bold 2.0} `,
name: 'qar',
value: 'qar',
},
{
name: chalk`zoo 1.1.0 1.{yellowBright.bold 2.0} `,
message: chalk`zoo 1.1.0 1.{yellowBright.bold 2.0} `,
name: 'zoo',
value: 'zoo',
},
{
name: chalk`foo 1.0.1 1.{yellowBright.bold 2.0} `,
message: chalk`foo 1.0.1 1.{yellowBright.bold 2.0} `,
name: 'foo',
value: 'foo',
},
],
},
{
name: 'optionalDependencies',
name: '[optionalDependencies]',
message: 'optionalDependencies',
choices: [
{
name: 'Package Current Target URL ',
@@ -124,7 +131,8 @@ test('getUpdateChoices()', () => {
value: '',
},
{
name: chalk`qaz 1.0.1 1.{yellowBright.bold 2.0} `,
message: chalk`qaz 1.0.1 1.{yellowBright.bold 2.0} `,
name: 'qaz',
value: 'qaz',
},
],

View File

@@ -100,15 +100,18 @@ test('interactively update', async () => {
choices: [
headerChoice,
{
name: chalk`is-negative 1.0.0 1.0.{greenBright.bold 1} `,
message: chalk`is-negative 1.0.0 1.0.{greenBright.bold 1} `,
value: 'is-negative',
name: 'is-negative',
},
{
name: chalk`micromatch 3.0.0 3.{yellowBright.bold 1.10} `,
message: chalk`micromatch 3.0.0 3.{yellowBright.bold 1.10} `,
value: 'micromatch',
name: 'micromatch',
},
],
name: 'dependencies',
name: '[dependencies]',
message: 'dependencies',
},
])
expect(prompt).toBeCalledWith(
@@ -149,19 +152,23 @@ test('interactively update', async () => {
choices: [
headerChoice,
{
name: chalk`is-negative 1.0.1 {redBright.bold 2.1.0} `,
message: chalk`is-negative 1.0.1 {redBright.bold 2.1.0} `,
value: 'is-negative',
name: 'is-negative',
},
{
name: chalk`is-positive 2.0.0 {redBright.bold 3.1.0} `,
message: chalk`is-positive 2.0.0 {redBright.bold 3.1.0} `,
value: 'is-positive',
name: 'is-positive',
},
{
name: chalk`micromatch 3.0.0 {redBright.bold 4.0.5} `,
message: chalk`micromatch 3.0.0 {redBright.bold 4.0.5} `,
value: 'micromatch',
name: 'micromatch',
},
],
name: 'dependencies',
name: '[dependencies]',
message: 'dependencies',
},
])
expect(prompt).toBeCalledWith(
@@ -314,11 +321,13 @@ test('interactively update should ignore dependencies from the ignoreDependencie
value: '',
},
{
name: chalk`micromatch 3.0.0 3.{yellowBright.bold 1.10} `,
message: chalk`micromatch 3.0.0 3.{yellowBright.bold 1.10} `,
value: 'micromatch',
name: 'micromatch',
},
],
name: 'dependencies',
name: '[dependencies]',
message: 'dependencies',
},
]
)