Skip to content

Commit 3190ae1

Browse files
committed
src: port --bash-completion to C++
So that it gets handle earlier and faster during the bootstrap process. Drive-by fixes: - Remove `[has_eval_string]` and `[ssl_openssl_cert_store]` from the completion output - Set `kProfProcess` execution mode for `--prof-process` instead of `kPrintBashProcess` which is removed in this patch. - Append new line to the end of the output of --bash-completion
1 parent 8b78fbd commit 3190ae1

File tree

5 files changed

+53
-33
lines changed

5 files changed

+53
-33
lines changed

lib/internal/main/print_bash_completion.js

-29
This file was deleted.

node.gyp

-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@
138138
'lib/internal/main/eval_string.js',
139139
'lib/internal/main/eval_stdin.js',
140140
'lib/internal/main/inspect.js',
141-
'lib/internal/main/print_bash_completion.js',
142141
'lib/internal/main/print_help.js',
143142
'lib/internal/main/prof_process.js',
144143
'lib/internal/main/repl.js',

src/node.cc

+12-3
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,6 @@ MaybeLocal<Value> StartMainThreadExecution(Environment* env) {
396396
return StartExecution(env, "internal/main/print_help");
397397
}
398398

399-
if (per_process::cli_options->print_bash_completion) {
400-
return StartExecution(env, "internal/main/print_bash_completion");
401-
}
402399

403400
if (env->options()->prof_process) {
404401
return StartExecution(env, "internal/main/prof_process");
@@ -771,6 +768,12 @@ void Init(int* argc,
771768
exit(0);
772769
}
773770

771+
if (per_process::cli_options->print_bash_completion) {
772+
std::string completion = options_parser::GetBashCompletion();
773+
printf("%s\n", completion.c_str());
774+
exit(0);
775+
}
776+
774777
if (per_process::cli_options->print_v8_help) {
775778
V8::SetFlagsFromString("--help", 6); // Doesn't return.
776779
UNREACHABLE();
@@ -829,6 +832,12 @@ InitializationResult InitializeOncePerProcess(int argc, char** argv) {
829832
return result;
830833
}
831834

835+
if (per_process::cli_options->print_bash_completion) {
836+
std::string completion = options_parser::GetBashCompletion();
837+
printf("%s\n", completion.c_str());
838+
exit(0);
839+
}
840+
832841
if (per_process::cli_options->print_v8_help) {
833842
V8::SetFlagsFromString("--help", 6); // Doesn't return.
834843
UNREACHABLE();

src/node_options.cc

+39
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "env-inl.h"
55
#include "node_binding.h"
66

7+
#include <errno.h>
8+
#include <sstream>
79
#include <cstdlib> // strtoul, errno
810

911
using v8::Boolean;
@@ -678,6 +680,43 @@ HostPort SplitHostPort(const std::string& arg,
678680
ParseAndValidatePort(arg.substr(colon + 1), errors) };
679681
}
680682

683+
std::string GetBashCompletion() {
684+
Mutex::ScopedLock lock(per_process::cli_options_mutex);
685+
const auto& parser = _ppop_instance;
686+
687+
std::ostringstream out;
688+
689+
out << "_node_complete() {\n"
690+
" local cur_word options\n"
691+
" cur_word=\"${COMP_WORDS[COMP_CWORD]}\"\n"
692+
" if [[ \"${cur_word}\" == -* ]] ; then\n"
693+
" COMPREPLY=( $(compgen -W '";
694+
695+
for (const auto& item : parser.options_) {
696+
if (item.first[0] != '[') {
697+
out << item.first << " ";
698+
}
699+
}
700+
for (const auto& item : parser.aliases_) {
701+
if (item.first[0] != '[') {
702+
out << item.first << " ";
703+
}
704+
}
705+
if (parser.aliases_.size() > 0) {
706+
out.seekp(-1, out.cur); // Strip the trailing space
707+
}
708+
709+
out << "' -- \"${cur_word}\") )\n"
710+
" return 0\n"
711+
" else\n"
712+
" COMPREPLY=( $(compgen -f \"${cur_word}\") )\n"
713+
" return 0\n"
714+
" fi\n"
715+
"}\n"
716+
"complete -F _node_complete node node_g";
717+
return out.str();
718+
}
719+
681720
// Return a map containing all the options and their metadata as well
682721
// as the aliases
683722
void GetOptions(const FunctionCallbackInfo<Value>& args) {

src/node_options.h

+2
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ namespace options_parser {
224224
HostPort SplitHostPort(const std::string& arg,
225225
std::vector<std::string>* errors);
226226
void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args);
227+
std::string GetBashCompletion();
227228

228229
enum OptionEnvvarSettings {
229230
kAllowedInEnvironment,
@@ -412,6 +413,7 @@ class OptionsParser {
412413
friend class OptionsParser;
413414

414415
friend void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args);
416+
friend std::string GetBashCompletion();
415417
};
416418

417419
using StringVector = std::vector<std::string>;

0 commit comments

Comments
 (0)