1.1.3 โ€ข Published 6 months ago

@keyboardcowboy/taskprompt v1.1.3

Weekly downloads
-
License
-
Repository
-
Last release
6 months ago

TaskPrompt CLI

A flexible and extensible task management system for Node.js applications. TaskPrompt CLI allows you to create interactive command-line applications with a menu-driven interface, user input handling, and task dependencies.

Features

  • ๐ŸŽฏ Task-based architecture
  • ๐Ÿ”„ Task chaining and dependencies
  • ๐Ÿ’ฌ Interactive user input with validation
  • ๐ŸŽจ Customizable task ordering
  • ๐Ÿ’พ Persistent answer storage
  • ๐Ÿงน Built-in cache management
  • ๐Ÿšช Graceful exit handling

File Structure

your-app/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ tasks/           # All tasks go here
โ”‚   โ”‚   โ”œโ”€โ”€ index.js     # Export all tasks
โ”‚   โ”‚   โ”œโ”€โ”€ TaskA.js     # Individual task files
โ”‚   โ”‚   โ””โ”€โ”€ TaskB.js
โ”‚   โ”œโ”€โ”€ ...             # Your application files
โ”‚   โ””โ”€โ”€ ...             # Additional core files
โ”œโ”€โ”€ index.js            # Main application file
โ”œโ”€โ”€ package.json
โ””โ”€โ”€ README.md

Each task should be in its own file under the src/tasks directory. Use index.js to export all tasks for easy importing. The src directory can contain any additional files needed by your application.

Installation

npm install taskprompt-cli

Quick Start

  1. Create a new task file in src/tasks/HelloWorldTask.js:
const { Task } = require('taskprompt-cli');

/**
 * @type {Task}
 */
const HelloWorldTask = {
    id: 'hello-world',
    name: 'Hello World',
    description: 'Prints a hello world message',
    weight: 0,
    chain: true,
    execute: async (taskManager) => {
        console.log('Hello, World! ๐Ÿ‘‹');
    }
};

module.exports = { HelloWorldTask };
  1. Set up your main application file at the root level (index.js):
const { TaskManager } = require('taskprompt-cli');
const { HelloWorldTask } = require('./src/tasks/HelloWorldTask');

async function main() {
    const taskManager = new TaskManager();
    taskManager.registerTask(HelloWorldTask);
    await taskManager.run();
}

main().catch(console.error);

Task Structure

A task is defined with the following properties:

{
    // Required properties
    id: 'unique-id',         // Unique identifier
    name: 'Task Name',       // Display name
    description: 'Description', // Task description
    execute: async (taskManager, answers) => {
        // Task implementation
    },

    // Optional properties
    weight: 0,              // Controls task order (higher = later)
    chain: true,            // Whether to show menu after completion
    dependencies: ['task-id'], // Tasks that must run first
    questions: []           // User input questions
}

User Input

Tasks can collect user input using the questions property. We use the Inquirer package for user input, so you can define questions using their docs.

const UserInputTask = {
    id: 'user-input',
    name: 'User Input Example',
    description: 'Demonstrates user input',
    questions: [
        {
            type: 'input',
            name: 'name',
            message: 'What is your name?',
            validate: (input) => {
                if (input.length < 2) {
                    return 'Name must be at least 2 characters long';
                }
                return true;
            }
        },
        {
            type: 'number',
            name: 'age',
            message: 'What is your age?',
            validate: (input) => {
                if (input < 0 || input > 150) {
                    return 'Please enter a valid age';
                }
                return true;
            }
        }
    ],
    execute: async (taskManager, answers) => {
        const { name, age } = answers;
        console.log(`Hello, ${name}! You are ${age} years old.`);
    }
};

Task Dependencies

Tasks can depend on other tasks:

const ProcessDataTask = {
    id: 'process-data',
    name: 'Process Data',
    description: 'Processes the input data',
    dependencies: ['user-input'], // Will run after user-input
    execute: async (taskManager) => {
        const userInfo = taskManager.getTaskAnswers('user-input');
        // Use the stored answers from user-input task
    }
};

Task Ordering

Control task order using the weight property:

const ExitTask = {
    id: 'exit',
    name: 'Exit Program',
    description: 'Exit the application',
    weight: 100, // Appears last in the menu
    chain: false, // Exits after completion
    execute: async () => {
        console.log('Goodbye! ๐Ÿ‘‹');
        process.exit(0);
    }
};

Built-in Tasks

The library includes two built-in tasks:

  1. ExitTask (weight: 100)

    • Exits the application
    • Appears last in the menu
  2. ClearCacheTask (weight: 99)

    • Clears stored task answers
    • Appears second to last in the menu

Example Application

Check out the example directory for a complete working example that demonstrates:

  • Basic task creation
  • User input handling
  • Task dependencies
  • Answer persistence
  • Task ordering

Contributing

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

License

MIT

1.1.3

6 months ago

1.1.2

6 months ago

1.1.0

6 months ago

1.0.0

6 months ago