@treeport/treeport 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,256 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ //#region src/server/core/shell-integration.ts
4
+ const ZSH_DELEGATE = (file) => `if [[ -r "$TREEPORT_USER_ZDOTDIR/${file}" ]]; then
5
+ _treeport_integration_zdotdir=$ZDOTDIR
6
+ ZDOTDIR=$TREEPORT_USER_ZDOTDIR
7
+ source "$ZDOTDIR/${file}"
8
+ TREEPORT_USER_ZDOTDIR=$ZDOTDIR
9
+ ZDOTDIR=$_treeport_integration_zdotdir
10
+ unset _treeport_integration_zdotdir
11
+ fi
12
+ `;
13
+ const ZSH_INTEGRATION = `${ZSH_DELEGATE(".zshrc")}
14
+ if [[ -n "$TMUX_PANE" && -n "$TREEPORT_TMUX_EXECUTABLE" ]]; then
15
+ _treeport_tmux_executable=$TREEPORT_TMUX_EXECUTABLE
16
+ unset TREEPORT_TMUX_EXECUTABLE
17
+ autoload -Uz add-zsh-hook 2>/dev/null
18
+ _treeport_command_title_preexec() {
19
+ emulate -L zsh
20
+ local title=\${1//[[:cntrl:]]/}
21
+ title=\${title[1,256]}
22
+ "$_treeport_tmux_executable" set-option -p -t "$TMUX_PANE" -- @treeport-command "$title" >/dev/null 2>&1
23
+ }
24
+ _treeport_command_title_precmd() {
25
+ emulate -L zsh
26
+ "$_treeport_tmux_executable" set-option -p -u -t "$TMUX_PANE" @treeport-command >/dev/null 2>&1
27
+ }
28
+ add-zsh-hook -d preexec _treeport_command_title_preexec 2>/dev/null
29
+ add-zsh-hook -d precmd _treeport_command_title_precmd 2>/dev/null
30
+ add-zsh-hook preexec _treeport_command_title_preexec
31
+ add-zsh-hook precmd _treeport_command_title_precmd
32
+ fi
33
+
34
+ ZDOTDIR=$TREEPORT_USER_ZDOTDIR
35
+ unset TREEPORT_USER_ZDOTDIR
36
+ `;
37
+ const BASH_INTEGRATION = `[[ $- == *i* ]] || return
38
+
39
+ _treeport_bash_injected_prompt='source "\${TREEPORT_BASH_INTEGRATION_FILE}"'
40
+ _treeport_bash_remaining_prompt=\${PROMPT_COMMAND//"$_treeport_bash_injected_prompt"/}
41
+ _treeport_bash_remaining_prompt=\${_treeport_bash_remaining_prompt#;}
42
+ _treeport_bash_remaining_prompt=\${_treeport_bash_remaining_prompt%;}
43
+ if [[ -n "\${TREEPORT_BASH_PROMPT_COMMAND_SET-}" ]]; then
44
+ PROMPT_COMMAND=$TREEPORT_BASH_PROMPT_COMMAND
45
+ [[ -n "$_treeport_bash_remaining_prompt" ]] && PROMPT_COMMAND="\${PROMPT_COMMAND:+$PROMPT_COMMAND;}$_treeport_bash_remaining_prompt"
46
+ else
47
+ PROMPT_COMMAND=$_treeport_bash_remaining_prompt
48
+ fi
49
+ unset TREEPORT_BASH_INTEGRATION_FILE TREEPORT_BASH_PROMPT_COMMAND TREEPORT_BASH_PROMPT_COMMAND_SET
50
+ unset _treeport_bash_injected_prompt _treeport_bash_remaining_prompt
51
+
52
+ _treeport_tmux_executable=$TREEPORT_TMUX_EXECUTABLE
53
+ unset TREEPORT_TMUX_EXECUTABLE
54
+ _treeport_command_title_preexec() {
55
+ local fallback=\${1-} line title
56
+ line=$(HISTTIMEFORMAT= builtin history 1)
57
+ line="\${line#"\${line%%[![:space:]]*}"}"
58
+ line="\${line#* }"
59
+ title="\${line#"\${line%%[![:space:]]*}"}"
60
+ [[ -n "$title" ]] || title=$fallback
61
+ title=$(LC_ALL=C printf '%s' "$title" | tr -d '\\000-\\037\\177')
62
+ title="\${title:0:256}"
63
+ [[ -n "$TMUX_PANE" && -n "$_treeport_tmux_executable" ]] || return
64
+ "$_treeport_tmux_executable" set-option -p -t "$TMUX_PANE" -- @treeport-command "$title" >/dev/null 2>&1
65
+ }
66
+ _treeport_command_title_prompt() {
67
+ if [[ -n "$TMUX_PANE" && -n "$_treeport_tmux_executable" ]]; then
68
+ "$_treeport_tmux_executable" set-option -p -u -t "$TMUX_PANE" @treeport-command >/dev/null 2>&1
69
+ fi
70
+ [[ -n "\${_treeport_uses_debug_trap-}" ]] && _treeport_command_ready=1
71
+ }
72
+
73
+ # PS0 provides a non-invasive preexec boundary in Bash 4.4 and newer. On
74
+ # older Bash, use DEBUG only when doing so will not replace a user trap.
75
+ _treeport_title_integration_ready=
76
+ if (( BASH_VERSINFO[0] > 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 4) )); then
77
+ PS0='$(_treeport_command_title_preexec)'"\${PS0-}"
78
+ _treeport_title_integration_ready=1
79
+ elif [[ -z "$(trap -p DEBUG)" ]]; then
80
+ _treeport_uses_debug_trap=1
81
+ _treeport_command_ready=0
82
+ trap 'if [[ $_treeport_command_ready == 1 ]]; then _treeport_command_ready=0; _treeport_command_title_preexec "$BASH_COMMAND"; fi' DEBUG
83
+ _treeport_title_integration_ready=1
84
+ fi
85
+ if [[ -n "$_treeport_title_integration_ready" ]]; then
86
+ if [[ $(declare -p PROMPT_COMMAND 2>/dev/null) == 'declare -a'* ]]; then
87
+ PROMPT_COMMAND=("\${PROMPT_COMMAND[@]}" _treeport_command_title_prompt)
88
+ else
89
+ PROMPT_COMMAND="\${PROMPT_COMMAND:+$PROMPT_COMMAND;}_treeport_command_title_prompt"
90
+ fi
91
+ fi
92
+ unset _treeport_title_integration_ready
93
+ if [[ -n "\${_treeport_uses_debug_trap-}" ]]; then
94
+ _treeport_command_ready=1
95
+ fi
96
+ `;
97
+ const FISH_INTEGRATION = `if set -q TREEPORT_FISH_XDG_DATA_DIR
98
+ if set -q TREEPORT_FISH_XDG_DATA_DIRS_SET
99
+ set -l treeport_xdg_dirs
100
+ for treeport_xdg_dir in (string split : -- "$XDG_DATA_DIRS")
101
+ if test "$treeport_xdg_dir" != "$TREEPORT_FISH_XDG_DATA_DIR"
102
+ set -a treeport_xdg_dirs "$treeport_xdg_dir"
103
+ end
104
+ end
105
+ set -gx XDG_DATA_DIRS (string join : -- $treeport_xdg_dirs)
106
+ else
107
+ set -e XDG_DATA_DIRS
108
+ end
109
+ set -e TREEPORT_FISH_XDG_DATA_DIR TREEPORT_FISH_XDG_DATA_DIRS_SET
110
+ end
111
+ set -g _treeport_tmux_executable "$TREEPORT_TMUX_EXECUTABLE"
112
+ set -e TREEPORT_TMUX_EXECUTABLE
113
+
114
+ function _treeport_command_title_preexec --on-event fish_preexec
115
+ set -l title (string replace -ar '[[:cntrl:]]' '' -- "$argv[1]" | string sub -l 256)
116
+ if test -n "$TMUX_PANE"; and test -n "$_treeport_tmux_executable"
117
+ command "$_treeport_tmux_executable" set-option -p -t "$TMUX_PANE" -- @treeport-command "$title" >/dev/null 2>&1
118
+ end
119
+ end
120
+
121
+ function _treeport_command_title_prompt --on-event fish_prompt
122
+ if test -n "$TMUX_PANE"; and test -n "$_treeport_tmux_executable"
123
+ command "$_treeport_tmux_executable" set-option -p -u -t "$TMUX_PANE" @treeport-command >/dev/null 2>&1
124
+ end
125
+ end
126
+ `;
127
+ async function prepareShellIntegration(root) {
128
+ const zshDir = path.join(root, "zsh");
129
+ const bashDir = path.join(root, "bash");
130
+ const fishDir = path.join(root, "fish", "fish", "vendor_conf.d");
131
+ await Promise.all([
132
+ fs.mkdir(zshDir, {
133
+ recursive: true,
134
+ mode: 448
135
+ }),
136
+ fs.mkdir(bashDir, {
137
+ recursive: true,
138
+ mode: 448
139
+ }),
140
+ fs.mkdir(fishDir, {
141
+ recursive: true,
142
+ mode: 448
143
+ })
144
+ ]);
145
+ await Promise.all([
146
+ ...[
147
+ ".zshenv",
148
+ ".zprofile",
149
+ ".zlogin",
150
+ ".zlogout"
151
+ ].map((file) => fs.writeFile(path.join(zshDir, file), ZSH_DELEGATE(file), { mode: 384 })),
152
+ fs.writeFile(path.join(zshDir, ".zshrc"), ZSH_INTEGRATION, { mode: 384 }),
153
+ fs.writeFile(path.join(bashDir, "treeport.bash"), BASH_INTEGRATION, { mode: 384 }),
154
+ fs.writeFile(path.join(fishDir, "treeport.fish"), FISH_INTEGRATION, { mode: 384 })
155
+ ]);
156
+ }
157
+ function integrateShellLaunch(argv, env, root, tmuxExecutable) {
158
+ const shell = path.basename(argv[0] ?? "").replace(/^-/, "");
159
+ if (!root || !tmuxExecutable || ![
160
+ "zsh",
161
+ "bash",
162
+ "fish"
163
+ ].includes(shell)) return {
164
+ argv,
165
+ env
166
+ };
167
+ let positional = false;
168
+ if (shell !== "bash") {
169
+ let hasCommandOption = false;
170
+ for (const argument of argv.slice(1)) {
171
+ if (positional) {
172
+ hasCommandOption = argument !== "-";
173
+ break;
174
+ }
175
+ if (argument === "--") positional = true;
176
+ else if (argument === "--command" || argument.startsWith("--command=") || argument.startsWith("-") && !argument.startsWith("--") && argument.slice(1).includes("c")) {
177
+ hasCommandOption = true;
178
+ break;
179
+ } else if (!argument.startsWith("-") && argument !== "-") {
180
+ hasCommandOption = true;
181
+ break;
182
+ }
183
+ }
184
+ if (hasCommandOption) return {
185
+ argv,
186
+ env
187
+ };
188
+ }
189
+ const integratedEnv = {
190
+ ...env,
191
+ TREEPORT_TMUX_EXECUTABLE: tmuxExecutable
192
+ };
193
+ if (shell === "zsh") {
194
+ const integrationZdotDir = path.join(root, "zsh");
195
+ const configuredZdotDir = env.ZDOTDIR?.trim();
196
+ const userZdotDir = configuredZdotDir && path.resolve(configuredZdotDir) !== path.resolve(integrationZdotDir) ? configuredZdotDir : env.HOME?.trim();
197
+ if (!userZdotDir) return {
198
+ argv,
199
+ env
200
+ };
201
+ return {
202
+ argv,
203
+ env: {
204
+ ...integratedEnv,
205
+ TREEPORT_USER_ZDOTDIR: userZdotDir,
206
+ ZDOTDIR: integrationZdotDir
207
+ }
208
+ };
209
+ }
210
+ if (shell === "fish") {
211
+ const integrationDataDir = path.join(root, "fish");
212
+ return {
213
+ argv,
214
+ env: {
215
+ ...integratedEnv,
216
+ TREEPORT_FISH_XDG_DATA_DIR: integrationDataDir,
217
+ ...env.XDG_DATA_DIRS !== void 0 ? { TREEPORT_FISH_XDG_DATA_DIRS_SET: "1" } : {},
218
+ XDG_DATA_DIRS: [integrationDataDir, env.XDG_DATA_DIRS].filter(Boolean).join(":")
219
+ }
220
+ };
221
+ }
222
+ positional = false;
223
+ for (const argument of argv.slice(1)) {
224
+ if (positional) {
225
+ if (argument !== "-") return {
226
+ argv,
227
+ env
228
+ };
229
+ continue;
230
+ }
231
+ if (argument === "--") positional = true;
232
+ else if (argument === "--posix" || argument === "--command" || argument.startsWith("--command=") || argument.startsWith("-") && !argument.startsWith("--") && argument.slice(1).includes("c")) return {
233
+ argv,
234
+ env
235
+ };
236
+ else if (!argument.startsWith("-") && argument !== "-") return {
237
+ argv,
238
+ env
239
+ };
240
+ }
241
+ const integrationFile = path.join(root, "bash", "treeport.bash");
242
+ return {
243
+ argv,
244
+ env: {
245
+ ...integratedEnv,
246
+ TREEPORT_BASH_INTEGRATION_FILE: integrationFile,
247
+ ...env.PROMPT_COMMAND !== void 0 ? {
248
+ TREEPORT_BASH_PROMPT_COMMAND_SET: "1",
249
+ TREEPORT_BASH_PROMPT_COMMAND: env.PROMPT_COMMAND
250
+ } : {},
251
+ PROMPT_COMMAND: "source \"${TREEPORT_BASH_INTEGRATION_FILE}\""
252
+ }
253
+ };
254
+ }
255
+ //#endregion
256
+ export { prepareShellIntegration as n, integrateShellLaunch as t };