@@ -12,6 +12,7 @@ static constexpr const char* bin_path = "/node_modules/.bin";
12
12
#endif // _WIN32
13
13
14
14
ProcessRunner::ProcessRunner (std::shared_ptr<InitializationResultImpl> result,
15
+ const std::string& script_name,
15
16
std::string_view command,
16
17
const PositionalArgs& positional_args) {
17
18
memset (&options_, 0 , sizeof (uv_process_options_t ));
@@ -51,39 +52,9 @@ ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
51
52
// callback.
52
53
process_.data = this ;
53
54
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);
77
56
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);
87
58
88
59
// Use the stored reference on the instance.
89
60
options_.file = file_.c_str ();
@@ -128,11 +99,49 @@ ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
128
99
options_.args [i] = const_cast <char *>(command_args_[i].c_str ());
129
100
}
130
101
options_.args [argc] = nullptr ;
102
+ }
103
+
104
+ void ProcessRunner::SetEnvironmentVariables (const std::string& bin_path, const std::string& script_name) {
105
+ // Set environment variables
106
+ uv_env_item_t * env_items;
107
+ int env_count;
108
+ CHECK_EQ (0 , uv_os_environ (&env_items, &env_count));
109
+ env = std::unique_ptr<char *[]>(new char *[env_count + 1 ]);
110
+ options_.env = env.get ();
131
111
112
+ // Iterate over environment variables once to store them in the current
113
+ // ProcessRunner instance.
132
114
for (int i = 0 ; i < env_count; i++) {
115
+ std::string name = env_items[i].name ;
116
+ auto value = env_items[i].value ;
117
+
118
+ #ifdef _WIN32
119
+ // We use comspec environment variable to find cmd.exe path on Windows
120
+ // Example: 'C:\\Windows\\system32\\cmd.exe'
121
+ // If we don't find it, we fallback to 'cmd.exe' for Windows
122
+ if (StringEqualNoCase (name.c_str (), " comspec" )) {
123
+ file_ = value;
124
+ }
125
+ #endif // _WIN32
126
+
127
+ // Check if environment variable key is matching case-insensitive "path"
128
+ if (StringEqualNoCase (name.c_str (), " path" )) {
129
+ env_vars_.push_back (name + " =" + bin_path + value);
130
+ } else {
131
+ // Environment variables should be in "KEY=value" format
132
+ env_vars_.push_back (name + " =" + value);
133
+ }
134
+ }
135
+ uv_os_free_environ (env_items, env_count);
136
+
137
+ // Add NODE_LIFECYCLE_EVENT environment variable to the environment
138
+ // to indicate which script is being run.
139
+ env_vars_.push_back (" NODE_LIFECYCLE_EVENT=" + script_name);
140
+
141
+ for (size_t i = 0 ; i < env_vars_.size (); i++) {
133
142
options_.env [i] = const_cast <char *>(env_vars_[i].c_str ());
134
143
}
135
- options_.env [env_count ] = nullptr ;
144
+ options_.env [env_vars_. size () ] = nullptr ;
136
145
}
137
146
138
147
// EscapeShell escapes a string to be used as a command line argument.
@@ -276,7 +285,7 @@ void RunTask(std::shared_ptr<InitializationResultImpl> result,
276
285
return ;
277
286
}
278
287
279
- auto runner = ProcessRunner (result, command, positional_args);
288
+ auto runner = ProcessRunner (result, std::string (command_id), command, positional_args);
280
289
runner.Run ();
281
290
}
282
291
0 commit comments