• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pkgxdev / pkgx / 6605283511

22 Oct 2023 05:39PM UTC coverage: 97.311% (-0.5%) from 97.833%
6605283511

push

github

mxcl
Have command not found handler sync

Sync is quick if they have done it recently. Let’s see how this feels.

Rationale: We add new pkgs all the time.

389 of 408 branches covered (0.0%)

2 of 5 new or added lines in 1 file covered. (40.0%)

2 existing lines in 1 file now uncovered.

1384 of 1414 relevant lines covered (97.88%)

137.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.67
/src/modes/shellcode.ts
1
import useConfig from 'pkgx/hooks/useConfig.ts'
2✔
2
import undent from 'outdent'
2✔
3
import { Path } from 'pkgx'
2✔
4
import { flatmap } from "pkgx/utils/misc.ts";
2✔
5

6
// NOTES
2✔
7
// * is safely re-entrant (and idempotent)
2✔
8
// * we `eval` for BASH/ZSH because otherwise parsing fails for POSIX `sh`
2✔
9
// * we add `~/.local/bin` to `PATH` because we eg `npm i -g` is configured to install there
2✔
10
// * `command_not_found_handler` cannot change the global environment hence we write a file
2✔
11

12
// TODO
2✔
13
//   ref https://gitlab.freedesktop.org/xdg/xdg-specs/-/issues/14
2✔
14
// * remove the files we create for command not found handler once any prompt appears
2✔
15
// * need to use a proper tmp location perhaps
2✔
16

17
export default function() {
2✔
18
  const blurple = (x: string) => `\\033[38;5;63m${x}\\033[0m`
27✔
19
  const dim = (x: string) => `\\e[2m${x}\\e[0m`
27✔
20
  const datadir = useConfig().data.join("dev")
27✔
21
  const tmp = (flatmap(Deno.env.get("XDG_STATE_HOME"), Path.abs) ?? platform_state_default()).join("pkgx")
27✔
22
  const sh = '${SHELL:-/bin/sh}'
27✔
23

24
  return undent`
27✔
25
    pkgx() {
26
      case "$1" in
27
      install)
28
        if [ $# -gt 1 ]; then
29
          command pkgx "$@"
30
        elif type _pkgx_install >/dev/null 2>&1; then
31
          _pkgx_install
32
        else
33
          echo "pkgx: nothing to install" >&2
34
          return 1
35
        fi;;
36
      unload)
37
        if type _pkgx_reset >/dev/null 2>&1; then
38
          _pkgx_reset
39
        fi
40
        unset -f _pkgx_chpwd_hook _pkgx_should_deactivate_devenv pkgx x command_not_found_handler command_not_found_handle pkgx@latest _pkgx_commit _pkgx_dev_off >/dev/null 2>&1
41
        echo "pkgx: shellcode unloaded" >&2;;
42
      *)
43
        command pkgx "$@";;
44
      esac
45
    }
46

47
    x() {
48
      case $1 in
49
      "")
50
        if [ -f "${tmp}/shellcode/x.$$" ]; then
27✔
51
          if foo="$("${tmp}/shellcode/u.$$")"; then
27✔
52
            eval "$foo"
53
            ${sh} "${tmp}/shellcode/x.$$"
27✔
54
          fi
55
          rm "${tmp}/shellcode/"?.$$
27✔
56
        else
57
          echo "pkgx: nothing to run" >&2
58
          return 1
59
        fi;;
60
      *)
61
        command pkgx -- "$@";;
62
      esac
63
    }
64

65
    env() {
66
      for arg in "$@"; do
67
        case $arg in
68
        --*)
69
          command env "$@"
70
          return;;
71
        -*);;
72
        +*);;
73
        *)
74
          command env "$@"
75
          return;;
76
        esac
77
      done
78
      if [ $# -eq 0 ]; then
79
        command env
80
      fi
81
      if type _pkgx_reset >/dev/null 2>&1; then
82
        _pkgx_reset
83
      fi
84
      eval "$(command pkgx --internal.use "$@")"
85
    }
86

87
    dev() {
88
      if [ "$1" = 'off' ]; then
89
        _pkgx_dev_off
90
      elif type _pkgx_dev_off >/dev/null 2>&1; then
91
        echo 'dev: environment already active' >&2
92
        return 1
93
      else
94
        if type _pkgx_reset >/dev/null 2>&1; then
95
          _pkgx_reset
96
        fi
97
        eval "$(command pkgx --internal.activate "$PWD" "$@")"
98
      fi
99
    }
100

101
    command_not_found_handler() {
102
      if [ "$1" = pkgx ]; then
103
        echo 'fatal: \`pkgx\` not in PATH' >&2
104
        return 1
105
      elif command pkgx --sync --keep-going --silent --provider "$1"; then
106
        echo -e '${dim('^^ type `')}x${dim('` to run that')}' >&2
27✔
107

108
        d="${tmp}/shellcode"
27✔
109
        mkdir -p "$d"
110
        echo "#!${sh}" > "$d/u.$$"
27✔
111
        echo "echo -e \\"${blurple('env')} +$1 ${dim('&&')} $@ \\" >&2" >> "$d/u.$$"
27✔
112
        echo "exec pkgx --internal.use +\\"$1\\"" >> "$d/u.$$"
113
        chmod u+x "$d/u.$$"
114
        echo -n "exec " > "$d/x.$$"
115
        for arg in "$@"; do
116
          printf "%q " "$arg" >> "$d/x.$$"
117
        done
118

119
        return 127
120
      else
121
        echo "cmd not found: $1" >&2
122
        return 127
123
      fi
124
    }
125

126
    _pkgx_chpwd_hook() {
127
      if _pkgx_should_deactivate_devenv >/dev/null 2>&1; then
128
        _pkgx_dev_off --shy
129
      fi
130
      if ! type _pkgx_dev_off >/dev/null 2>&1; then
131
        dir="$PWD"
132
        while [ "$dir" != "/" ]; do
133
          if [ -f "${datadir}/$dir/dev.pkgx.activated" ]; then
27✔
134
            if type _pkgx_reset >/dev/null 2>&1; then
135
              _pkgx_reset
136
            fi
137
            eval "$(command pkgx --internal.activate "$dir")"
138
            break
139
          fi
140
          dir="$(dirname "$dir")"
141
        done
142
      fi
143
    }
144

145
    if [ -n "$ZSH_VERSION" ] && [ $(emulate) = zsh ]; then
146
      eval 'typeset -ag chpwd_functions
147

148
            if [[ -z "\${chpwd_functions[(r)_pkgx_chpwd_hook]+1}" ]]; then
149
              chpwd_functions=( _pkgx_chpwd_hook \${chpwd_functions[@]} )
150
            fi
151

152
            if [ "$TERM_PROGRAM" != Apple_Terminal ]; then
153
              _pkgx_chpwd_hook
154
            fi
155

156
            _pkgx() {
157
              local words
158
              words=($(pkgx --shell-completion $1))
159
              reply=($words)
160
            }
161
            compctl -K _pkgx pkgx'
162
    elif [ -n "$BASH_VERSION" ] && [ "$POSIXLY_CORRECT" != y ] ; then
163
      eval 'cd() {
164
              builtin cd "$@" || return
165
              _pkgx_chpwd_hook
166
            }
167

168
            command_not_found_handle() {
169
              command_not_found_handler "$@"
170
            }
171

172
            _pkgx_chpwd_hook'
173
    else
174
      POSIXLY_CORRECT=y
175
      echo "pkgx: warning: unsupported shell" >&2
176
    fi
177

178
    if [ "$POSIXLY_CORRECT" != y ]; then
179
      eval 'pkgx@latest() {
180
              command pkgx pkgx@latest "$@"
181
            }'
182
      if [[ "$PATH" != *"$HOME/.local/bin"* ]]; then
183
        export PATH="$HOME/.local/bin:$PATH"
184
      fi
185
    fi
186
    `
27✔
187
}
27✔
188

189
function platform_state_default() {
27✔
190
  switch (Deno.build.os) {
27✔
191
  case 'darwin':
14!
192
    return Path.home().join("Library/Application Support")
14✔
UNCOV
193
  case "windows":
×
UNCOV
194
    return new Path(Deno.env.get("LOCALAPPDATA")!)
×
195
  default:
13!
196
    return Path.home().join(".local", "state")
13✔
197
  }
27✔
198
}
27✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc