command
A command in nr
encapsulates a reference to an external executable (CLI, script, etc), the arguments to pass to it, and configuration related to how to handle its output. Commands may then be invoked by scripts.
To create a command, use the function at the command
property of the context object passed to your configuration function:
command
command
This function accepts the following parameters:
name: string
name: string
Human friendly name for the command. This will be used for error-reporting and can be used to reference the command in scripts.
Array containing the executable/file to run and any arguments to pass to it. This array may take one of four forms, described below.
[executable]
of type[string]
[executable, positionals]
of type[string, Array<string>]
[executable, flags]
of type[string, Record<string, any>]
[executable, positionals, flags]
of type[string, Array<string>, Record<string, any>]
Example:
This approach may seem verbose at first, but compared to a single, raw string it separates the parts of a command invocation into discreet values that enable re-use:
Commands are not executed in a shell environment where you can use constructs like &&
, ||
, or$?
. Rather, a command should be shell-agnostic and point to a single executable file that will be invoked without an interpreter like cmd.exe
or /bin/bash
.
Commands may be further configured by passing an optional options object as the third parameter to command
. Its properties are detailed below.
prefix?: (chalk: Chalk) => string
prefix?: (chalk: Chalk) => string
Function that will be passed a chalk
instance and should return a string. This string will be used to prefix each line of output from the command.
This feature can be useful in cases where scripts run multiple commands in parallel or when a CLIs output may not make it obvious what program the output is coming from.
Example:
In this example, let's add a prefix to our Babel command.
nr
executes commands using execa
. Here, you may provide any option accepted by execa
to further fine-tune how the command is executed.
Example:
preserveArgumentCasing?: boolean
preserveArgumentCasing?: boolean
Default: false
Idiomatic JavaScript uses camel-case for object keys while the vast majority of CLIs accept named arguments in kebab-case. For this reason, nr
automatically converts named arguments to kebab-case for you.
However, some CLIs (TypeScript, for example) expect named arguments to be in camel-case. To account for this, this option may be set to true
to bypass automatic conversion of named arguments to kebab-case.
Example:
When command
is called, it creates a CommandThunk
, a function that can be invoked to run the command. This function is added to a registry using the command's name
, and then it is returned. There are two ways to reference a command in a script:
Use the
CommandThunk
directly in a script definition.Use a string in the form
cmd:name
to instructnr
to look-up the command in the registry.
command.node
command.node
This variant of command
is useful for commands where the executable is the node
binary itself.
It has the same signature as command
. It can be used to execute a Node script (regardless of whether the executable flag has been set on it) using the current version of Node. This variant uses execaNode
to execute the script.
Example:
command.babel
command.babel
This variant of command
is useful for executing commands with babel-node
.
It has the same signature as command
. It can be used to execute a Node script (regardless of whether the executable flag has been set on it) using the babel-node
CLI. This variant also uses execaNode
to execute the script.
Example:
Last updated