Skip to content

Commit 97cbe5e

Browse files
Fix multiselect validation crash (#1035)
Co-authored-by: pullfrog[bot] <226033991+pullfrog[bot]@users.noreply.github.com>
1 parent 586c93c commit 97cbe5e

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
varlock: patch
3+
---
4+
5+
Fix `encrypt --file` crashing when confirming values to encrypt

packages/varlock/src/cli/helpers/prompts.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ export interface MultiSelectOptions<Value> extends CommonOptions {
256256
cursorAt?: Value;
257257
}
258258
export const multiselect = <Value>(opts: MultiSelectOptions<Value>) => {
259+
const required = opts.required ?? true;
259260
const opt = (
260261
option: Option<Value>,
261262
state: 'inactive' | 'active' | 'selected' | 'active-selected' | 'submitted' | 'cancelled',
@@ -290,10 +291,10 @@ export const multiselect = <Value>(opts: MultiSelectOptions<Value>) => {
290291
input: opts.input,
291292
output: opts.output,
292293
initialValues: opts.initialValues,
293-
required: opts.required ?? true,
294+
required,
294295
cursorAt: opts.cursorAt,
295296
validate(selected: Array<Value> | undefined) {
296-
if (this.required && (!selected || selected.length === 0)) {
297+
if (required && (!selected || selected.length === 0)) {
297298
return `Please select at least one option.\n${color.reset(
298299
color.dim(
299300
`Press ${color.gray(color.bgWhite(color.inverse(' space ')))} to select, ${color.gray(
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { PassThrough } from 'node:stream';
2+
import { describe, expect, test } from 'vitest';
3+
4+
import { multiselect } from '../prompts';
5+
6+
describe('multiselect', () => {
7+
test('submits preselected values when Enter is pressed', async () => {
8+
const input = new PassThrough();
9+
const output = new PassThrough();
10+
const result = multiselect({
11+
message: 'Select values',
12+
options: [
13+
{ value: 'first' },
14+
{ value: 'second' },
15+
],
16+
initialValues: ['first', 'second'],
17+
input,
18+
output,
19+
});
20+
21+
input.write('\r');
22+
23+
await expect(result).resolves.toEqual(['first', 'second']);
24+
});
25+
});

0 commit comments

Comments
 (0)