@ygor/tasks

5.4.1 • Public • Published

@ygor/tasks

NPM version Downloads Build Status Coverage Status

A no-frills task runner. Built on promises to work wonderfully with async and await in Node.js 8 and above. Part of the Ygor toolkit.

Node is the CLI, npm is the plugin system. Go nuts.

Install

$ npm install --save-dev @ygor/tasks

Usage

Node is the CLI.

Usage: node <file> [task] [options]

  file   The filename of your script.
  task   The name of the task to run (default: 'default').

Options:

  -q, --quiet   Suppress logging (default: false).
      --run     Auto-run task (default: true).

Create a JavaScript file, write some functions, tell Ygor.

// make.js

const tasks = require('@ygor/tasks');

async function bundle() {
    // bundle something
}

async function instrument() {
    // instrument tests
}

async function test() {
    // test something
}

async function cover() {
    await instrument();
    await test();

    // report coverage
}

tasks
    .add('default', bundle)
    .add('test', test);
    .add('cover', cover);

To run a task, execute the file with Node.js (or with babel-node if that's how you roll) and indicate which task to perform.

$ node make
$ node make test
$ node make cover

Deferred Tasks

If you need to define tasks asynchronously, you may call tasks as a function at a later time.

ToolsApi
    .getTools()
    .then(tools => {
        return tasks()
            .add('foo', tools.foo)
            .add('bar', tools.bar);
    });

Subtasks

You may also call tasks() within a task callback to create subtasks.

function childA1() { console.log('hi from a1'); }
function childA2() { console.log('hi from a2'); }

function parentA() {
    // Subtasks
    return tasks()
        .add('1', childA1)
        .add('2', childA2);
}

function childB1() { console.log('hi from b1'); }
function childB2() { console.log('hi from b2'); }

function parentB() {
    // Subtasks
    return tasks()
        .add('1', childB1)
        .add('2', childB2);
}

tasks
    .add('a', parentA)
    .add('b', parentB);

Then execute subtasks by passing the parent task name as the first argument and the child task name as the second.

$ node make a 2
hi from a2

$ node make b 1
hi from b1

Bring Your Own Arguments

You can override the default cli parsing by providing your own arguments object.

function logCli(cli) {
    console.log(cli);
}

tasks({ foo: 'bar' })
    .add('log', logCli);
$ node make log
{ foo: 'bar' }

API

tasks.cli

Command-line arguments as parsed by minimist.

tasks.add(name, callback): tasks

  • name {String} Unique task identifier.
  • callback {Function(cli, tasks)} Function to run when the task is invoked.

Registers a task with Ygor. The callback provided will be executed with tasks.cli as the first argument and tasks as the second.

function foo(cli, tasks) {
    console.log(cli, tasks);
}

tasks.add('foo', foo);

tasks.run(name): Promise<>

  • name {String} Unique task identifier.

Tells Ygor to run a task. This is used internally and generally shouldn't be invoked directly. Ygor recommends that tasks be declared as standalone functions.

// Avoid

tasks.add('foo', function () {
    // do something
});

tasks.add('bar', function (cli, t) {
    t.run('foo');
});

// Recommended

function foo() {
    // do something
}

function bar() {
    foo();
}

tasks
    .add('foo', foo)
    .add('bar', bar);

tasks([cli]): tasks

  • options {Object} - The cli arguments. (default: minimist(process.argv.slice(2)))

Creates a subset of tasks, useful for providing your own cli arguments, lazy task definition, and creating subtasks.


ygor


MIT © Shannon Moeller

Package Sidebar

Install

npm i @ygor/tasks

Weekly Downloads

9

Version

5.4.1

License

MIT

Unpacked Size

9.04 kB

Total Files

3

Last publish

Collaborators

  • shannonmoeller