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

pkgxdev / pkgx / 10854498834

13 Sep 2024 06:47PM UTC coverage: 91.505% (-1.2%) from 92.709%
10854498834

push

github

mxcl
fix regression in construct-env.ts

closes #1036

373 of 407 branches covered (91.65%)

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

20 existing lines in 4 files now uncovered.

1469 of 1606 relevant lines covered (91.47%)

81.78 hits per line

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

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

6
// NOTES
1✔
7
// * is safely re-entrant (and idempotent)
1✔
8
// * we `eval` for BASH/ZSH because otherwise parsing fails for POSIX `sh`
1✔
9
// * we `eval` for conditional functions since weirdly zsh parses code it isn’t actually branching on
1✔
10
// * we add `~/.local/bin` to `PATH` because we eg `npm i -g` is configured to install there
1✔
11
// * `command_not_found_handler` cannot change the global environment hence we write a file
1✔
12

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

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

25
  return undent`
13✔
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
13✔
53
            if foo="$("${tmp}/shellcode/u.$$")"; then
13✔
54
              eval "$foo"
55
              ${sh} "${tmp}/shellcode/x.$$"
13✔
56
              unset foo
57
            fi
58
            rm -f "${tmp}/shellcode/"?.$$
13✔
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
        if type _pkgx_dev_off >/dev/null 2>&1; then
94
          _pkgx_dev_off
95
        else
96
          echo 'dev: environment not active' >&2
97
          return 1
98
        fi
99
      elif type _pkgx_dev_off >/dev/null 2>&1; then
100
        echo 'dev: environment already active' >&2
101
        return 1
102
      else
103
        if type _pkgx_reset >/dev/null 2>&1; then
104
          _pkgx_reset
105
        fi
106
        eval "$(command pkgx --internal.activate "$PWD" "$@")"
107
      fi
108
    }
109

110
    _pkgx_provider() {
111
      if ! command pkgx --silent --provider "$1"; then
112
        command pkgx --sync --keep-going --silent --provider "$1"
113
      fi
114
    }
115

116
    command_not_found_handler() {
117
      if [ $1 = pkgx ]; then
118
        echo 'fatal: \`pkgx\` not in PATH' >&2
119
        return 1
120
      elif [ -t 2 ] && _pkgx_provider $1; then
121
        echo -e '${dim('^^ type `')}x${dim('` to run that')}' >&2
13✔
122

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

134
        return 127
135
      else
136
        echo "cmd not found: $1" >&2
137
        return 127
138
      fi
139
    }
140

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

160
    if [ -n "$ZSH_VERSION" ] && [ $(emulate) = zsh ]; then
161
      eval 'typeset -ag chpwd_functions
162

163
            if [[ -z "\${chpwd_functions[(r)_pkgx_chpwd_hook]+1}" ]]; then
164
              chpwd_functions=( _pkgx_chpwd_hook \${chpwd_functions[@]} )
165
            fi
166

167
            if [ "$TERM_PROGRAM" != Apple_Terminal ]; then
168
              _pkgx_chpwd_hook
169
            fi
170

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

183
            command_not_found_handle() {
184
              command_not_found_handler "$@"
185
            }
186

187
            _pkgx_chpwd_hook'
188
    else
189
      POSIXLY_CORRECT=y
190
      echo "pkgx: warning: unsupported shell" >&2
191
    fi
192

193
    if [ "$POSIXLY_CORRECT" != y ]; then
194
      eval 'pkgx@latest() {
195
              command pkgx pkgx@latest "$@"
196
            }'
197
      if [[ "$PATH" != *"$HOME/.local/bin"* ]]; then
198
        export PATH="$HOME/.local/bin:$PATH"
199
      fi
200
    fi
201
    `
13✔
202
}
13✔
203

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