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

pkgxdev / pkgx / 6914296175

18 Nov 2023 01:41PM UTC coverage: 97.366% (+0.005%) from 97.361%
6914296175

push

github

mxcl
Maybe this will fix #808

396 of 415 branches covered (0.0%)

3 of 3 new or added lines in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

1415 of 1445 relevant lines covered (97.92%)

139.96 hits per line

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

86.96
/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 `eval` for conditional functions since weirdly zsh parses code it isn’t actually branching on
2✔
10
// * we add `~/.local/bin` to `PATH` because we eg `npm i -g` is configured to install there
2✔
11
// * `command_not_found_handler` cannot change the global environment hence we write a file
2✔
12

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

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

25
  return undent`
27✔
26
    pkgx() {
27
      case "$1" in
28
      install)
29
        if [ $# -gt 1 ]; then
30
          command pkgx "$@"
31
        elif type _pkgx_install >/dev/null 2>&1; then
32
          _pkgx_install
33
        else
34
          echo "pkgx: nothing to install" >&2
35
          return 1
36
        fi;;
37
      unload)
38
        if type _pkgx_reset >/dev/null 2>&1; then
39
          _pkgx_reset
40
        fi
41
        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
42
        echo "pkgx: shellcode unloaded" >&2;;
43
      *)
44
        command pkgx "$@";;
45
      esac
46
    }
47

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

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

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

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

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

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

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

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

155
    if [ -n "$ZSH_VERSION" ] && [ $(emulate) = zsh ]; then
156
      eval 'typeset -ag chpwd_functions
157

158
            if [[ -z "\${chpwd_functions[(r)_pkgx_chpwd_hook]+1}" ]]; then
159
              chpwd_functions=( _pkgx_chpwd_hook \${chpwd_functions[@]} )
160
            fi
161

162
            if [ "$TERM_PROGRAM" != Apple_Terminal ]; then
163
              _pkgx_chpwd_hook
164
            fi
165

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

178
            command_not_found_handle() {
179
              command_not_found_handler "$@"
180
            }
181

182
            _pkgx_chpwd_hook'
183
    else
184
      POSIXLY_CORRECT=y
185
      echo "pkgx: warning: unsupported shell" >&2
186
    fi
187

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

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