enquirer-prompt-editor
Advanced tools
Comparing version 1.1.0 to 2.0.0
import { Prompt } from 'enquirer'; | ||
// Copy/paste since enquirer doesn't really have usable typings... | ||
interface BasePromptOptions { | ||
name: string | (() => string); | ||
type: string | (() => string); | ||
message: string | (() => string) | (() => Promise<string>); | ||
initial?: any; | ||
required?: boolean; | ||
format?(value: string): string | Promise<string>; | ||
result?(value: string): string | Promise<string>; | ||
skip?: ((state: object) => boolean | Promise<boolean>) | boolean; | ||
validate?( | ||
value: string | ||
): boolean | Promise<boolean> | string | Promise<string>; | ||
onSubmit?( | ||
name: string, | ||
value: any, | ||
prompt: Enquirer.Prompt | ||
): boolean | Promise<boolean>; | ||
onCancel?( | ||
name: string, | ||
value: any, | ||
prompt: Enquirer.Prompt | ||
): boolean | Promise<boolean>; | ||
stdin?: NodeJS.ReadStream; | ||
stdout?: NodeJS.WriteStream; | ||
} | ||
declare const EditorPrompt: Prompt; | ||
export = EditorPrompt; | ||
export interface EditorPromptOptions extends BasePromptOptions { | ||
extensions: string; | ||
} |
37
index.js
const { Prompt } = require('enquirer'); | ||
const { editAsync } = require('external-editor'); | ||
const { edit } = require('edit-briefly'); | ||
@@ -7,4 +7,17 @@ class Editor extends Prompt { | ||
super(options); | ||
this.value = options.initial || ''; | ||
this.cursorHide(); | ||
} | ||
async keypress(input, event = {}) { | ||
/** | ||
* Whitelist only certain key events. | ||
* This prevents accidental submits, when `return` is pressed. | ||
*/ | ||
if (!['cancel'].includes(event.name)) { | ||
return; | ||
} | ||
await super.keypress(input, event); | ||
} | ||
async hint() { | ||
@@ -38,2 +51,12 @@ const message = this.state.cancelled | ||
/** | ||
* Call this as a side effect, otherwhise we'll end in a endless loop | ||
* of submits if validation fails. | ||
* | ||
* submit -> invalid -> render -> ... | ||
*/ | ||
this.openEditor(); | ||
} | ||
async openEditor() { | ||
if (this.state.submitted) { | ||
@@ -43,10 +66,8 @@ return; | ||
editAsync(this.value, async (err, text) => { | ||
if (err) { | ||
await this.cancel(err); | ||
} else { | ||
this.value = text; | ||
await this.submit(); | ||
} | ||
const text = await edit({ | ||
contents: this.value, | ||
extension: this.options.extension || '', | ||
}); | ||
this.value = text; | ||
await this.submit(); | ||
} | ||
@@ -53,0 +74,0 @@ } |
{ | ||
"name": "enquirer-prompt-editor", | ||
"version": "1.1.0", | ||
"description": "Opens your text editor and waits for you to save your input during a prompt.", | ||
"version": "2.0.0", | ||
"description": "Use your text editor as prompt input.", | ||
"license": "MIT", | ||
"author": "Sebastian Sebald (https://github.com/sebald)", | ||
"repository": "sebald/enquirer-prompt-editor", | ||
"bugs": { | ||
"url": "https://github.com/sebald/enquirer-prompt-editor/issues" | ||
}, | ||
"keywords": [ | ||
@@ -29,4 +26,4 @@ "cli", | ||
"dependencies": { | ||
"enquirer": "^2.3.0", | ||
"external-editor": "^3.0.3" | ||
"edit-briefly": "^1.0.0", | ||
"enquirer": "^2.3.0" | ||
}, | ||
@@ -33,0 +30,0 @@ "scripts": { |
@@ -23,2 +23,3 @@ # enquirer-prompt-editor | ||
message: 'Please write a short bio of at least 3 lines.', | ||
extension: 'md', | ||
validate: text => { | ||
@@ -31,3 +32,4 @@ if (text.split('\n').length < 2) { | ||
}) | ||
.then(answers => console.log(answers)); | ||
.then(answers => console.log(answers)) | ||
.catch(err => console.log(err)); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5224
93
34
2
+ Addededit-briefly@^1.0.0
+ Added@types/node@10.12.21(transitive)
+ Addedcrypto-random-string@1.0.0(transitive)
+ Addededit-briefly@1.0.0(transitive)
+ Addedtemp-dir@1.0.0(transitive)
+ Addedtempy@0.2.1(transitive)
+ Addedunique-string@1.0.0(transitive)
- Removedexternal-editor@^3.0.3
- Removedchardet@0.7.0(transitive)
- Removedexternal-editor@3.1.0(transitive)
- Removediconv-lite@0.4.24(transitive)
- Removedos-tmpdir@1.0.2(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedtmp@0.0.33(transitive)