Skip to content
Merged
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
33 changes: 33 additions & 0 deletions packages/prompts/test/__snapshots__/messages.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`cancel > an explicit empty message still renders nothing 1`] = `
[
"└ 

",
]
`;

exports[`cancel > renders the cancel message set with updateSettings 1`] = `
[
"└ Operation cancelled

",
]
`;

exports[`cancel > renders the default cancel message when called without a message 1`] = `
[
"└ Canceled

",
]
`;

exports[`cancel > renders the given message 1`] = `
[
"└ Installation canceled

",
]
`;
45 changes: 45 additions & 0 deletions packages/prompts/test/messages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { settings, updateSettings } from '@clack/core';
import { afterEach, beforeEach, describe, expect, test } from 'vitest';
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 } });
});

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

expect(output.buffer).toMatchSnapshot();
});

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

expect(output.buffer).toMatchSnapshot();
});

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

prompts.cancel(undefined, { output });

expect(output.buffer).toMatchSnapshot();
});

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

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

expect(output.buffer).toMatchSnapshot();
});
});
Loading