Skip to content

Commit

Permalink
@inquirer/editor TS migration (#1137)
Browse files Browse the repository at this point in the history
* feat: @inquirer/editor migrated to TS

* docs: Added default param to editor README

* Keep MD table consistent

In case other systems aren't using the same parser as GH which could lead to a broken table layout.

Co-authored-by: Simon Boudrias <admin@simonboudrias.com>
  • Loading branch information
TDP17 and SBoudrias authored Jul 12, 2022
1 parent 6b76a9e commit cd3dc72
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const answer = await editor({
| Property | Type | Required | Description |
| -------- | --------- | -------- | ------------------------------ |
| message | `string` | yes | The question to ask |
| default | `string` | no | Default value which will automatically be present in the editor |
| validate | `string => boolean \| string \| Promise<string \| boolean>` | no | On submit, validate the content. When returning a string, it'll be used as the error message displayed to the user. Note: returning a rejected promise, we'll assume a code error happened and crash. |
| postfix | `string` | no (default to `.txt`) | The postfix of the file being edited. Adding this will add color highlighting to the file content in most editors. |

Expand Down
2 changes: 1 addition & 1 deletion packages/editor/demo.js → packages/editor/demo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import editor from './index.js';
import editor from './src/index.js';

(async () => {
const answer = await editor({
Expand Down
9 changes: 8 additions & 1 deletion packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
"type": "module",
"version": "0.0.14-alpha.0",
"description": "Inquirer multiline editor prompt",
"main": "index.js",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist/"
],
"repository": "SBoudrias/Inquirer.js",
"keywords": [
"cli",
Expand All @@ -19,6 +23,9 @@
"chalk": "^5.0.1",
"external-editor": "^3.0.3"
},
"scripts": {
"tsc": "tsc"
},
"publishConfig": {
"access": "public"
}
Expand Down
16 changes: 11 additions & 5 deletions packages/editor/index.js → packages/editor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import {
useKeypress,
usePrefix,
isEnterKey,
AsyncPromptConfig,
} from '@inquirer/core';

export default createPrompt((config, done) => {
const [status, setStatus] = useState('pending');
const [value, setValue] = useState(config.default || '');
const [errorMsg, setError] = useState();
type EditorConfig = AsyncPromptConfig & {
default?: string;
postfix?: string;
};

export default createPrompt<string, EditorConfig>((config, done) => {
const [status, setStatus] = useState<string>('pending');
const [value, setValue] = useState<string>(config.default || '');
const [errorMsg, setError] = useState<string | undefined>(undefined);

useKeypress(async (key, rl) => {
// Ignore keypress while our prompt is doing other processing.
Expand All @@ -26,7 +32,7 @@ export default createPrompt((config, done) => {
async (error, answer) => {
rl.resume();
if (error) {
setError(error);
setError(error.toString());
} else {
setStatus('loading');
const isValid = await config.validate(answer);
Expand Down
7 changes: 7 additions & 0 deletions packages/editor/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./src"]
}

0 comments on commit cd3dc72

Please sign in to comment.