Skip to content

web4more/html-simple-dialogs

Repository files navigation

Node.js simple dialogs polyfill

🗨️ prompt() and friends for Node.js

Installation

npm

You can install this package from npm, or any npm-compatible package manager like Yarn or pnpm.

npm install @jcbhmr/html-simple-dialogs

🛑 This polyfill only works with Node.js. If you're using Deno or a browser, these functions are already provided for you.

⚠️ We use node:readline/promises which was added in Node.js v17.0.0.

Usage

Node.js Terminal

alert(), prompt(), and confirm() will synchronously block the main thread. This behaviour is similar to the corresponding implementation of these functions in the browser. If the current environment isn't suited to a UI prompt (e.g. you're in CI or STDIN isn't a TTY), these functions will behave flawlessly and equivalent to their browser counterparts as though popups were disabled.

These functions are especially useful when you're writing a very basic CLI app and you don't need a bunch of fancy parsing schemas to handle one or two input fields for strings. However, if you are looking for a CLI framework for a more advanced CLI app, you should check out one of the popular CLI tools for Node.js.

Example

This example will prompt you for your name, and then ask if you're a developer. If you are, it will pause and alert() you, otherwise, it will log to the console and exit immediately.

import "@jcbhmr/html-simple-dialogs";

const name = prompt("What's your name?");
if (confirm("Are you a developer?")) {
  alert(`Hello, ${name}. You're a developer.`);
} else {
  console.log(`Hello, ${name}. You're not a developer.`);
}

Development

TypeScript Vite Vitest

This package is written in TypeScript and uses Vite for development. To get started, you can use GitHub Codespaces to open this repository in a browser-based VS Code instance, or you can clone the repository and run npm install to install the dependencies.

To start up the dev-loop and run the tests, you can run npm start. To run only the tests, you can run npm test. Note that the demo script is only run in non-CI environments from npm test. It's recommended to run npm test before commiting or pushing changes. It also formats your code with Prettier!

📚 For more information about how this package works and more dev-focused docs, check out the wiki!