-
-
Notifications
You must be signed in to change notification settings - Fork 105
Conversation
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
This is lovely. Do you mind adding docs about this to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs, please!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok! I actually went and reviewed the code, which I should've done in the first place. Looks good so far, with a few changes requested.
This still needs to get documented (be sure to add it to usage
in both the README and in parse-args
!)
auto-fallback.js
Outdated
end` | ||
|
||
module.exports = function autoFallback (shell) { | ||
const { BASH_VERSION, SHELL, ZSH_VERSION } = process.env |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're restricted to node@4
or latest, so was can't do destructuring assignment 😭
@@ -18,6 +19,12 @@ updateNotifier({pkg}).notify() | |||
main(parseArgs()) | |||
|
|||
function main (argv) { | |||
const shell = argv['shell-auto-fallback'] | |||
if (shell || shell === '') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
won't this always be true? parseArgs()
defaults to ''
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
requireArg
is false
! So sometimes the option will return null/undefined/false/whatever yargs returns for a non-provided option. But ''
is falsy, so I do need to check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't having a default mean that you always get ''
if you don't provide the option, though? I'm so confused.
@@ -62,6 +62,14 @@ function parseArgs () { | |||
type: 'string', | |||
describe: 'execute string as if inside `npm run-script`' | |||
}) | |||
.option('shell-auto-fallback', { | |||
choices: ['', 'bash', 'fish', 'zsh'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why ''
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll re-test, but I think yargs refused to parse the option if the default wasn't in the choices. Although. Given the difficulty of figuring out the shell from a subprocess, maybe I should just remove both the autodetecting code and the default value here, and just do it based on the passed value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just don't give it a default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As to why I made the default ''
... err. I don't remember. I'll have a look at the code again in its entirety to see if I can figure it out.
Oops, I broke it. ...good reminder to add tests, I suppose. I'll do that now. |
Ok, I've fixed it, added tests, wrote documentation. I've kept the same functionality, but I can remove the autodetect / default if you want. |
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.
As Seen On Twitter™