npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

console-tasks

v0.1.13

Published

ConsoleTasks is a NodeJs library for managing concurrent tasks in the console. It provides a clean and intuitive API for creating, executing, and monitoring tasks with a highly configurable and visually appealing progress display.

Downloads

350

Readme

ConsoleTasks

ConsoleTasks is a flexible NodeJs library for managing and displaying concurrent or sequential tasks in the console. It provides a clean and intuitive API for creating, executing, and monitoring tasks with a highly configurable and visually appealing progress display.

Features

  • Concurrent and sequential task execution
  • A task completely owns its rendering in the console UI
  • Task status updates (running, successful, failed, or cancelled)
  • Easily customizable spinners for all or specific tasks
  • Support for dynamic task creation
  • Graceful handling of task cancellation
  • TypeScript support for enhanced type safety

Installation

To install ConsoleTasks, use npm:

npm install console-tasks

Basic Usage

Here's a basic example of how to use ConsoleTasks:

import { TaskManager } from 'console-tasks';

// Define a task
const exampleTask = {
  initialMessage: "Starting example task...",
  task: async (updateFn, abortSignal) => {
    await new Promise((resolve) => setTimeout(resolve, 2000)); // Simulate async work
    updateFn("Example task in progress...");
    await new Promise((resolve) => setTimeout(resolve, 2000)); // More async work
    updateFn("Example task completed!");
  },
};

// Create a TaskManager instance
const taskManager = TaskManager.getInstance({ title: " Example " });

// Add and execute the tasks
taskManager.run(exampleTask, exampleTask, exampleTask);

// [Optional] Wait for all tasks to complete
await taskManager.await();
console.log("All tasks completed!");

Which results in this:

Basic Demo

Demo showcasing more of the features

Basic Demo

API Reference

TaskManager

The TaskManager class is responsible for managing and executing tasks.

Methods

  • static getInstance(options?: TaskManagerOptions): TaskManager

    • Gets or creates a singleton instance of TaskManager.
    • options: Configuration options for the TaskManager.
  • run(...tasks: Task[]): number[]

    • Adds new tasks to the TaskManager and starts executing them immediately.
    • Returns an array of task IDs for each of the tasks.
  • await(): Promise<void>

    • Waits for all tasks to complete.
    • Returns a promise that resolves when all tasks are completed.
  • stop(): void

    • Stops the spinner and renders the final state.
  • update(taskId: number, message: string): void

    • Updates the message for a specific task.
  • onStatusChange(taskId: number, handler: (newStatus: SpinnerStatus, data?: any) => void): void

    • Registers a handler for status changes of a specific task.

TaskManagerOptions

  • stream?: Writable: The output stream (default: process.stdout).
  • title?: string: The title to display above the tasks.
  • customStatusSymbols?: Partial<StatusSymbol>: Custom status symbols for tasks.
  • keepAlive?: boolean: Whether to keep the task manager running after all tasks are completed.
  • taskPrefix?: (taskSeparator: string, statusSymbol: string) => string: Custom task prefix function.
  • stopAndRecreate?: boolean: Whether to recreate the TaskManager if an instance exists already.
  • headerFormatter?: (title: string) => string: Function to format / style the header.

Task

The Task interface represents a task to be executed by the TaskManager.

Properties

  • initialMessage: string: The initial message to be displayed.
  • task: (updateFn: (msg: string) => void, signal: AbortSignal) => Promise<Task | void | any>: The actual task function.
  • disabled?: boolean: Whether the task is disabled (default: false).
  • isHidden?: boolean: When true, the task is run but its UI is not rendered.
  • statusSymbol?: string | Partial<StatusSymbol>: Custom status symbol to display instead of the spinner.
  • index?: number: Optional index for positioning the task in the console output.

Helper Functions

  • addMessage(msg: string, statusSymbol?: string): void

    • Adds a simple message task to the TaskManager.
  • taskify<T>(fn: () => Promise<T>, title?: string): Promise<T>

    • Converts a function into a task and executes it.
    • Returns a promise that resolves with the result of the function.
  • sequence(...tasks: Task[]): Task

    • Creates a task that executes a sequence of tasks.
    • Returns a new Task that represents the sequence of tasks.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.