@inquirer/editor
Advanced tools
| import { type IFileOptions } from '@inquirer/external-editor'; | ||
| import { type Theme } from '@inquirer/core'; | ||
| import type { PartialDeep } from '@inquirer/type'; | ||
| type EditorTheme = { | ||
| validationFailureMode: 'keep' | 'clear'; | ||
| style: { | ||
| waitingMessage: (enterKey: string) => string; | ||
| }; | ||
| }; | ||
| type EditorConfig = { | ||
| message: string; | ||
| default?: string; | ||
| postfix?: string; | ||
| waitForUserInput?: boolean; | ||
| validate?: (value: string) => boolean | string | Promise<string | boolean>; | ||
| file?: IFileOptions; | ||
| theme?: PartialDeep<Theme<EditorTheme>>; | ||
| }; | ||
| declare const _default: import("@inquirer/type").Prompt<string, EditorConfig>; | ||
| export default _default; |
| import { editAsync } from '@inquirer/external-editor'; | ||
| import { createPrompt, useEffect, useState, useKeypress, usePrefix, isEnterKey, makeTheme, } from '@inquirer/core'; | ||
| const editorTheme = { | ||
| validationFailureMode: 'keep', | ||
| style: { | ||
| waitingMessage: (enterKey) => `Press ${enterKey} to launch your preferred editor.`, | ||
| }, | ||
| }; | ||
| export default createPrompt((config, done) => { | ||
| const { waitForUserInput = true, file: { postfix = config.postfix ?? '.txt', ...fileProps } = {}, validate = () => true, } = config; | ||
| const theme = makeTheme(editorTheme, config.theme); | ||
| const [status, setStatus] = useState('idle'); | ||
| const [value = '', setValue] = useState(config.default); | ||
| const [errorMsg, setError] = useState(); | ||
| const prefix = usePrefix({ status, theme }); | ||
| function startEditor(rl) { | ||
| rl.pause(); | ||
| const editCallback = async (error, answer) => { | ||
| rl.resume(); | ||
| if (error) { | ||
| setError(error.toString()); | ||
| } | ||
| else { | ||
| setStatus('loading'); | ||
| const finalAnswer = answer ?? ''; | ||
| const isValid = await validate(finalAnswer); | ||
| if (isValid === true) { | ||
| setError(undefined); | ||
| setStatus('done'); | ||
| done(finalAnswer); | ||
| } | ||
| else { | ||
| if (theme.validationFailureMode === 'clear') { | ||
| setValue(config.default); | ||
| } | ||
| else { | ||
| setValue(finalAnswer); | ||
| } | ||
| setError(isValid || 'You must provide a valid value'); | ||
| setStatus('idle'); | ||
| } | ||
| } | ||
| }; | ||
| editAsync(value, (error, answer) => void editCallback(error, answer), { | ||
| postfix, | ||
| ...fileProps, | ||
| }); | ||
| } | ||
| useEffect((rl) => { | ||
| if (!waitForUserInput) { | ||
| startEditor(rl); | ||
| } | ||
| }, []); | ||
| useKeypress((key, rl) => { | ||
| // Ignore keypress while our prompt is doing other processing. | ||
| if (status !== 'idle') { | ||
| return; | ||
| } | ||
| if (isEnterKey(key)) { | ||
| startEditor(rl); | ||
| } | ||
| }); | ||
| const message = theme.style.message(config.message, status); | ||
| let helpTip = ''; | ||
| if (status === 'idle') { | ||
| const enterKey = theme.style.key('enter'); | ||
| helpTip = theme.style.help(theme.style.waitingMessage(enterKey)); | ||
| } | ||
| let error = ''; | ||
| if (errorMsg) { | ||
| error = theme.style.error(errorMsg); | ||
| } | ||
| return [[prefix, message, helpTip].filter(Boolean).join(' '), error]; | ||
| }); |
+5
-5
| { | ||
| "name": "@inquirer/editor", | ||
| "version": "5.0.9", | ||
| "version": "5.0.10", | ||
| "description": "Inquirer multiline editor prompt", | ||
@@ -70,5 +70,5 @@ "keywords": [ | ||
| "dependencies": { | ||
| "@inquirer/core": "^11.1.6", | ||
| "@inquirer/external-editor": "^2.0.3", | ||
| "@inquirer/type": "^4.0.3" | ||
| "@inquirer/core": "^11.1.7", | ||
| "@inquirer/external-editor": "^2.0.4", | ||
| "@inquirer/type": "^4.0.4" | ||
| }, | ||
@@ -91,3 +91,3 @@ "devDependencies": { | ||
| "types": "./dist/index.d.ts", | ||
| "gitHead": "1ce03199b82b4a5fb6f7c97ce374c6da5087444f" | ||
| "gitHead": "b218fcc4afe888a58957aa78c9a032f9bd2d60cb" | ||
| } |
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
11262
42.43%5
66.67%94
Infinity%0
-100%Updated
Updated