Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/cancel-uses-settings-message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clack/prompts": patch
---

`cancel()` called without a message now shows `settings.messages.cancel`, so a message set with `updateSettings({ messages: { cancel } })` is displayed.
7 changes: 4 additions & 3 deletions packages/prompts/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { type CommonOptions, S_BAR, S_BAR_END, S_BAR_START } from './common.js';
* The `cancel` function defines an interruption of an interaction
* and therefore its end.
*
* @param title Optional closing message to be displayed.
* @param title Optional closing message to be displayed. Defaults to
* `settings.messages.cancel`, which can be changed with `updateSettings`.
* @param opts Additional configuration options.
*
* @see https://bomb.sh/docs/clack/packages/prompts/#cancel
Expand All @@ -21,11 +22,11 @@ import { type CommonOptions, S_BAR, S_BAR_END, S_BAR_START } from './common.js';
* process.exit(1);
* ```
*/
export const cancel = (message = '', opts?: CommonOptions) => {
export const cancel = (message?: string, opts?: CommonOptions) => {
const output: Writable = opts?.output ?? process.stdout;
const hasGuide = opts?.withGuide ?? settings.withGuide;
const prefix = hasGuide ? `${styleText('gray', S_BAR_END)} ` : '';
output.write(`${prefix}${styleText('red', message)}\n\n`);
output.write(`${prefix}${styleText('red', message ?? settings.messages.cancel)}\n\n`);
};

/**
Expand Down
50 changes: 50 additions & 0 deletions packages/prompts/test/messages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { styleText } from 'node:util';
import { settings, updateSettings } from '@clack/core';
import { afterEach, beforeEach, describe, expect, test } from 'vitest';
import { S_BAR_END } from '../src/common.js';
import * as prompts from '../src/index.js';
import { MockWritable } from './test-utils.js';

describe('cancel', () => {
let output: MockWritable;
const defaultCancel = settings.messages.cancel;

beforeEach(() => {
output = new MockWritable();
});

afterEach(() => {
updateSettings({ messages: { cancel: defaultCancel } });
});

const line = (message: string) =>
`${styleText('gray', S_BAR_END)} ${styleText('red', message)}\n\n`;

test('renders the given message', () => {
prompts.cancel('Installation canceled', { output });

expect(output.buffer.join('')).toBe(line('Installation canceled'));
});

test('renders the default cancel message when called without a message', () => {
prompts.cancel(undefined, { output });

expect(output.buffer.join('')).toBe(line(defaultCancel));
});

test('renders the cancel message set with updateSettings', () => {
updateSettings({ messages: { cancel: 'Operation cancelled' } });

prompts.cancel(undefined, { output });

expect(output.buffer.join('')).toBe(line('Operation cancelled'));
});

test('an explicit empty message still renders nothing', () => {
updateSettings({ messages: { cancel: 'Operation cancelled' } });

prompts.cancel('', { output });

expect(output.buffer.join('')).toBe(line(''));
});
});
Loading