Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@inquirer/editor TS migration #1137

Merged
merged 3 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]
}