Skip to content

Commit

Permalink
chore: Ask user whether they want to continue
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Dec 8, 2023
1 parent 71a4b7f commit 8f12854
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/io-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,19 @@ Returns `true` if specified filesystem `path` points to a file, `false` otherwis
#### isReadable(path)

Returns `true` if specified filesystem `path` can be read by the current user, `false` otherwise. Does not throw if `path` does not exist on the filesystem, instead returns `false`

### Standdard input/output utilities

#### promptToContinue([message, defaultValue])

Asks the user whether they want to continue and reads the input from the CLI standard input. By default the question displayed is `Do you want to continue?` and the default response is `yes`

```typescript
const goahead = await promptToContinue();

// To ask a different question
const goahead = await promptToContinue("Are you sure?");

// To default the response to false, good for important and unsafe decisions
const goahead = await promptToContinue("Are you sure?", false);
```
1 change: 1 addition & 0 deletions packages/io-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@types/jest": "^29.5.10",
"fast-check": "^3.14.0",
"jest": "^29.7.0",
"prompts": "^2.4.2",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tslib": "~2.6.2",
Expand Down
1 change: 1 addition & 0 deletions packages/io-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './filesystem'
export * from './stdio'
1 change: 1 addition & 0 deletions packages/io-utils/src/stdio/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './prompts'
18 changes: 18 additions & 0 deletions packages/io-utils/src/stdio/prompts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import assert from 'assert'
import prompts from 'prompts'

export const promptToContinue = async (
message: string = 'Do you want to continue?',
defaultValue = true
): Promise<boolean> => {
const { value } = await prompts({
type: 'confirm',
name: 'value',
message,
initial: defaultValue,
})

assert(typeof value === 'boolean', `Invariant error: Expected a boolean response, got ${value}`)

return value
}

0 comments on commit 8f12854

Please sign in to comment.