| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 793af1b commit cfcde78
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1879,6 +1879,9 @@ changes: | |||
| 1879 | 1879 | - version: REPLACEME | |
| 1880 | 1880 | pr-url: https://github.com/nodejs/node/pull/53032 | |
| 1881 | 1881 | description: NODE_RUN_SCRIPT_NAME environment variable is added. | |
| 1882 | + - version: REPLACEME | ||
| 1883 | + pr-url: https://github.com/nodejs/node/pull/53058 | ||
| 1884 | + description: NODE_RUN_PACKAGE_JSON_PATH environment variable is added. | ||
| 1882 | 1885 | --> | |
| 1883 | 1886 | ||
| 1884 | 1887 | > Stability: 1.1 - Active development | |
@@ -1925,6 +1928,8 @@ The following environment variables are set when running a script with `--run`: | |||
| 1925 | 1928 | ||
| 1926 | 1929 | * `NODE_RUN_SCRIPT_NAME`: The name of the script being run. For example, if | |
| 1927 | 1930 | `--run` is used to run `test`, the value of this variable will be `test`. | |
| 1931 | + * `NODE_RUN_PACKAGE_JSON_PATH`: The path to the `package.json` that is being | ||
| 1932 | + processed. | ||
| 1928 | 1933 | ||
| 1929 | 1934 | ### `--secure-heap=n` | |
| 1930 | 1935 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 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 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,7 +23,8 @@ using PositionalArgs = std::vector<std::string_view>; | |||
| 23 | 23 | class ProcessRunner { | |
| 24 | 24 | public: | |
| 25 | 25 | ProcessRunner(std::shared_ptr<InitializationResultImpl> result, | |
| 26 | - const std::string& script_name, | ||
| 26 | + std::string_view package_json_path, | ||
| 27 | + std::string_view script_name, | ||
| 27 | 28 | std::string_view command_id, | |
| 28 | 29 | const PositionalArgs& positional_args); | |
| 29 | 30 | void Run(); | |
@@ -45,7 +46,9 @@ class ProcessRunner { | |||
| 45 | 46 | // OnExit is the callback function that is called when the process exits. | |
| 46 | 47 | void OnExit(int64_t exit_status, int term_signal); | |
| 47 | 48 | void SetEnvironmentVariables(const std::string& bin_path, | |
| 48 | - const std::string& script_name); | ||
| 49 | + std::string_view cwd, | ||
| 50 | + std::string_view package_json_path, | ||
| 51 | + std::string_view script_name); | ||
| 49 | 52 | ||
| 50 | 53 | #ifdef _WIN32 | |
| 51 | 54 | std::string file_ = "cmd.exe"; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -83,12 +83,14 @@ describe('node run [command]', () => { | |||
| 83 | 83 | ||
| 84 | 84 | it('should set special environment variables', async () => { | |
| 85 | 85 | const scriptName = `special-env-variables${envSuffix}`; | |
| 86 | + const packageJsonPath = fixtures.path('run-script/package.json'); | ||
| 86 | 87 | const child = await common.spawnPromisified( | |
| 87 | 88 | process.execPath, | |
| 88 | 89 | [ '--no-warnings', '--run', scriptName], | |
| 89 | 90 | { cwd: fixtures.path('run-script') }, | |
| 90 | 91 | ); | |
| 91 | 92 | assert.ok(child.stdout.includes(scriptName)); | |
| 93 | + assert.ok(child.stdout.includes(packageJsonPath)); | ||
| 92 | 94 | assert.strictEqual(child.stderr, ''); | |
| 93 | 95 | assert.strictEqual(child.code, 0); | |
| 94 | 96 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments