1
1
#include " node_task_runner.h"
2
2
#include " util.h"
3
3
4
+ #include < filesystem>
4
5
#include < regex> // NOLINT(build/c++11)
5
6
6
7
namespace node ::task_runner {
@@ -12,7 +13,8 @@ static constexpr const char* bin_path = "/node_modules/.bin";
12
13
#endif // _WIN32
13
14
14
15
ProcessRunner::ProcessRunner (std::shared_ptr<InitializationResultImpl> result,
15
- const std::string& script_name,
16
+ std::string_view package_json_path,
17
+ std::string_view script_name,
16
18
std::string_view command,
17
19
const PositionalArgs& positional_args) {
18
20
memset (&options_, 0 , sizeof (uv_process_options_t ));
@@ -52,7 +54,10 @@ ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
52
54
// callback.
53
55
process_.data = this ;
54
56
55
- SetEnvironmentVariables (current_bin_path, script_name);
57
+ SetEnvironmentVariables (current_bin_path,
58
+ std::string_view (cwd, cwd_size),
59
+ package_json_path,
60
+ script_name);
56
61
57
62
std::string command_str (command);
58
63
@@ -102,7 +107,9 @@ ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
102
107
}
103
108
104
109
void ProcessRunner::SetEnvironmentVariables (const std::string& current_bin_path,
105
- const std::string& script_name) {
110
+ std::string_view cwd,
111
+ std::string_view package_json_path,
112
+ std::string_view script_name) {
106
113
uv_env_item_t * env_items;
107
114
int env_count;
108
115
CHECK_EQ (0 , uv_os_environ (&env_items, &env_count));
@@ -132,7 +139,19 @@ void ProcessRunner::SetEnvironmentVariables(const std::string& current_bin_path,
132
139
133
140
// Add NODE_RUN_SCRIPT_NAME environment variable to the environment
134
141
// to indicate which script is being run.
135
- env_vars_.push_back (" NODE_RUN_SCRIPT_NAME=" + script_name);
142
+ env_vars_.push_back (" NODE_RUN_SCRIPT_NAME=" + std::string (script_name));
143
+
144
+ // Add NODE_RUN_PACKAGE_JSON_PATH environment variable to the environment to
145
+ // indicate which package.json is being processed.
146
+ if (std::filesystem::path (package_json_path).is_absolute ()) {
147
+ // TODO(anonrig): Traverse up the directory tree until we find a
148
+ // package.json
149
+ env_vars_.push_back (" NODE_RUN_PACKAGE_JSON_PATH=" +
150
+ std::string (package_json_path));
151
+ } else {
152
+ auto path = std::filesystem::path (cwd) / std::string (package_json_path);
153
+ env_vars_.push_back (" NODE_RUN_PACKAGE_JSON_PATH=" + path.string ());
154
+ }
136
155
137
156
env = std::unique_ptr<char *[]>(new char *[env_vars_.size () + 1 ]);
138
157
options_.env = env.get ();
@@ -284,7 +303,7 @@ void RunTask(std::shared_ptr<InitializationResultImpl> result,
284
303
}
285
304
286
305
auto runner =
287
- ProcessRunner (result, std::string ( command_id) , command, positional_args);
306
+ ProcessRunner (result, path, command_id, command, positional_args);
288
307
runner.Run ();
289
308
}
290
309
0 commit comments