From 8f1285404f08643886363fc151818a9fbd630239 Mon Sep 17 00:00:00 2001 From: Jan Nanista Date: Fri, 8 Dec 2023 14:23:58 -0800 Subject: [PATCH 1/3] chore: Ask user whether they want to continue --- packages/io-utils/README.md | 16 ++++++++++++++++ packages/io-utils/package.json | 1 + packages/io-utils/src/index.ts | 1 + packages/io-utils/src/stdio/index.ts | 1 + packages/io-utils/src/stdio/prompts.ts | 18 ++++++++++++++++++ 5 files changed, 37 insertions(+) create mode 100644 packages/io-utils/src/stdio/index.ts create mode 100644 packages/io-utils/src/stdio/prompts.ts diff --git a/packages/io-utils/README.md b/packages/io-utils/README.md index 11a8fdc68..e1cbe1c75 100644 --- a/packages/io-utils/README.md +++ b/packages/io-utils/README.md @@ -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); +``` diff --git a/packages/io-utils/package.json b/packages/io-utils/package.json index 6423fb388..cb2598d77 100644 --- a/packages/io-utils/package.json +++ b/packages/io-utils/package.json @@ -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", diff --git a/packages/io-utils/src/index.ts b/packages/io-utils/src/index.ts index b0899e4ef..0f39ca5b6 100644 --- a/packages/io-utils/src/index.ts +++ b/packages/io-utils/src/index.ts @@ -1 +1,2 @@ export * from './filesystem' +export * from './stdio' diff --git a/packages/io-utils/src/stdio/index.ts b/packages/io-utils/src/stdio/index.ts new file mode 100644 index 000000000..ba85b76eb --- /dev/null +++ b/packages/io-utils/src/stdio/index.ts @@ -0,0 +1 @@ +export * from './prompts' diff --git a/packages/io-utils/src/stdio/prompts.ts b/packages/io-utils/src/stdio/prompts.ts new file mode 100644 index 000000000..3e323a896 --- /dev/null +++ b/packages/io-utils/src/stdio/prompts.ts @@ -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 => { + 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 +} From f4c4c662f2130c5439757f166c843778e6dc87c5 Mon Sep 17 00:00:00 2001 From: Jan Nanista Date: Fri, 8 Dec 2023 14:24:44 -0800 Subject: [PATCH 2/3] chore: Fix typo --- packages/io-utils/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/io-utils/README.md b/packages/io-utils/README.md index e1cbe1c75..692008384 100644 --- a/packages/io-utils/README.md +++ b/packages/io-utils/README.md @@ -46,7 +46,7 @@ Returns `true` if specified filesystem `path` points to a file, `false` otherwis 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 +### Standard input/output utilities #### promptToContinue([message, defaultValue]) From 20494a2cfcb60c2fc74b8b7b1e68710c9d421e92 Mon Sep 17 00:00:00 2001 From: Jan Nanista Date: Fri, 8 Dec 2023 14:28:09 -0800 Subject: [PATCH 3/3] chore: Add granular exports to io-utils --- packages/io-utils/package.json | 9 ++++++++- packages/io-utils/tsup.config.ts | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/io-utils/package.json b/packages/io-utils/package.json index cb2598d77..805001717 100644 --- a/packages/io-utils/package.json +++ b/packages/io-utils/package.json @@ -14,6 +14,11 @@ "types": "./dist/index.d.ts", "require": "./dist/index.js", "import": "./dist/index.mjs" + }, + "./*": { + "types": "./dist/*.d.ts", + "require": "./dist/*.js", + "import": "./dist/*.mjs" } }, "main": "dist/index.js", @@ -29,11 +34,13 @@ "lint": "npx eslint '**/*.{js,ts,json}'", "test": "jest --passWithNoTests" }, + "dependencies": { + "prompts": "^2.4.2" + }, "devDependencies": { "@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", diff --git a/packages/io-utils/tsup.config.ts b/packages/io-utils/tsup.config.ts index b0e373950..158aab429 100644 --- a/packages/io-utils/tsup.config.ts +++ b/packages/io-utils/tsup.config.ts @@ -1,7 +1,7 @@ import { defineConfig } from 'tsup' export default defineConfig({ - entry: ['src/index.ts'], + entry: ['src/index.ts', 'src/filesystem/index.ts', 'src/stdio/index.ts'], outDir: './dist', clean: true, dts: true,