Skip to content

Commit

Permalink
Migrate @inquirer/confirm to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Jun 18, 2022
1 parent ce1e653 commit 9edebf0
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 54 deletions.
16 changes: 0 additions & 16 deletions packages/confirm/demo.js

This file was deleted.

18 changes: 18 additions & 0 deletions packages/confirm/demo.ts
@@ -0,0 +1,18 @@
import confirm from './src/index.js';

(async () => {
console.log(
'Answer:',
await confirm({
message: 'Confirm?',
})
);

console.log(
'Answer:',
await confirm({
message: 'Confirm with default to no?',
default: false,
})
);
})();
36 changes: 0 additions & 36 deletions packages/confirm/index.js

This file was deleted.

6 changes: 5 additions & 1 deletion packages/confirm/package.json
Expand Up @@ -3,7 +3,8 @@
"type": "module",
"version": "0.0.19-alpha.0",
"description": "Inquirer confirm prompt",
"main": "index.js",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"repository": "SBoudrias/Inquirer.js",
"keywords": [
"cli",
Expand All @@ -22,6 +23,9 @@
"@inquirer/input": "^0.0.20-alpha.0",
"chalk": "^5.0.1"
},
"scripts": {
"tsc": "tsc"
},
"publishConfig": {
"access": "public"
}
Expand Down
38 changes: 38 additions & 0 deletions packages/confirm/src/index.ts
@@ -0,0 +1,38 @@
import chalk from 'chalk';
import {
createPrompt,
useState,
useKeypress,
isEnterKey,
usePrefix,
} from '@inquirer/core';

export default createPrompt<boolean, { message: string; default?: boolean }>(
(config, done) => {
const [status, setStatus] = useState('pending');
const [value, setValue] = useState('');
const prefix = usePrefix();

useKeypress((key, rl) => {
if (isEnterKey(key)) {
const answer = value ? /^y(es)?/i.test(value) : config.default !== false;
setValue(answer ? 'yes' : 'no');
setStatus('done');
done(answer);
} else {
setValue(rl.line);
}
});

let formattedValue = value;
let defaultValue = '';
if (status === 'done') {
formattedValue = chalk.cyan(value);
} else {
defaultValue = chalk.dim(config.default === false ? ' (y/N)' : ' (Y/n)');
}

const message = chalk.bold(config.message);
return `${prefix} ${message}${defaultValue} ${formattedValue}`;
}
);
7 changes: 7 additions & 0 deletions packages/confirm/tsconfig.json
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./src"]
}
2 changes: 1 addition & 1 deletion packages/core/src/lib/prefix.ts
Expand Up @@ -4,7 +4,7 @@ import { useState, useEffect } from '../index.js';

const spinner = spinners.dots;

export function usePrefix(isLoading: boolean): string {
export function usePrefix(isLoading: boolean = false): string {
const [tick, setTick] = useState(0);

useEffect((): void | (() => unknown) => {
Expand Down

0 comments on commit 9edebf0

Please sign in to comment.