Skip to content

Commit

Permalink
cli: add lifecycle event env to node --run
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed May 17, 2024
1 parent 075853e commit 69dbfd7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
11 changes: 11 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,10 @@ Modules preloaded with `--require` will run before modules preloaded with `--imp

<!-- YAML
added: v22.0.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/53032
description: `NODE_LIFECYCLE_EVENT` environment variable is added.
-->

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

#### Environment variables

The following environment variables are set when running a script with `--run`:

* `NODE_LIFECYCLE_EVENT`: The name of the script being run. For example, if
`--run` is used to run `test`, the value of this variable will be `test`.

### `--secure-heap=n`

<!-- YAML
Expand Down
7 changes: 6 additions & 1 deletion src/node_task_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ static constexpr const char* bin_path = "/node_modules/.bin";
#endif // _WIN32

ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
std::string_view script_name,
std::string_view command,
const PositionalArgs& positional_args) {
memset(&options_, 0, sizeof(uv_process_options_t));
Expand Down Expand Up @@ -85,6 +86,10 @@ ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
}
uv_os_free_environ(env_items, env_count);

// Add NODE_LIFECYCLE_EVENT environment variable to the environment
// to indicate which script is being run.
env_vars_.push_back("NODE_LIFECYCLE_EVENT=" + std::string(script_name));

// Use the stored reference on the instance.
options_.file = file_.c_str();

Expand Down Expand Up @@ -276,7 +281,7 @@ void RunTask(std::shared_ptr<InitializationResultImpl> result,
return;
}

auto runner = ProcessRunner(result, command, positional_args);
auto runner = ProcessRunner(result, command_id, command, positional_args);
runner.Run();
}

Expand Down
1 change: 1 addition & 0 deletions src/node_task_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ProcessRunner {
};

void RunTask(std::shared_ptr<InitializationResultImpl> result,
std::string_view script_name,
std::string_view command_id,
const PositionalArgs& positional_args);
PositionalArgs GetPositionalArgs(const std::vector<std::string>& args);
Expand Down

0 comments on commit 69dbfd7

Please sign in to comment.