Skip to content
This repository has been archived by the owner on Jul 6, 2019. It is now read-only.

Add --shell-auto-fallback #7

Merged
merged 5 commits into from
May 31, 2017
Merged

Add --shell-auto-fallback #7

merged 5 commits into from
May 31, 2017

Conversation

passcod
Copy link
Contributor

@passcod passcod commented May 30, 2017

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

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
@passcod passcod changed the title feat(opts): add --shell-auto-fallback Add --shell-auto-fallback May 30, 2017
@zkat
Copy link
Owner

zkat commented May 30, 2017

This is lovely. Do you mind adding docs about this to README.md?

@zkat zkat self-requested a review May 30, 2017 17:01
Copy link
Owner

@zkat zkat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs, please!

Copy link
Owner

@zkat zkat left a 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
Copy link
Owner

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 === '') {
Copy link
Owner

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 ''

Copy link
Contributor Author

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.

Copy link
Owner

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'],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ''?

Copy link
Contributor Author

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.

Copy link
Owner

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?

Copy link
Contributor Author

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.

@passcod
Copy link
Contributor Author

passcod commented May 31, 2017

Oops, I broke it. ...good reminder to add tests, I suppose. I'll do that now.
Orrrr maybe it never worked. I've confused myself. I'll figure it out.

@passcod
Copy link
Contributor Author

passcod commented May 31, 2017

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.

@zkat zkat merged commit ac9cb40 into zkat:latest May 31, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants