Combine javascript functions and bash commands in your node scripts. Every task will be displayed with a spinner in the command line.
const fs = require("fs")
const path = require("path")
const { makeRunnable, exec, run } = require("./index.js")
makeRunnable(async () => {
await run(createGitRepo, "Create git repository")
await run(removeGitRepo, "Remove git repository")
})()
async function createGitRepo() {
await exec("git init")
}
async function removeGitRepo() {
fs.rmdirSync(path.join(__dirname, "..", ".git"), { recursive: true })
}
√ Create git repository 68.58ms
√ Remove git repository 9.6ms
Executed in 0.08s
npm install @m.moelter/task-runner
The exec
function is used to execute bash commands and run
will execute an async function. Create your tasks using async functions, use exec
inside them and run them using run
. makeRunnable
should be called in your script's main file and execute all of your tasks. If your scripts are big enough you may encapsulate every task in it's own file.
Executes the given command(s) using node's integrated require("child_process").exec
function.
exec(commands, [options])
Arguments
commands: [String | Array<String>]
The command to be executed. If an array is given, all commands in that array will be executed in order.
options: {
// If set to true, any error thrown while exeuting the command(s) will be ignored
skipErrors: Boolean
}
Example
// Single command
await exec("git init")
// Multiple commands and ignoring errors
await exec([
"git add .",
"git commit -m \"my commit message\"",
"git push"
], { skipErrors: true })
Runs an (asynchronous) function and displays a spinner and the execution time.
run(fn, title)
Arguments
fn: Function
The function to be executed
title: String
The text shown in the terminal while the function is running
spinnerOptions: Object
The options passed to the ora instance
Example
run(createGitRepo, "Create git repository")
async function createGitRepo() {
await exec("git init")
}
Creates a function which executes the given function and provides statistics.
makeRunnable(fn) -> Function
Arguments
fn: Function
The function to be executed and messured
Example
makeRunnable(async () => {
await run(createGitRepo, "Create git repository")
await run(removeGitRepo, "Remove git repository")
})()