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

pkgxdev / pkgx / 6613388016

23 Oct 2023 11:44AM UTC coverage: 97.311% (+0.5%) from 96.843%
6613388016

push

github

mxcl
only sync if not found

389 of 408 branches covered (0.0%)

1384 of 1414 relevant lines covered (97.88%)

138.04 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 _pkgx_provider >/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
            unset foo
55
          fi
56
          rm "${tmp}/shellcode/"?.$$
27✔
57
        else
58
          echo "pkgx: nothing to run" >&2
59
          return 1
60
        fi;;
61
      *)
62
        command pkgx -- "$@";;
63
      esac
64
    }
65

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

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

102
    _pkgx_provider() {
103
      if ! command pkgx --silent --provider "$1"; then
104
        command pkgx --sync --keep-going --silent --provider "$1"
105
      fi
106
    }
107

108
    command_not_found_handler() {
109
      if [ $1 = pkgx ]; then
110
        echo 'fatal: \`pkgx\` not in PATH' >&2
111
        return 1
112
      elif [ -t 2 ] && _pkgx_provider $1; then
113
        echo -e '${dim('^^ type `')}x${dim('` to run that')}' >&2
27✔
114

115
        d="${tmp}/shellcode"
27✔
116
        mkdir -p "$d"
117
        echo "#!${sh}" > "$d/u.$$"
27✔
118
        echo "echo -e \\"${blurple('env')} +$1 ${dim('&&')} $@ \\" >&2" >> "$d/u.$$"
27✔
119
        echo "exec pkgx --internal.use +\\"$1\\"" >> "$d/u.$$"
120
        chmod u+x "$d/u.$$"
121
        echo -n "exec " > "$d/x.$$"
122
        for arg in "$@"; do
123
          printf "%q " "$arg" >> "$d/x.$$"
124
        done
125

126
        return 127
127
      else
128
        echo "cmd not found: $1" >&2
129
        return 127
130
      fi
131
    }
132

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

152
    if [ -n "$ZSH_VERSION" ] && [ $(emulate) = zsh ]; then
153
      eval 'typeset -ag chpwd_functions
154

155
            if [[ -z "\${chpwd_functions[(r)_pkgx_chpwd_hook]+1}" ]]; then
156
              chpwd_functions=( _pkgx_chpwd_hook \${chpwd_functions[@]} )
157
            fi
158

159
            if [ "$TERM_PROGRAM" != Apple_Terminal ]; then
160
              _pkgx_chpwd_hook
161
            fi
162

163
            _pkgx() {
164
              local words
165
              words=($(pkgx --shell-completion $1))
166
              reply=($words)
167
            }
168
            compctl -K _pkgx pkgx'
169
    elif [ -n "$BASH_VERSION" ] && [ "$POSIXLY_CORRECT" != y ] ; then
170
      eval 'cd() {
171
              builtin cd "$@" || return
172
              _pkgx_chpwd_hook
173
            }
174

175
            command_not_found_handle() {
176
              command_not_found_handler "$@"
177
            }
178

179
            _pkgx_chpwd_hook'
180
    else
181
      POSIXLY_CORRECT=y
182
      echo "pkgx: warning: unsupported shell" >&2
183
    fi
184

185
    if [ "$POSIXLY_CORRECT" != y ]; then
186
      eval 'pkgx@latest() {
187
              command pkgx pkgx@latest "$@"
188
            }'
189
      if [[ "$PATH" != *"$HOME/.local/bin"* ]]; then
190
        export PATH="$HOME/.local/bin:$PATH"
191
      fi
192
    fi
193
    `
27✔
194
}
27✔
195

196
function platform_state_default() {
27✔
197
  switch (Deno.build.os) {
27✔
198
  case 'darwin':
14!
199
    return Path.home().join("Library/Application Support")
14✔
200
  case "windows":
×
201
    return new Path(Deno.env.get("LOCALAPPDATA")!)
×
202
  default:
13!
203
    return Path.home().join(".local", "state")
13✔
204
  }
27✔
205
}
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