Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate @inquirer/confirm to TypeScript
- Loading branch information
Showing
7 changed files
with
69 additions
and
54 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}) | ||
); | ||
})(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./dist" | ||
}, | ||
"include": ["./src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters