Skip to content

ludekarts/task-runner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Task runner

Small utility for executing series of tasks with console reporting.

Usage

  1. Install with npm:

    npm install @ludekarts/task-runner --save-dev
    
  2. Create task.js file and import task-runner e.g.:

    echo 'const { TaskRunner, runCommand } = require("@ludekarts/task-runner");' > tasks.js
    
  3. Add your first tasks collection and add first task

    const tasksCollection = {};
    
    tasksCollection.checkNpmVersion = async function () {
      const npmVersion = await runCommand("npm -v", { resolveWithOutput: true, showOutput: true });
      if (!/^\d+\.\d+\.\d+/.test(npmVersion)) {
        throw new Error("No NPM! 😱");
      }
    };
    
    TaskRunner(tasksCollection, { showErrorReport: true });
    
  4. Execute tasks.js script thorugh system terminal

    > node tasks.js
    
  5. Expeceted result

    Running task: npmVersion
    npmVersion: completed ✔
    No errors occured!
    

How to run only some tasks?

  1. Create new toolbox.js file:

  2. Add following code to the file:

    const { TaskRunner, runCommand } = require("@ludekarts/task-runner");
    
    const selectiveList = process.argv.slice(2);
    
    const tasks = {
      async taskNameOne() {
        await runCommand("echo runing:taskNameOne");
      },
    
      async taskNameTwo() {
        await runCommand("echo runing:taskNameTwo");
      },
    }
    
    TaskRunner(tasks, { selectiveList, showErrorReport: true });
    
  3. Run only tasks you want like this:

    > node toolbox.js taskNameTwo
    

How to combine NPM scripts and taskRunner?

  1. Create new builder.js file:

  2. Add following code to the file:

    const { runCommand, message } = require("@ludekarts/task-runner");
    
    (async function builder(task, platform) {
      switch (task) {
    
        case "development":
          await runCommand(`cross-env NODE_PLATFORM=${platform} webpack serve --mode development`);
          break;
    
        default:
          message.error(`Unknown task: ${task}`);
          break;
    
      }
    }(...process.argv.slice(2)));
    
  3. Add new script to your package.json file e.g:

    "scripts": {
      "builder": "node ./builder.js"
    },
    
  4. Run npm script like this:

    npm run builder -- development desktop
    

API Reference

  • TaskRunner

    TaskRunner (
      tasksCollection: Object,
      {
        selectiveList: Array (undefined),
        showErrorReport: Boolean(false)
      }
    );
    

    Runs all tasks from given tasksCollection. Each task is called asynchronous. If showErrorReport flag set to TRUE at the end user will be presented with full Error Report. By passing to selectiveList array of tasks to run user can specify which task should run. Best way to utilize this feature is to connect it to precess.args e.g.:

    const selectiveList = process.argv.slice(2);
    TaskRunner(tasks, { selectiveList, showErrorReport: true });
    

    Thanks to this user can call script with names of tasks to run e.g.:

    > node tasks.js taskNameOne taskNameTwo
    
  • runCommand(

    runCommand(
      systemCommand: String,
      resolveWithData: Boolean(false)
    ): Promise;
    

    Allows user to run system commands. Returns promise that resolves by default with exit code or stringified buffer data if resolveWithData flag is set. If method resolves with data the output of the method will no be present in the console instead it will be returned as the promise result.

  • message (sync methods):

    • info( message: String ) - Output blue text to the console.
    • error( message: String ) - Output red text to the console.
    • success( message: String ) - Output green text to the console.
    • warning( message: String ) - Output orange text to the console.
    • input( message: String, defaultValue: String ) -> take userInput: String -> Promise() - Display Message and take user input.
  • file (sync methods):

    • save ( path: String, content: String, { override: Boolean(true), isAbsolute: Boolean(false) } ) - Save file under given path.
    • read ( path: String, isAbsolute: Boolean(false) ) -> content: String - Read file from given path.
    • saveJson( path: String, json: Object, { override: Boolean(true), isAbsolute: Boolean(false) } ) - Save JSON file under given path.
    • readJson( path: String, isAbsolute: Boolean(false) ) -> json: Object - Read JSON file from given path.
    • crawler( directory: String, processing: function ) -> Walk through given directory, and process each path with processing fn.

    ⚠️ NOTE: Use isAbsolute flag to mark path in file's methods as absoute in other case it will ge relative to the executed file.

About

Tool for executing series of system tasks with console reporting

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published