1'use strict' 2 3const Y = require('./y.js') 4 5function mkPosix (opts) { 6 return ` 7command_not_found_${opts.isBash ? 'handle' : 'handler'}() { 8 # Do not run within a pipe 9 if test ! -t 1; then 10 >&2 echo "${Y`command not found: ${'$1'}`}" 11 return 127 12 fi 13 if which npx > /dev/null; then 14 echo "${Y`${'$1'} not found. Trying with npx...`}" >&2 15 else 16 return 127 17 fi 18 if ! [[ $1 =~ @ ]]; then 19 npx --no-install "$@" 20 else 21 npx "$@" 22 fi 23 return $? 24}` 25} 26 27function mkFish (opts) { 28 return ` 29function __fish_command_not_found_on_interactive --on-event fish_prompt 30 functions --erase __fish_command_not_found_handler 31 functions --erase __fish_command_not_found_setup 32 33 function __fish_command_not_found_handler --on-event fish_command_not_found 34 if which npx > /dev/null 35 echo "${Y`${'$argv[1]'} not found. Trying with npx...`}" >&2 36 else 37 return 127 38 end 39 if string match -q -r @ $argv[1] 40 npx $argv 41 else 42 npx --no-install $argv 43 end 44 end 45 46 functions --erase __fish_command_not_found_on_interactive 47end` 48} 49 50module.exports = autoFallback 51function autoFallback (shell, fromEnv, opts) { 52 if (shell.includes('bash')) { 53 return mkPosix({isBash: true, install: opts.install}) 54 } 55 56 if (shell.includes('zsh')) { 57 return mkPosix({isBash: false, install: opts.install}) 58 } 59 60 if (shell.includes('fish')) { 61 return mkFish(opts) 62 } 63 64 if (fromEnv) { 65 return autoFallback(fromEnv, null, opts) 66 } 67 68 console.error(Y`Only Bash, Zsh, and Fish shells are supported :(`) 69} 70