This repository has been archived by the owner on Jul 6, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(opts): add --shell-auto-fallback (#7)
Generates shell code that hooks into the "command not found" mechanism and attempts to run the command with npx instead of failing. The option has an optional argument that forces generation of a particular variant, otherwise it attempts to autodetect the shell. However, autodetecting is actually very flaky, so it's better to always specify the argument. To use, place this in relevant shell config file: For Bash: source <(npx --shell-auto-fallback bash) For Zsh: source <(npx --shell-auto-fallback zsh) For Fish: source (npx --shell-auto-fallback fish | psub) As Seen On Twitter: https://twitter.com/passcod/status/869469928474107906
- Loading branch information
Showing
6 changed files
with
199 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
|
||
const POSIX = ` | ||
command_not_found_handler() { | ||
# Do not run within a pipe | ||
if test ! -t 1; then | ||
echo "command not found: $1" | ||
return 127 | ||
fi | ||
echo "Trying with npx..." | ||
npx $* | ||
return $? | ||
}` | ||
|
||
const FISH = ` | ||
function __fish_command_not_found_on_interactive --on-event fish_prompt | ||
functions --erase __fish_command_not_found_handler | ||
functions --erase __fish_command_not_found_setup | ||
function __fish_command_not_found_handler --on-event fish_command_not_found | ||
echo "Trying with npx..." | ||
npx $argv | ||
end | ||
functions --erase __fish_command_not_found_on_interactive | ||
end` | ||
|
||
module.exports = function autoFallback (shell) { | ||
const SHELL = process.env.SHELL || '' | ||
|
||
if (shell === 'bash' || SHELL.includes('bash')) { | ||
return POSIX.replace('handler()', 'handle()') | ||
} | ||
|
||
if (shell === 'zsh' || SHELL.includes('zsh')) { | ||
return POSIX | ||
} | ||
|
||
if (shell === 'fish' || SHELL.includes('fish')) { | ||
return FISH | ||
} | ||
|
||
console.error('Only Bash, Zsh, and Fish shells are supported :(') | ||
process.exit(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
'use strict' | ||
|
||
const exec = require('child_process').exec | ||
const test = require('tap').test | ||
|
||
test('not called with option', (t) => | ||
exec('node .', (err, stdout, stderr) => { | ||
t.equal(err.code, 1) | ||
t.notOk(stdout) | ||
t.match(stderr, /--shell-auto-fallback/) | ||
t.end() | ||
}) | ||
) | ||
|
||
test('detect: SHELL ~= fish', (t) => | ||
exec('node . --shell-auto-fallback', { | ||
env: { | ||
SHELL: '/usr/bin/fish' | ||
} | ||
}, (err, stdout, stderr) => { | ||
if (err) { throw err } | ||
t.match(stdout, /function __fish_command_not_found/) | ||
t.notOk(stderr) | ||
t.end() | ||
}) | ||
) | ||
|
||
test('detect: SHELL ~= bash', (t) => | ||
exec('node . --shell-auto-fallback', { | ||
env: { | ||
SHELL: '/bin/bash' | ||
} | ||
}, (err, stdout, stderr) => { | ||
if (err) { throw err } | ||
t.match(stdout, /command_not_found_handle\(/) | ||
t.notOk(stderr) | ||
t.end() | ||
}) | ||
) | ||
|
||
test('detect: SHELL ~= zsh', (t) => | ||
exec('node . --shell-auto-fallback', { | ||
env: { | ||
SHELL: '/usr/local/bin/zsh' | ||
} | ||
}, (err, stdout, stderr) => { | ||
if (err) { throw err } | ||
t.match(stdout, /command_not_found_handler\(/) | ||
t.notOk(stderr) | ||
t.end() | ||
}) | ||
) | ||
|
||
test('detect: no SHELL', (t) => | ||
exec('node . --shell-auto-fallback', { | ||
env: {} | ||
}, (err, stdout, stderr) => { | ||
t.equal(err.code, 1) | ||
t.notOk(stdout) | ||
t.match(stderr, /Only .+ shells are supported :\(/) | ||
t.end() | ||
}) | ||
) | ||
|
||
test('detect: SHELL ~= unsupported', (t) => | ||
exec('node . --shell-auto-fallback', { | ||
env: { | ||
SHELL: '/sbin/nope' | ||
} | ||
}, (err, stdout, stderr) => { | ||
t.equal(err.code, 1) | ||
t.notOk(stdout) | ||
t.match(stderr, /Only .+ shells are supported :\(/) | ||
t.end() | ||
}) | ||
) | ||
|
||
test('given: fish', (t) => | ||
exec('node . --shell-auto-fallback fish', (err, stdout, stderr) => { | ||
if (err) { throw err } | ||
t.match(stdout, /function __fish_command_not_found/) | ||
t.notOk(stderr) | ||
t.end() | ||
}) | ||
) | ||
|
||
test('given: bash', (t) => | ||
exec('node . --shell-auto-fallback bash', (err, stdout, stderr) => { | ||
if (err) { throw err } | ||
t.match(stdout, /command_not_found_handle\(/) | ||
t.notOk(stderr) | ||
t.end() | ||
}) | ||
) | ||
|
||
test('given: zsh', (t) => | ||
exec('node . --shell-auto-fallback zsh', (err, stdout, stderr) => { | ||
if (err) { throw err } | ||
t.match(stdout, /command_not_found_handler\(/) | ||
t.notOk(stderr) | ||
t.end() | ||
}) | ||
) | ||
|
||
test('given: unsupported', (t) => | ||
exec('node . --shell-auto-fallback nope', (err, stdout, stderr) => { | ||
t.equal(err.code, 1) | ||
t.notOk(stdout) | ||
t.match(stderr, /Invalid values:\s+Argument: shell-auto-fallback/) | ||
t.end() | ||
}) | ||
) |