@@ -12,6 +12,7 @@ static constexpr const char* bin_path = "/node_modules/.bin";
1212#endif // _WIN32
1313
1414ProcessRunner::ProcessRunner (std::shared_ptr<InitializationResultImpl> result,
15+ const std::string& script_name,
1516 std::string_view command,
1617 const PositionalArgs& positional_args) {
1718 memset (&options_, 0 , sizeof (uv_process_options_t ));
@@ -51,39 +52,9 @@ ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
5152 // callback.
5253 process_.data = this ;
5354
54- std::string command_str (command);
55-
56- // Set environment variables
57- uv_env_item_t * env_items;
58- int env_count;
59- CHECK_EQ (0 , uv_os_environ (&env_items, &env_count));
60- env = std::unique_ptr<char *[]>(new char *[env_count + 1 ]);
61- options_.env = env.get ();
62-
63- // Iterate over environment variables once to store them in the current
64- // ProcessRunner instance.
65- for (int i = 0 ; i < env_count; i++) {
66- std::string name = env_items[i].name ;
67- auto value = env_items[i].value ;
68-
69- #ifdef _WIN32
70- // We use comspec environment variable to find cmd.exe path on Windows
71- // Example: 'C:\\Windows\\system32\\cmd.exe'
72- // If we don't find it, we fallback to 'cmd.exe' for Windows
73- if (StringEqualNoCase (name.c_str (), " comspec" )) {
74- file_ = value;
75- }
76- #endif // _WIN32
55+ SetEnvironmentVariables (current_bin_path, script_name);
7756
78- // Check if environment variable key is matching case-insensitive "path"
79- if (StringEqualNoCase (name.c_str (), " path" )) {
80- env_vars_.push_back (name + " =" + current_bin_path + value);
81- } else {
82- // Environment variables should be in "KEY=value" format
83- env_vars_.push_back (name + " =" + value);
84- }
85- }
86- uv_os_free_environ (env_items, env_count);
57+ std::string command_str (command);
8758
8859 // Use the stored reference on the instance.
8960 options_.file = file_.c_str ();
@@ -128,11 +99,50 @@ ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
12899 options_.args [i] = const_cast <char *>(command_args_[i].c_str ());
129100 }
130101 options_.args [argc] = nullptr ;
102+ }
103+
104+ void ProcessRunner::SetEnvironmentVariables (const std::string& bin_path,
105+ const std::string& script_name) {
106+ // Set environment variables
107+ uv_env_item_t * env_items;
108+ int env_count;
109+ CHECK_EQ (0 , uv_os_environ (&env_items, &env_count));
110+ env = std::unique_ptr<char *[]>(new char *[env_count + 1 ]);
111+ options_.env = env.get ();
131112
113+ // Iterate over environment variables once to store them in the current
114+ // ProcessRunner instance.
132115 for (int i = 0 ; i < env_count; i++) {
116+ std::string name = env_items[i].name ;
117+ auto value = env_items[i].value ;
118+
119+ #ifdef _WIN32
120+ // We use comspec environment variable to find cmd.exe path on Windows
121+ // Example: 'C:\\Windows\\system32\\cmd.exe'
122+ // If we don't find it, we fallback to 'cmd.exe' for Windows
123+ if (StringEqualNoCase (name.c_str (), " comspec" )) {
124+ file_ = value;
125+ }
126+ #endif // _WIN32
127+
128+ // Check if environment variable key is matching case-insensitive "path"
129+ if (StringEqualNoCase (name.c_str (), " path" )) {
130+ env_vars_.push_back (name + " =" + bin_path + value);
131+ } else {
132+ // Environment variables should be in "KEY=value" format
133+ env_vars_.push_back (name + " =" + value);
134+ }
135+ }
136+ uv_os_free_environ (env_items, env_count);
137+
138+ // Add NODE_LIFECYCLE_EVENT environment variable to the environment
139+ // to indicate which script is being run.
140+ env_vars_.push_back (" NODE_LIFECYCLE_EVENT=" + script_name);
141+
142+ for (size_t i = 0 ; i < env_vars_.size (); i++) {
133143 options_.env [i] = const_cast <char *>(env_vars_[i].c_str ());
134144 }
135- options_.env [env_count ] = nullptr ;
145+ options_.env [env_vars_. size () ] = nullptr ;
136146}
137147
138148// EscapeShell escapes a string to be used as a command line argument.
@@ -276,7 +286,8 @@ void RunTask(std::shared_ptr<InitializationResultImpl> result,
276286 return ;
277287 }
278288
279- auto runner = ProcessRunner (result, command, positional_args);
289+ auto runner =
290+ ProcessRunner (result, std::string (command_id), command, positional_args);
280291 runner.Run ();
281292}
282293
0 commit comments