Something Like a Task Runner.
node_modules/.bin is automatically addded to PATH).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.
cmd() for defining a Command with a single command line.seq() or par() for defining a grouping Command that runs multiple child Commands.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() and par() accept also any command line string values.cmdEx() for creating a Command from any async function.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.js
As 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 clean
When 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:
rename() method for changing the display name.hide() method to just hide it.collapse() method to collapse a grouping command (seq() or par()) 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();
Creates a Command unit from an external asynchronous function.
Any function of type () => Promise<void>.
Name for displaying instead of an actual command line.
Runs a command immediately & asynchronously.
May be used as a placeholder instead of exec().
Emits a debug log immediately and does nothing else.
May be used as a placeholder instead of cmd().
Creates a Command that has no effect.
Creates a Command object that runs given commands in parallel.
Creates a Command object that runs given commands in sequence.
Generated using TypeDoc
Creates a
Commandunit.