From 128c60d90609ac25e6dce9cd5937c497786bda41 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sun, 7 Apr 2024 20:49:14 -0400 Subject: [PATCH] cli: implement `node --run ` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Lemire PR-URL: https://github.com/nodejs/node/pull/52190 Reviewed-By: Daniel Lemire Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: Geoffrey Booth Reviewed-By: Moshe Atlow Reviewed-By: Matteo Collina Reviewed-By: Robert Nagy Reviewed-By: Paolo Insogna Reviewed-By: Tierney Cyren Reviewed-By: Chemi Atlow Reviewed-By: Benjamin Gruenbaum Reviewed-By: Ruy Adorno --- doc/api/cli.md | 44 +++++++++ lib/internal/main/run.js | 74 ++++++++++++++ lib/internal/shell.js | 37 +++++++ src/node.cc | 4 + src/node_modules.cc | 40 ++++++++ src/node_modules.h | 3 + src/node_options.cc | 3 + src/node_options.h | 1 + test/fixtures/run-script/.env | 1 + .../fixtures/run-script/node_modules/.bin/ada | 2 + .../run-script/node_modules/.bin/ada.bat | 1 + .../run-script/node_modules/.bin/custom-env | 2 + .../node_modules/.bin/custom-env.bat | 1 + .../node_modules/.bin/positional-args | 2 + .../node_modules/.bin/positional-args.bat | 2 + test/fixtures/run-script/package.json | 11 +++ test/message/node_run_non_existent.js | 14 +++ test/message/node_run_non_existent.out | 10 ++ test/parallel/test-node-run.js | 99 +++++++++++++++++++ typings/internalBinding/modules.d.ts | 1 + 20 files changed, 352 insertions(+) create mode 100644 lib/internal/main/run.js create mode 100644 lib/internal/shell.js create mode 100644 test/fixtures/run-script/.env create mode 100755 test/fixtures/run-script/node_modules/.bin/ada create mode 100755 test/fixtures/run-script/node_modules/.bin/ada.bat create mode 100755 test/fixtures/run-script/node_modules/.bin/custom-env create mode 100755 test/fixtures/run-script/node_modules/.bin/custom-env.bat create mode 100755 test/fixtures/run-script/node_modules/.bin/positional-args create mode 100755 test/fixtures/run-script/node_modules/.bin/positional-args.bat create mode 100644 test/fixtures/run-script/package.json create mode 100644 test/message/node_run_non_existent.js create mode 100644 test/message/node_run_non_existent.out create mode 100644 test/parallel/test-node-run.js diff --git a/doc/api/cli.md b/doc/api/cli.md index 002aa1eef5859c..f08afb737d41b4 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1807,6 +1807,50 @@ Only CommonJS modules are supported. Use [`--import`][] to preload an [ECMAScript module][]. Modules preloaded with `--require` will run before modules preloaded with `--import`. +### `--run` + + + +> Stability: 1.1 - Active development + +This runs a specified command from a package.json's `"scripts"` object. +If no `"command"` is provided, it will list the available scripts. + +`--run` prepends `./node_modules/.bin`, relative to the current +working directory, to the `PATH` in order to execute the binaries from +dependencies. + +For example, the following command will run the `test` script of +the `package.json` in the current folder: + +```console +$ node --run test +``` + +You can also pass arguments to the command. Any argument after `--` will +be appended to the script: + +```console +$ node --run test -- --verbose +``` + +#### Intentional limitations + +`node --run` is not meant to match the behaviors of `npm run` or of the `run` +commands of other package managers. The Node.js implementation is intentionally +more limited, in order to focus on top performance for the most common use +cases. +Some features of other `run` implementations that are intentionally excluded +are: + +* Searching for `package.json` files outside the current folder. +* Prepending the `.bin` or `node_modules/.bin` paths of folders outside the + current folder. +* Running `pre` or `post` scripts in addition to the specified script. +* Defining package manager-specific environment variables. + ### `--secure-heap=n`