Chaining commands, interpolating args and setting the cwd
Let's say each of our workspace projects has some custom bash scripts in a scripts folder. We want a simple way to create empty bash script files for a given project, that have the execute permissions already set.
Given that Nx knows our workspace structure, we should be able to give it a project and the name of our script, and it should take care of the rest.
The commands option accepts as many commands as you want. By default, they all run in parallel. You can run them sequentially by setting parallel: false:
The above command will execute: webpack --config=example.config.js
This functionality can be disabled by using commands and expanding each command into an object that sets the forwardAllArgs option to false as shown below:
Normally, run-commands considers the commands done when all of them have finished running. If you don't need to wait until they're all done, you can set a special string that considers the commands finished the moment the string appears in stdout or stderr:
The above commands will finish immediately, instead of waiting for 5 seconds.
Nx Affected
The true power of run-commands comes from the fact that it runs through nx, which knows about your project graph. So you can run custom commands only for the projects that have been affected by a change.
We can create some configurations to generate docs, and if run using nx affected, it will only generate documentation for the projects that have been changed:
Try out this interactive editor of the configuration object. Values are validated as you type and hovering over labels will give you more information.
Examples
These buttons show the config object for specific common tasks.
Loading...
Options
Mixins
This type has all of the properties below, but must also match this type:
oneOf [required: commands, required: command]
__unparsed__
Hidden
Array<string>
No description available.
args
string
Extra arguments. You can pass them as follows: nx run project:target --args='--wait=100'. You can then use {args.wait} syntax to interpolate them in the workspace config file. See example above
Current working directory of the commands. If it's not specified the commands will run in the workspace root, if a relative path is specified the commands will run in that path relative to the workspace root and if it's an absolute path the commands will run in that path.
envFile
string
You may specify a custom .env file path.
outputPath
oneOf [string, Array<string>]
Allows you to specify where the build artifacts are stored. This allows Nx Cloud to pick them up correctly, in the case that the build artifacts are placed somewhere other than the top level dist folder.
parallel
boolean
Default: true
Run commands in parallel.
readyWhen
string
String to appear in stdout or stderr that indicates that the task is done. When running multiple commands, this option can only be used when parallel is set to true. If not specified, the task is done when all the child processes complete.
Additional Properties
anything
Extra properties of any type may be provided to this object.