From 0a38c95c9ab0054233626c79a4a1872069c3d9c7 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Fri, 22 Mar 2024 17:32:16 -0400 Subject: [PATCH] cli: implement `node --run ` Co-authored-by: Daniel Lemire --- 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 a1a81a88503698..35e6766a982fb1 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`