Small utility for executing series of tasks with console reporting.
-
Install with npm:
npm install @ludekarts/task-runner --save-dev
-
Create
task.js
file and import task-runner e.g.:echo 'const { TaskRunner, runCommand } = require("@ludekarts/task-runner");' > tasks.js
-
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 });
-
Execute
tasks.js
script thorugh system terminal> node tasks.js
-
Expeceted result
Running task: npmVersion npmVersion: completed ✔ No errors occured!
-
Create new
toolbox.js
file: -
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 });
-
Run only tasks you want like this:
> node toolbox.js taskNameTwo
-
Create new
builder.js
file: -
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)));
-
Add new script to your package.json file e.g:
"scripts": { "builder": "node ./builder.js" },
-
Run npm script like this:
npm run builder -- development desktop
-
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: UseisAbsolute
flag to mark path in file's methods as absoute in other case it will ge relative to the executed file.