Coder Social home page Coder Social logo

html-simple-dialogs's Introduction

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!

html-simple-dialogs's People

Contributors

anseki avatar jcbhmr avatar paulmillr avatar grahack avatar dependabot[bot] avatar

Stargazers

Martin Löper avatar

Watchers

 avatar

html-simple-dialogs's Issues

Use sync readline

Basically just take a whole lotta code from the Node.js project https://github.com/nodejs/node/blob/v19.8.1/lib/internal/readline/interface.js#L582 and make it use sync reads, not ondata events.

That's way more work than I just made it sound.

An easier thing to do might be to simulate a tighter eventloop like:

const cbs = [];
let returnValue;

cbs.push((buffer) => {
  if (buffer[0] === "\n") {
    returnValue = buffer;
    return;
  }
});

while (!returnValue) {
  readSync(fd, buffer);
  for (const cb of cbs) {
    cb(buffer);
  }
}
return returnValue;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.