diff --git a/examples/changesets/index.ts b/examples/changesets/index.ts index 0dd6653..9006619 100644 --- a/examples/changesets/index.ts +++ b/examples/changesets/index.ts @@ -29,52 +29,54 @@ async function main() { { value: '@scope/x' }, { value: '@scope/y' }, { value: '@scope/z' }, - ] - } + ], + }, }), major: ({ results }) => { const packages = results.packages ?? []; return p.multiselect({ message: `Which packages should have a ${color.red('major')} bump?`, - options: packages.map(value => ({ value })), + options: packages.map((value) => ({ value })), required: false, - }) + }); }, minor: ({ results }) => { const packages = results.packages ?? []; const major = Array.isArray(results.major) ? results.major : []; - const possiblePackages = packages.filter(pkg => !major.includes(pkg)) + const possiblePackages = packages.filter((pkg) => !major.includes(pkg)); if (possiblePackages.length === 0) return; return p.multiselect({ message: `Which packages should have a ${color.yellow('minor')} bump?`, - options: possiblePackages.map(value => ({ value })), - required: false - }) + options: possiblePackages.map((value) => ({ value })), + required: false, + }); }, patch: async ({ results }) => { const packages = results.packages ?? []; const major = Array.isArray(results.major) ? results.major : []; const minor = Array.isArray(results.minor) ? results.minor : []; - const possiblePackages = packages.filter(pkg => !major.includes(pkg) && !minor.includes(pkg)); + const possiblePackages = packages.filter( + (pkg) => !major.includes(pkg) && !minor.includes(pkg) + ); if (possiblePackages.length === 0) return; let note = possiblePackages.join('\n'); - + p.log.step(`These packages will have a ${color.green('patch')} bump.\n${color.dim(note)}`); - return possiblePackages - } + return possiblePackages; + }, }, { - onCancel + onCancel, } ); const message = await p.text({ placeholder: 'Summary', - message: 'Please enter a summary for this change' - }) + message: 'Please enter a summary for this change', + }); if (p.isCancel(message)) { - return onCancel() + return onCancel(); } p.outro(`Changeset added! ${color.underline(color.cyan('.changeset/orange-crabs-sing.md'))}`); diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 25df057..db26c39 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,6 +1,6 @@ export { default as ConfirmPrompt } from './prompts/confirm'; -export { default as MultiSelectPrompt } from './prompts/multi-select'; export { default as GroupMultiSelectPrompt } from './prompts/group-multiselect'; +export { default as MultiSelectPrompt } from './prompts/multi-select'; export { default as PasswordPrompt } from './prompts/password'; export { default as Prompt, isCancel } from './prompts/prompt'; export type { State } from './prompts/prompt'; diff --git a/packages/core/src/prompts/group-multiselect.ts b/packages/core/src/prompts/group-multiselect.ts index 6918b1c..538fb27 100644 --- a/packages/core/src/prompts/group-multiselect.ts +++ b/packages/core/src/prompts/group-multiselect.ts @@ -1,6 +1,7 @@ import Prompt, { PromptOptions } from './prompt'; -interface GroupMultiSelectOptions extends PromptOptions> { +interface GroupMultiSelectOptions + extends PromptOptions> { options: Record; initialValues?: T['value'][]; required?: boolean; @@ -11,12 +12,12 @@ export default class GroupMultiSelectPrompt extends Pr cursor: number = 0; getGroupItems(group: string): T[] { - return this.options.filter(o => o.group === group); + return this.options.filter((o) => o.group === group); } isGroupSelected(group: string) { const items = this.getGroupItems(group); - return items.every(i => this.value.includes(i.value)); + return items.every((i) => this.value.includes(i.value)); } private toggleValue() { @@ -25,9 +26,11 @@ export default class GroupMultiSelectPrompt extends Pr const group = item.value; const groupedItems = this.getGroupItems(group); if (this.isGroupSelected(group)) { - this.value = this.value.filter((v: string) => groupedItems.findIndex(i => i.value === v) === -1); + this.value = this.value.filter( + (v: string) => groupedItems.findIndex((i) => i.value === v) === -1 + ); } else { - this.value = [...this.value, ...groupedItems.map(i => i.value)]; + this.value = [...this.value, ...groupedItems.map((i) => i.value)]; } this.value = Array.from(new Set(this.value)); } else { @@ -42,9 +45,9 @@ export default class GroupMultiSelectPrompt extends Pr super(opts, false); const { options } = opts; this.options = Object.entries(options).flatMap(([key, option]) => [ - { value: key, group: true, label: key }, - ...option.map((opt) => ({ ...opt, group: key })), -]) + { value: key, group: true, label: key }, + ...option.map((opt) => ({ ...opt, group: key })), + ]); this.value = [...(opts.initialValues ?? [])]; this.cursor = Math.max( this.options.findIndex(({ value }) => value === opts.cursorAt), diff --git a/packages/core/src/prompts/multi-select.ts b/packages/core/src/prompts/multi-select.ts index a812225..911dab2 100644 --- a/packages/core/src/prompts/multi-select.ts +++ b/packages/core/src/prompts/multi-select.ts @@ -16,7 +16,7 @@ export default class MultiSelectPrompt extends Prompt private toggleAll() { const allSelected = this.value.length === this.options.length; - this.value = allSelected ? [] : this.options.map(v => v.value); + this.value = allSelected ? [] : this.options.map((v) => v.value); } private toggleValue() { @@ -37,9 +37,9 @@ export default class MultiSelectPrompt extends Prompt ); this.on('key', (char) => { if (char === 'a') { - this.toggleAll() + this.toggleAll(); } - }) + }); this.on('cursor', (key) => { switch (key) { diff --git a/packages/prompts/src/index.ts b/packages/prompts/src/index.ts index 7b8a690..366fbb5 100644 --- a/packages/prompts/src/index.ts +++ b/packages/prompts/src/index.ts @@ -1,9 +1,9 @@ import { block, ConfirmPrompt, + GroupMultiSelectPrompt, isCancel, MultiSelectPrompt, - GroupMultiSelectPrompt, PasswordPrompt, SelectKeyPrompt, SelectPrompt, @@ -325,10 +325,12 @@ export const multiselect = [], Value extends Primi switch (this.state) { case 'submit': { - return `${title}${color.gray(S_BAR)} ${this.options - .filter(({ value }) => this.value.includes(value)) - .map((option) => opt(option, 'submitted')) - .join(color.dim(', ')) || color.dim('none')}`; + return `${title}${color.gray(S_BAR)} ${ + this.options + .filter(({ value }) => this.value.includes(value)) + .map((option) => opt(option, 'submitted')) + .join(color.dim(', ')) || color.dim('none') + }`; } case 'cancel': { const label = this.options @@ -400,8 +402,16 @@ export const groupMultiselect = [], Value extends ) => { const opt = ( option: Options[number], - state: 'inactive' | 'active' | 'selected' | 'active-selected' | 'group-active' | 'group-active-selected' | 'submitted' | 'cancelled', - options: Options = [] as any, + state: + | 'inactive' + | 'active' + | 'selected' + | 'active-selected' + | 'group-active' + | 'group-active-selected' + | 'submitted' + | 'cancelled', + options: Options = [] as any ) => { const label = option.label ?? String(option.value); const isItem = typeof option.group === 'string'; @@ -474,9 +484,14 @@ export const groupMultiselect = [], Value extends .join('\n'); return `${title}${color.yellow(S_BAR)} ${this.options .map((option, i, options) => { - const selected = this.value.includes(option.value) || (option.group === true && this.isGroupSelected(option.value)); + const selected = + this.value.includes(option.value) || + (option.group === true && this.isGroupSelected(option.value)); const active = i === this.cursor; - const groupActive = !active && typeof option.group === 'string' && this.options[this.cursor].value === option.group; + const groupActive = + !active && + typeof option.group === 'string' && + this.options[this.cursor].value === option.group; if (groupActive) { return opt(option, selected ? 'group-active-selected' : 'group-active', options); } @@ -493,9 +508,14 @@ export const groupMultiselect = [], Value extends default: { return `${title}${color.cyan(S_BAR)} ${this.options .map((option, i, options) => { - const selected = this.value.includes(option.value) || (option.group === true && this.isGroupSelected(option.value)); + const selected = + this.value.includes(option.value) || + (option.group === true && this.isGroupSelected(option.value)); const active = i === this.cursor; - const groupActive = !active && typeof option.group === 'string' && this.options[this.cursor].value === option.group; + const groupActive = + !active && + typeof option.group === 'string' && + this.options[this.cursor].value === option.group; if (groupActive) { return opt(option, selected ? 'group-active-selected' : 'group-active', options); } @@ -517,11 +537,14 @@ export const groupMultiselect = [], Value extends const strip = (str: string) => str.replace(ansiRegex(), ''); export const note = (message = '', title = '') => { const lines = `\n${message}\n`.split('\n'); - const len = Math.max( - lines.reduce((sum, ln) => { - ln = strip(ln); - return ln.length > sum ? ln.length : sum; - }, 0), strip(title).length) + 2; + const len = + Math.max( + lines.reduce((sum, ln) => { + ln = strip(ln); + return ln.length > sum ? ln.length : sum; + }, 0), + strip(title).length + ) + 2; const msg = lines .map( (ln) => @@ -641,7 +664,9 @@ export interface PromptGroupOptions { } export type PromptGroup = { - [P in keyof T]: (opts: { results: Partial> }) => void | Promise; + [P in keyof T]: (opts: { + results: Partial>; + }) => void | Promise; }; /**