Skip to content

Commit 5f7688e

Browse files
committed
src: parse dotenv with the rest of the options
1 parent f5f67ae commit 5f7688e

File tree

4 files changed

+33
-62
lines changed

4 files changed

+33
-62
lines changed

src/node.cc

+28-22
Original file line numberDiff line numberDiff line change
@@ -854,28 +854,6 @@ static ExitCode InitializeNodeWithArgsInternal(
854854
HandleEnvOptions(per_process::cli_options->per_isolate->per_env);
855855

856856
std::string node_options;
857-
auto file_paths = node::Dotenv::GetPathFromArgs(*argv);
858-
859-
if (!file_paths.empty()) {
860-
CHECK(!per_process::v8_initialized);
861-
862-
for (const auto& file_path : file_paths) {
863-
switch (per_process::dotenv_file.ParsePath(file_path)) {
864-
case Dotenv::ParseResult::Valid:
865-
break;
866-
case Dotenv::ParseResult::InvalidContent:
867-
errors->push_back(file_path + ": invalid format");
868-
break;
869-
case Dotenv::ParseResult::FileError:
870-
errors->push_back(file_path + ": not found");
871-
break;
872-
default:
873-
UNREACHABLE();
874-
}
875-
}
876-
877-
per_process::dotenv_file.AssignNodeOptionsIfAvailable(&node_options);
878-
}
879857

880858
#if !defined(NODE_WITHOUT_NODE_OPTIONS)
881859
if (!(flags & ProcessInitializationFlags::kDisableNodeOptionsEnv)) {
@@ -912,6 +890,34 @@ static ExitCode InitializeNodeWithArgsInternal(
912890
if (exit_code != ExitCode::kNoFailure) return exit_code;
913891
}
914892

893+
if (!per_process::cli_options->per_isolate->per_env->env_file.empty()) {
894+
CHECK(!per_process::v8_initialized);
895+
896+
for (const auto& file_path :
897+
per_process::cli_options->per_isolate->per_env->env_file) {
898+
switch (per_process::dotenv_file.ParsePath(file_path)) {
899+
case Dotenv::ParseResult::Valid:
900+
break;
901+
case Dotenv::ParseResult::InvalidContent:
902+
errors->push_back(file_path + ": invalid format");
903+
break;
904+
case Dotenv::ParseResult::FileError:
905+
errors->push_back(file_path + ": not found");
906+
break;
907+
default:
908+
UNREACHABLE();
909+
}
910+
}
911+
912+
std::vector<std::string> env_argv = ParseNodeOptionsEnvVar(
913+
per_process::dotenv_file.GetNodeOptions(), errors);
914+
env_argv.insert(env_argv.begin(), argv->at(0));
915+
916+
const ExitCode exit_code =
917+
ProcessGlobalArgsInternal(&env_argv, nullptr, errors, kAllowedInEnvvar);
918+
if (exit_code != ExitCode::kNoFailure) return exit_code;
919+
}
920+
915921
// Set the process.title immediately after processing argv if --title is set.
916922
if (!per_process::cli_options->title.empty())
917923
uv_set_process_title(per_process::cli_options->title.c_str());

src/node_dotenv.cc

+3-38
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,6 @@ using v8::NewStringType;
1111
using v8::Object;
1212
using v8::String;
1313

14-
std::vector<std::string> Dotenv::GetPathFromArgs(
15-
const std::vector<std::string>& args) {
16-
const auto find_match = [](const std::string& arg) {
17-
return arg == "--" || arg == "--env-file" || arg.starts_with("--env-file=");
18-
};
19-
std::vector<std::string> paths;
20-
auto path = std::find_if(args.begin(), args.end(), find_match);
21-
22-
while (path != args.end()) {
23-
if (*path == "--") {
24-
return paths;
25-
}
26-
auto equal_char = path->find('=');
27-
28-
if (equal_char != std::string::npos) {
29-
paths.push_back(path->substr(equal_char + 1));
30-
} else {
31-
auto next_path = std::next(path);
32-
33-
if (next_path == args.end()) {
34-
return paths;
35-
}
36-
37-
paths.push_back(*next_path);
38-
}
39-
40-
path = std::find_if(++path, args.end(), find_match);
41-
}
42-
43-
return paths;
44-
}
45-
4614
void Dotenv::SetEnvironment(node::Environment* env) {
4715
auto isolate = env->isolate();
4816

@@ -261,12 +229,9 @@ Dotenv::ParseResult Dotenv::ParsePath(const std::string_view path) {
261229
return ParseResult::Valid;
262230
}
263231

264-
void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) const {
265-
auto match = store_.find("NODE_OPTIONS");
266-
267-
if (match != store_.end()) {
268-
*node_options = match->second;
269-
}
232+
std::string Dotenv::GetNodeOptions() const {
233+
auto it = store_.find("NODE_OPTIONS");
234+
return (it != store_.end()) ? it->second : "";
270235
}
271236

272237
} // namespace node

src/node_dotenv.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Dotenv {
2323

2424
void ParseContent(const std::string_view content);
2525
ParseResult ParsePath(const std::string_view path);
26-
void AssignNodeOptionsIfAvailable(std::string* node_options) const;
26+
std::string GetNodeOptions() const;
2727
void SetEnvironment(Environment* env);
2828
v8::Local<v8::Object> ToObject(Environment* env) const;
2929

src/node_options.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class EnvironmentOptions : public Options {
176176
#endif // HAVE_INSPECTOR
177177
std::string redirect_warnings;
178178
std::string diagnostic_dir;
179-
std::string env_file;
179+
std::vector<std::string> env_file;
180180
bool has_env_file_string = false;
181181
bool test_runner = false;
182182
uint64_t test_runner_concurrency = 0;

0 commit comments

Comments
 (0)