From 340f6132ecb23d7f9bd8a3c92fe8ea7cc758054f Mon Sep 17 00:00:00 2001 From: Bilal Meddah Date: Mon, 4 Nov 2024 17:04:19 +0100 Subject: [PATCH] Refactor editor prompt to use file options and enhance testing - Updated editor prompt configuration to accept `file` options (`IFileOptions`) instead of only `postfix`, allowing for more flexible file handling within the editor's prompt. - Refactored test cases in `editor.test.ts` to accommodate the new file option structure. --- packages/editor/editor.test.ts | 5 ++++- packages/editor/src/index.ts | 15 +++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/editor/editor.test.ts b/packages/editor/editor.test.ts index d26ace2d3..009f95fe1 100644 --- a/packages/editor/editor.test.ts +++ b/packages/editor/editor.test.ts @@ -24,6 +24,7 @@ describe('editor prompt', () => { it('open editor after pressing enter', async () => { const { answer, events, getScreen } = await render(editor, { message: 'Add a description', + file: { postfix: '.txt' }, }); expect(getScreen()).toMatchInlineSnapshot( @@ -57,7 +58,9 @@ describe('editor prompt', () => { const { answer, events, getScreen } = await render(editor, { message: 'Add a description', default: 'default description', - postfix: '.md', + file: { + postfix: '.md', + }, }); expect(editAsync).not.toHaveBeenCalled(); diff --git a/packages/editor/src/index.ts b/packages/editor/src/index.ts index 8d3a9a020..292253167 100644 --- a/packages/editor/src/index.ts +++ b/packages/editor/src/index.ts @@ -1,5 +1,5 @@ import { AsyncResource } from 'node:async_hooks'; -import { editAsync } from 'external-editor'; +import { editAsync, IFileOptions } from 'external-editor'; import { createPrompt, useEffect, @@ -16,14 +16,18 @@ import type { PartialDeep, InquirerReadline } from '@inquirer/type'; type EditorConfig = { message: string; default?: string; - postfix?: string; waitForUseInput?: boolean; validate?: (value: string) => boolean | string | Promise; theme?: PartialDeep; + file?: IFileOptions; }; export default createPrompt((config, done) => { - const { waitForUseInput = true, postfix = '.txt', validate = () => true } = config; + const { + waitForUseInput = true, + file: { postfix = '.txt', ...fileProps } = {}, + validate = () => true, + } = config; const theme = makeTheme(config.theme); const [status, setStatus] = useState('idle'); @@ -57,7 +61,10 @@ export default createPrompt((config, done) => { }, ); - editAsync(value, (error, answer) => void editCallback(error, answer), { postfix }); + editAsync(value, (error, answer) => void editCallback(error, answer), { + postfix, + ...fileProps, + }); } useEffect((rl) => {