Skip to content

Commit

Permalink
Refactor editor prompt to use file options and enhance testing
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
bilalesi committed Nov 4, 2024
1 parent fb143e2 commit 340f613
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 4 additions & 1 deletion packages/editor/editor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand Down
15 changes: 11 additions & 4 deletions packages/editor/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AsyncResource } from 'node:async_hooks';
import { editAsync } from 'external-editor';
import { editAsync, IFileOptions } from 'external-editor';
import {
createPrompt,
useEffect,
Expand All @@ -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<string | boolean>;
theme?: PartialDeep<Theme>;
file?: IFileOptions;
};

export default createPrompt<string, EditorConfig>((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<Status>('idle');
Expand Down Expand Up @@ -57,7 +61,10 @@ export default createPrompt<string, EditorConfig>((config, done) => {
},
);

editAsync(value, (error, answer) => void editCallback(error, answer), { postfix });
editAsync(value, (error, answer) => void editCallback(error, answer), {
postfix,
...fileProps,
});
}

useEffect((rl) => {
Expand Down

0 comments on commit 340f613

Please sign in to comment.