Run multiple commands concurrently.Like npm run watch-js và npm run watch-less but better.

Bạn đang xem: Npm là gì? giới thiệu cơ bản cho người mới bắt đầu

*

Table of contents

ConcurrentlyAPI

Why

I lượt thích task automation with npmbut the usual way khổng lồ run multiple commands concurrently isnpm run watch-js và npm run watch-css. That"s fine but it"s hard lớn keepon track of different outputs. Also if one process fails, others still keep runningand you won"t even notice the difference.

Another option would be to lớn just run all commands in separate terminals. I gottired of opening terminals and made concurrently.

Features:

Cross platform (including Windows)Output is easy khổng lồ follow with prefixesWith --kill-others switch, all commands are killed if one dies

Install

The tool is written in Node.js, but you can use it lớn run any commands.


npm install -g concurrently
or if you are using it from npm scripts:


npm install concurrently --save

Usage

Remember lớn surround separate commands with quotes:


concurrently "command1 arg" "command2 arg"
Otherwise concurrently would try khổng lồ run 4 separate commands:command1, arg, command2, arg.

In package.json, escape quotes:


"start": "concurrently "command1 arg" "command2 arg""
NPM run commands can be shortened:


concurrently "npm:watch-js" "npm:watch-css" "npm:watch-node"# Equivalent to:concurrently -n watch-js,watch-css,watch-node "npm run watch-js" "npm run watch-css" "npm run watch-node"
NPM shortened commands also tư vấn wildcards. Given the following scripts inpackage.json:


//... "scripts": // ... "watch-js": "...", "watch-css": "...", "watch-node": "...", // ... , // ...
concurrently "npm:watch-*"# Equivalent to:concurrently -n js,css,node "npm run watch-js" "npm run watch-css" "npm run watch-node"# Any name provided for the wildcard command will be used as a prefix to the wildcard# part of the script name:concurrently -n w: npm:watch-*# Equivalent to:concurrently -n w:js,w:css,w:node "npm run watch-js" "npm run watch-css" "npm run watch-node"
Exclusion is also supported. Given the following scripts in package.json:


// ... "scripts": "lint:js": "...", "lint:ts": "...", "lint:fix:js": "...", "lint:fix:ts": "...", // ... // ...
# Running only lint:js & lint:ts# with lint:fix:js & lint:fix:ts excludedconcurrently "npm:lint:*(!fix)"
Good frontend one-liner example here.

Help:

concurrently General -m, --max-processes How many processes should run at once. New processes only spawn after all restart tries of a process. -n, --names danh mục of custom names to lớn be used in prefix template. Example names: "main,browser,server" --name-separator The character to split on. Example usage: concurrently -n "styles|scripts|server" --name-separator "|" -r, --raw đầu ra only raw đầu ra of processes, disables prettifying & concurrently coloring. -s, --success Return exit code of zero or one based on the success or failure of the "first" child khổng lồ terminate, the "last child", or succeed only if "all" child processes succeed. --no-color Disables colors from logging --hide Comma-separated danh mục of processes khổng lồ hide the output. The processes can be identified by their name or index. -g, --group Order the output đầu ra as if the commands were run sequentially. --timings Show timing information for all processes Prefix styling -p, --prefix Prefix used in logging for each process. Possible values: index, pid, time, command, name, none, or a template. Example template: "time-pid" -c, --prefix-colors Comma-separated list of chalk colors khổng lồ use on prefixes. If there are more commands than colors, the last màu sắc will be repeated. - Available modifiers: reset, bold, dim, italic, underline, inverse, hidden, strikethrough - Available colors: black, red, green, yellow, blue, magenta, cyan, white, gray or any hex values for colors, eg #23de43 - Available background colors: bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite See https://www.fkhorizont-turnovo.com/package/chalk for more information. -l, --prefix-length Limit how many characters of the command is displayed in prefix. The option can be used lớn shorten the prefix when it is set to lớn "command" -t, --timestamp-format Specify the timestamp in moment/date-fns format. Input handling -i, --handle-input Whether input should be forwarded lớn the child processes. See examples for more information. --default-input-target Identifier for child process to lớn which đầu vào on stdin should be sent if not specified at start of input. Can be either the index or the name of the process. Killing other processes -k, --kill-others kill other processes if one exits or dies --kill-others-on-fail kill other processes if one exits with non zero status code Restarting --restart-tries How many times a process that died should restart. Negative numbers will make the process restart forever. --restart-after Delay time to respawn the process, in milliseconds. Options: -h, --help Show help -v, -V, --version Show version number Examples: - đầu ra nothing more than stdout+stderr of child processes $ concurrently --raw "npm run watch-less" "npm run watch-js" - Normal output đầu ra but without colors e.g. When logging to file $ concurrently --no-color "grunt watch" "http-server" > log - Custom prefix $ concurrently --prefix "time-pid" "npm run watch" "http-server" - Custom names and colored prefixes $ concurrently --names "HTTP,WATCH" -c "bgBlue.bold,bgMagenta.bold" "http-server" "npm run watch" - Configuring via environment variables with CONCURRENTLY_ prefix $ CONCURRENTLY_RAW=true CONCURRENTLY_KILL_OTHERS=true concurrently "echo hello" "echo world" - Send input to default $ concurrently --handle-input "nodemon" "npm run watch-js" rs # Sends rs command khổng lồ nodemon process - Send input to specific child identified by index $ concurrently --handle-input "npm run watch-js" nodemon 1:rs - Send input to specific child identified by name $ concurrently --handle-input -n js,srv "npm run watch-js" nodemon srv:rs - Shortened NPM run commands $ concurrently npm:watch-node npm:watch-js npm:watch-css - Shortened NPM run command with wildcard (make sure lớn wrap it in quotes!) $ concurrently "npm:watch-*"For more details, visit https://github.com/open-cli-tools/concurrently

API

concurrently can be used programmatically by using the API documented below:

concurrently(commands<, options>)

commands: an array of either strings (containing the commands khổng lồ run) or objectswith the shape command, name, prefixColor, env, cwd .

options (optional): an object containing any of the below:

cwd: the working directory to lớn be used by all commands. Can be overriden per command.Default: process.cwd().defaultInputTarget: the default input target when reading from inputStream.Default: 0.handleInput: when true, reads input from process.stdin.killOthers: an array of exitting conditions that will cause a process to kill others.Can contain any of success or failure.maxProcesses: how many processes should run at once.prefix: the prefix type lớn use when logging processes output.Possible values: index, pid, time, command, name, none, or a template (eg