Skip to content

Commit 69dbfd7

Browse files
committed
cli: add lifecycle event env to node --run
1 parent 075853e commit 69dbfd7

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/api/cli.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,10 @@ Modules preloaded with `--require` will run before modules preloaded with `--imp
18471847

18481848
<!-- YAML
18491849
added: v22.0.0
1850+
changes:
1851+
- version: REPLACEME
1852+
pr-url: https://github.com/nodejs/node/pull/53032
1853+
description: `NODE_LIFECYCLE_EVENT` environment variable is added.
18501854
-->
18511855

18521856
> Stability: 1.1 - Active development
@@ -1887,6 +1891,13 @@ are:
18871891
* Running `pre` or `post` scripts in addition to the specified script.
18881892
* Defining package manager-specific environment variables.
18891893

1894+
#### Environment variables
1895+
1896+
The following environment variables are set when running a script with `--run`:
1897+
1898+
* `NODE_LIFECYCLE_EVENT`: The name of the script being run. For example, if
1899+
`--run` is used to run `test`, the value of this variable will be `test`.
1900+
18901901
### `--secure-heap=n`
18911902

18921903
<!-- YAML

src/node_task_runner.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ static constexpr const char* bin_path = "/node_modules/.bin";
1212
#endif // _WIN32
1313

1414
ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
15+
std::string_view script_name,
1516
std::string_view command,
1617
const PositionalArgs& positional_args) {
1718
memset(&options_, 0, sizeof(uv_process_options_t));
@@ -85,6 +86,10 @@ ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
8586
}
8687
uv_os_free_environ(env_items, env_count);
8788

89+
// Add NODE_LIFECYCLE_EVENT environment variable to the environment
90+
// to indicate which script is being run.
91+
env_vars_.push_back("NODE_LIFECYCLE_EVENT=" + std::string(script_name));
92+
8893
// Use the stored reference on the instance.
8994
options_.file = file_.c_str();
9095

@@ -276,7 +281,7 @@ void RunTask(std::shared_ptr<InitializationResultImpl> result,
276281
return;
277282
}
278283

279-
auto runner = ProcessRunner(result, command, positional_args);
284+
auto runner = ProcessRunner(result, command_id, command, positional_args);
280285
runner.Run();
281286
}
282287

src/node_task_runner.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class ProcessRunner {
5252
};
5353

5454
void RunTask(std::shared_ptr<InitializationResultImpl> result,
55+
std::string_view script_name,
5556
std::string_view command_id,
5657
const PositionalArgs& positional_args);
5758
PositionalArgs GetPositionalArgs(const std::vector<std::string>& args);

0 commit comments

Comments
 (0)