hodev-dev / deno-invoker

command line tool and parse for deno js to run tasks in sequence and send data of each task to next one

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deno Invoker

import {Invoker, Command} from "https://deno.land/x/invoker@0.0.2/export.ts";

Example:

const invoker = new Invoker();

const get = new Command(Deno.args)
    .name("get")
    .argument("arg1","arg2")
    .option("-i", "--id")
    .option("-p", "--page")
    .sequence(function* tasks() {
        yield async (data: any, next: Function, reject: Function) => {
            const id = data.parse.options.id[0];
            return next({"id": id});
        };
        yield async (data: any, next: Function, reject: Function) => {
            const {prev} = data.response;
            const jsonResponse = await fetch(`https://jsonplaceholder.typicode.com/todos/${prev.id}`);
            const jsonData = await jsonResponse.json();
            return next({json: jsonData});
        };
        yield async (data: any, next: Function, reject: Function) => {
            const {prev} = data.response;
            return reject({error: "Something Wrong"});
        };
    }).catch((err: any) => {
        const {error} = err.data;
        console.log(error);
    }).finally((data: any) => {
        console.log(data);
    });

invoker.register([get]);
await invoker.run();

run

deno run --allow-net .\filename.ts get -i 2

task methods

  • next: send data to next task;
  • reject: reject task with data;
  • catch: run after reject and receives reject data;
  • finally: runs in the end

data object

data object has two part

  • parse: which parse command and gives you data about command
{
    "parse": {
        "arguments": {},
        "options": {
            "id": [
                "2"
            ]
        },
        "unexpectedArgument": [],
        "unexpectedOptions": []
    }
}
  • response: which gives you information passed but next() method
    1. prev: contains data of previous task that passed by next()
    2. stack: contains all data of all task till this task
 {
    "response": {
        "prev": {
            "status": true,
            "data": {
                "json": [
                    "Object"
                ]
            }
        },
        "stack": [
            {
                "status": true,
                "data": [
                    "Object"
                ]
            },
            {
                "status": true,
                "data": [
                    "Object"
                ]
            },
            {
                "status": false,
                "data": [
                    "Object"
                ]
            }
        ]
    }
}

Full Data Object

{
    "parse": {
        "arguments": {},
        "options": {
            "id": [
                "2"
            ]
        },
        "unexpectedArgument": [],
        "unexpectedOptions": []
    },
    "response": {
        "prev": {
            "status": true,
            "data": {
                "json": [
                    "Object"
                ]
            }
        },
        "stack": [
            {
                "status": true,
                "data": [
                    "Object"
                ]
            },
            {
                "status": true,
                "data": [
                    "Object"
                ]
            },
            {
                "status": false,
                "data": [
                    "Object"
                ]
            }
        ]
    }
}

About

command line tool and parse for deno js to run tasks in sequence and send data of each task to next one


Languages

Language:TypeScript 100.0%