Something Like a Task Runner.
- Run multiple commands in sequence or in parallel.
- Simple. Lightweight. No dependencies.
- Might be an alternative to NPM scripts (
node_modules/.binis automatically addded toPATH). - Write your own script in JS and construct any tree structure for defining the execution order.
- Create a router so that you can run different commands according to arguments.
- Check summary of execution results and see where it took time or where it failed.
GitHub: https://github.com/fal-works/s-l-t-r
npm install -D @fal-works/s-l-t-r
Here we will write a script and make a tree structure consisting of Command elements.
- Use
cmd()for defining aCommandwith a single command line. - Use
seq()orpar()for defining a groupingCommandthat runs multiple childCommands.
seq()for sequence,par()for parallel. They can be nested as well.
For example, a script for building the library s-l-t-r itself (say build.js) would look like this:
// build.js
/** import */
const s_l_t_r = require("@fal-works/s-l-t-r");
const { cmd, seq, par } = s_l_t_r; // functions for creating command elements
const { cleandir } = s_l_t_r.builtin; // built-in functions for specific purposes
/** prepare commands used frequently */
const lint = (files) => cmd("eslint", "--fix", files);
/** clean-up files in parallel */
const clean = par(cleandir("lib"), cleandir("types")).rename("clean");
/** emit files into lib/types */
const emit = cmd("tsc", "--skipLibCheck");
/** format files in parallel */
const format = par(lint("lib/**/*.js"), lint("types/**/*.ts")).rename("format");
/** do all of the above in sequence */
const build = seq(clean, emit, format).rename("build");Something more:
seq()andpar()accept also any command linestringvalues.- Use
cmdEx()for creating aCommandfrom anyasyncfunction. - Use
ignoreFailure()method if you want to run subsequent commands whether the command in question succeeds or not.
Use run() to start executing any Command:
// build.js
/* ...(see above)... */
s_l_t_r.run(build);Now you can simply run the script with Node.js:
# on the CLI or NPM script
node build.jsAs an alternative to the top-level run() function,
create a router so you can receive any key and run different Commands:
// build.js
/* ...(see above)... */
/**
* This router accepts the keys: "clean", "emit", "format" or "build",
* otherwise it prints the registered mapping between keys and commands.
*/
const router = s_l_t_r.tools.createRouter({ clean, emit, format, build });
const CLI_ARGUMENT = process.argv[2];
router.run(CLI_ARGUMENT);Now you can run the script passing any key that specifies the Command to be executed:
# on the CLI or NPM script
node build.js cleanWhen run() completes, it outputs a summary of the execution results.
It looks like this:
-- | [seq] build
-- | [par] clean
ok 0.01s | cleandir lib
ok 0.01s | cleandir types
ok 1.30s | tsc
-- | [par] format
ok 1.49s | eslint --fix lib/**/*.js
ok 1.24s | eslint --fix types/**/*.ts
...or can also be flattend by changing the config:
sltr.config.setResultSummaryType("list"); // default: "tree"which looks like this:
ok 0.01s | cleandir lib
ok 0.01s | cleandir types
ok 1.32s | tsc
ok 1.49s | eslint --fix lib/**/*.js
ok 1.23s | eslint --fix types/**/*.ts
You can also change the display in the summary for each Command individually:
- Use
rename()method for changing the display name. - Use
hide()method to just hide it. - Use
collapse()method to collapse a grouping command (seq()orpar()) and hide its children.
The quiet mode suppresses log messages and only the final result (1 line, whether completed or not) is printed.
sltr.config.setQuiet();