From 593ce309b0282f213e5f60f3e07b7d3982772c03 Mon Sep 17 00:00:00 2001 From: Ojas Inamdar Date: Mon, 11 Jul 2022 20:13:42 +0530 Subject: [PATCH 1/3] feat: @inquirer/editor migrated to TS --- packages/editor/{demo.js => demo.ts} | 2 +- packages/editor/package.json | 9 ++++++++- packages/editor/{index.js => src/index.ts} | 16 +++++++++++----- packages/editor/tsconfig.json | 7 +++++++ 4 files changed, 27 insertions(+), 7 deletions(-) rename packages/editor/{demo.js => demo.ts} (89%) rename packages/editor/{index.js => src/index.ts} (77%) create mode 100644 packages/editor/tsconfig.json diff --git a/packages/editor/demo.js b/packages/editor/demo.ts similarity index 89% rename from packages/editor/demo.js rename to packages/editor/demo.ts index ea7eddd7d..c8e802cf6 100644 --- a/packages/editor/demo.js +++ b/packages/editor/demo.ts @@ -1,4 +1,4 @@ -import editor from './index.js'; +import editor from './src/index.js'; (async () => { const answer = await editor({ diff --git a/packages/editor/package.json b/packages/editor/package.json index 10452aaf2..9d871b41c 100644 --- a/packages/editor/package.json +++ b/packages/editor/package.json @@ -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", @@ -19,6 +23,9 @@ "chalk": "^5.0.1", "external-editor": "^3.0.3" }, + "scripts": { + "tsc": "tsc" + }, "publishConfig": { "access": "public" } diff --git a/packages/editor/index.js b/packages/editor/src/index.ts similarity index 77% rename from packages/editor/index.js rename to packages/editor/src/index.ts index da9ca126f..be5e4c7c9 100644 --- a/packages/editor/index.js +++ b/packages/editor/src/index.ts @@ -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((config, done) => { + const [status, setStatus] = useState('pending'); + const [value, setValue] = useState(config.default || ''); + const [errorMsg, setError] = useState(undefined); useKeypress(async (key, rl) => { // Ignore keypress while our prompt is doing other processing. @@ -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); diff --git a/packages/editor/tsconfig.json b/packages/editor/tsconfig.json new file mode 100644 index 000000000..6a62dbc48 --- /dev/null +++ b/packages/editor/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "./dist" + }, + "include": ["./src"] +} From 6aea51e5fa684ec16d37ecf011511affaaea95af Mon Sep 17 00:00:00 2001 From: Ojas Inamdar Date: Mon, 11 Jul 2022 20:20:48 +0530 Subject: [PATCH 2/3] docs: Added default param to editor README --- packages/editor/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/editor/README.md b/packages/editor/README.md index f705b29ef..087726b08 100644 --- a/packages/editor/README.md +++ b/packages/editor/README.md @@ -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` | 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. | From 48309c7ce87851c43c508da79319482ec726bc2a Mon Sep 17 00:00:00 2001 From: Simon Boudrias Date: Tue, 12 Jul 2022 11:23:30 -0400 Subject: [PATCH 3/3] Keep MD table consistent In case other systems aren't using the same parser as GH which could lead to a broken table layout. --- packages/editor/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/editor/README.md b/packages/editor/README.md index 087726b08..55e68cf54 100644 --- a/packages/editor/README.md +++ b/packages/editor/README.md @@ -25,7 +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 +| default | `string` | no | Default value which will automatically be present in the editor | | validate | `string => boolean \| string \| Promise` | 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. |