@@ -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,50 @@ 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,
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 ();
131
112
113
+ // Iterate over environment variables once to store them in the current
114
+ // ProcessRunner instance.
132
115
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++) {
133
143
options_.env [i] = const_cast <char *>(env_vars_[i].c_str ());
134
144
}
135
- options_.env [env_count ] = nullptr ;
145
+ options_.env [env_vars_. size () ] = nullptr ;
136
146
}
137
147
138
148
// EscapeShell escapes a string to be used as a command line argument.
@@ -276,7 +286,8 @@ void RunTask(std::shared_ptr<InitializationResultImpl> result,
276
286
return ;
277
287
}
278
288
279
- auto runner = ProcessRunner (result, command, positional_args);
289
+ auto runner =
290
+ ProcessRunner (result, std::string (command_id), command, positional_args);
280
291
runner.Run ();
281
292
}
282
293
0 commit comments