Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5021d88

Browse files
committedMar 17, 2025·
src: added node --run-in
1 parent ab9660b commit 5021d88

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed
 

‎doc/api/cli.md

+10
Original file line numberDiff line numberDiff line change
@@ -2298,6 +2298,16 @@ The following environment variables are set when running a script with `--run`:
22982298
* `NODE_RUN_PACKAGE_JSON_PATH`: The path to the `package.json` that is being
22992299
processed.
23002300

2301+
### `--run-in=<dir>`
2302+
2303+
Run a package.json script in a custom working directory.
2304+
2305+
For example:
2306+
2307+
```console
2308+
$ node --run-in=/path/to/dir --run test
2309+
```
2310+
23012311
### `--secure-heap-min=n`
23022312

23032313
<!-- YAML

‎src/node_options.cc

+3
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,9 @@ PerProcessOptionsParser::PerProcessOptionsParser(
12161216
AddOption("--run",
12171217
"Run a script specified in package.json",
12181218
&PerProcessOptions::run);
1219+
AddOption("--run-in",
1220+
"Run a package.json script in a custom working directory",
1221+
&PerProcessOptions::run_in);
12191222
AddOption(
12201223
"--disable-wasm-trap-handler",
12211224
"Disable trap-handler-based WebAssembly bound checks. V8 will insert "

‎src/node_options.h

+1
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ class PerProcessOptions : public Options {
327327
bool print_version = false;
328328
std::string experimental_sea_config;
329329
std::string run;
330+
std::string run_in;
330331

331332
#ifdef NODE_HAVE_I18N_SUPPORT
332333
std::string icu_data_dir;

‎src/node_task_runner.cc

+6-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,12 @@ void ProcessRunner::OnExit(int64_t exit_status, int term_signal) {
206206

207207
void ProcessRunner::Run() {
208208
// keeps the string alive until destructor
209-
cwd = package_json_path_.parent_path().string();
209+
std::string working_dir;
210+
if (!node::per_process::cli_options->run_in.empty())
211+
working_dir = node::per_process::cli_options->run_in;
212+
else
213+
working_dir = package_json_path_.parent_path().string();
214+
cwd = working_dir;
210215
options_.cwd = cwd.c_str();
211216
if (int r = uv_spawn(loop_, &process_, &options_)) {
212217
fprintf(stderr, "Error: %s\n", uv_strerror(r));

‎test/parallel/test-node-run.js

+15
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,19 @@ describe('node --run [command]', () => {
222222
assert.strictEqual(child.stdout, '');
223223
assert.strictEqual(child.code, 1);
224224
});
225+
226+
it('runs script in a custom working directory using --run-in', async () => {
227+
const customDir = fixtures.path('run-script');
228+
const child = await common.spawnPromisified(
229+
process.execPath,
230+
[ '--run-in', customDir, '--run', `pwd${envSuffix}` ],
231+
232+
{ cwd: fixtures.path('run-script/sub-directory') }
233+
);
234+
235+
assert.strictEqual(child.stdout.trim(), customDir);
236+
assert.strictEqual(child.stderr, '');
237+
assert.strictEqual(child.code, 0);
238+
});
239+
225240
});

0 commit comments

Comments
 (0)
Please sign in to comment.