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

taskl

v1.0.4

Published

A useful tool for managing asynchronous tasks and logs in CLI

Downloads

1,628

Readme

Taskl

Taskl is a useful tool for effectively controlling asynchronous tasks and managing logs in CLI applications.

npm license

taskl

Installation

You can install Taskl using your preferred package manager:

npm

npm install taskl

Yarn

yarn add taskl

pnpm

pnpm add taskl

Bun

bun add taskl

Usage

To use Taskl in your project, follow these steps:

1. Import the Necessary Components from Taskl

First, import Taskl, Task, and TasklOptions from the taskl package.

import { Taskl, Task, TasklOptions } from 'taskl';

For JavaScript users, you can use require instead:

const { Taskl, Task, TasklOptions } = require('taskl');

2. Define Your Tasks

Create an array of tasks you want to execute. Each task should have a text description and a run function that returns a Promise<void>.

const tasks: Task[] = [
  {
    text: 'Connecting to the database',
    run: async () => {
      // Your actual logic for connecting to the database
      await connectToDatabase();
    },
  },
  {
    text: 'Starting the API server',
    run: async () => {
      // Your actual logic for starting the server
      await startApiServer();
    },
  }
];

For JavaScript users:

const tasks = [
  {
    text: 'Connecting to the database',
    run: async () => {
      // Your actual logic for connecting to the database
      await connectToDatabase();
    },
  },
  {
    text: 'Starting the API server',
    run: async () => {
      // Your actual logic for starting the server
      await startApiServer();
    },
  }
];

Task Interface:

  • text: A description of the task.
  • run: An asynchronous function that performs the task.

Tip: Ensure that each run function handles its own errors appropriately to allow Taskl to manage the task flow smoothly. For example:

const tasks = [
  {
    text: 'Connecting to the database',
    run: async () => {
      try {
        await connectToDatabase();
      } catch (error) {
        console.error('Database connection failed:', error);
        throw error; // Re-throw to let Taskl handle the failure
      }
    },
  },
  // ... other tasks
];

3. Set Up the TasklOptions

Configure the options for Taskl, including your tasks and the messages to display during the process.

const options: TasklOptions = {
  tasks: tasks,
  startMessage: '🔄 Starting your process',
  successMessage: 'Process completed successfully.',
  failedMessage: 'Process encountered an error.'
};

For JavaScript users:

const options = {
  tasks: tasks,
  startMessage: '🔄 Starting your process',
  successMessage: 'Process completed successfully.',
  failedMessage: 'Process encountered an error.'
};

Explanation of Options:

  • tasks: An array of Task objects to be executed in sequence.
  • startMessage: A message displayed when the task execution begins.
  • successMessage: A message displayed after all tasks have been successfully completed.
  • failedMessage: A message displayed if any of the tasks fail.

4. Create a Taskl Instance and Run the Tasks

Instantiate Taskl with the configured options and execute the tasks.

const taskl = new Taskl(options);
taskl.runTasks().catch(console.error);

For JavaScript users:

const taskl = new Taskl(options);
taskl.runTasks().catch(console.error);

What Happens Next:

  • Taskl will display the startMessage in cyan color.
  • Each task will be executed in sequence, showing a spinner with the task description.
  • Upon successful completion of a task, a success spinner will appear.
  • If a task fails, a failure spinner will be shown, and the failedMessage will be displayed at the end.
  • Finally, Taskl will display the total execution time.

Complete Example

Here's a complete example integrating all the steps:

import { Taskl, Task, TasklOptions } from 'taskl';

// Define your tasks
const tasks: Task[] = [
  {
    text: 'Connecting to the database',
    run: async () => {
      await connectToDatabase();
    },
  },
  {
    text: 'Starting the API server',
    run: async () => {
      await startApiServer();
    },
  }
];

// Set up Taskl options
const options: TasklOptions = {
  tasks: tasks,
  startMessage: '🔄 Starting your process',
  successMessage: 'Process completed successfully.',
  failedMessage: 'Process encountered an error.'
};

// Create Taskl instance and run tasks
const taskl = new Taskl(options);
taskl.runTasks().catch(console.error);

// Example functions (replace with your actual logic)
async function connectToDatabase() {
  // Simulate async database connection
  return new Promise<void>((resolve) => setTimeout(resolve, 1000));
}

async function startApiServer() {
  // Simulate async server start
  return new Promise<void>((resolve) => setTimeout(resolve, 1000));
}

For JavaScript users:

const { Taskl, Task, TasklOptions } = require('taskl');

// Define your tasks
const tasks = [
  {
    text: 'Connecting to the database',
    run: async () => {
      await connectToDatabase();
    },
  },
  {
    text: 'Starting the API server',
    run: async () => {
      await startApiServer();
    },
  }
];

// Set up Taskl options
const options = {
  tasks: tasks,
  startMessage: '🔄 Starting your process',
  successMessage: 'Process completed successfully.',
  failedMessage: 'Process encountered an error.'
};

// Create Taskl instance and run tasks
const taskl = new Taskl(options);
taskl.runTasks().catch(console.error);

// Example functions (replace with your actual logic)
async function connectToDatabase() {
  // Simulate async database connection
  return new Promise((resolve) => setTimeout(resolve, 1000));
}

async function startApiServer() {
  // Simulate async server start
  return new Promise((resolve) => setTimeout(resolve, 1000));
}

Running the Example:

  1. Ensure you have Taskl installed in your project.
  2. Replace the example connectToDatabase and startApiServer functions with your actual logic.
  3. For TypeScript users:
    • Compile your TypeScript files using tsc or run them using a TypeScript runner like ts-node.
    • Execute your script using node if compiled, or directly if using a TypeScript runner.
  4. For JavaScript users:
    • Execute your script using node:
      node your-script.js

You should see a console output with progress indicators, success/failure messages, and the total execution time.

Contributing

We welcome contributions to Taskl! Whether it's reporting a bug, suggesting an enhancement, or submitting a pull request, your input is valued.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contact

For any questions, suggestions, or feedback, please contact love1ace.