dsifford / yarn-completion

Bash completion for Yarn
MIT License
277 stars 25 forks source link

File path autocompletion #11

Closed kachkaev closed 6 years ago

kachkaev commented 6 years ago

Hi again @dsifford 👋

I've noticed one more difference between npm completion and yarn completion and decided to share. It is to do with file paths. Here's the use case:

I've got a TypeScript / Node.js project, which stores a few data-processing actions. Folder structure looks like so:

node_modules
src/actions/do-this.ts
src/actions/do-that.ts
src/actions/....ts
src/lib/...
package.json

Every file in src/actions is an entry point, which I could run just as node src/actions/do-this.js if they were written in pure JavaScript. The most straightforward alternative for TypeScript is to go for ts-node src/actions/do-this.ts, assuming that ts-node is globally installed. However, a local copy of ts-node is better, especially if the project is shared between multiple people.

A pretty handy alternative to typing node_modules/.bin/ts-node src/actions/do-this.ts can be creating simple proxy scripts in package.json:

    "exec": "ts-node",
    "exec:watch": "ts-node-dev --respawn --notify=false",

The commands become shorter:

yarn exec src/actions/do-this.ts
yarn exec:watch src/actions/do-this.ts

In these cases, it'd be great if yarn-completion could hint users with the paths, say after typing yarn exec src/actions/d + tab. This feature is already supported in npm: npm run exec:watch src/acnpm run exec:watch src/actions/ etc.

WDYT?

dsifford commented 6 years ago

Agree.

I added exec yesterday actually. Wasn't sure what it was for so I didn't enable path expansion. All that would need to be done is enabling path expansion in the compgen call for exec.

dsifford commented 6 years ago

Re-reading this now and I think I may have misinterpreted your question. Are you saying you'd want to add path expansion to all custom scripts? Or just the new exec command?

If the former, what does npm do in that situation?

kachkaev commented 6 years ago

Looks like npm does path autocompletion in these cases:

This behaviour makes sense to me and it feels more or less reasonable for yarn completion too. I don't think we should do path autocompletion after yarn exec only – this is just an arbitrary script name.

kachkaev commented 6 years ago

BTW I had to update my custom scripts:

    "exec": "ts-node",
    "exec:watch": "ts-node-dev --respawn --notify=false",

    "exe": "ts-node",
    "exe:watch": "ts-node-dev --respawn --notify=false",

It turned out that exec is some deprecated command in yarn, which causes glitches when I omit run. Typing yarn run exec worked OK while yarn exec was giving an error (because a special yarn's command was called, not my script). Both yarn run exe path/to/file.js and yarn exe path/to/file.js behave well and it'll be great to have path autocompletion in them one day! 👍

dsifford commented 6 years ago

exec is actually a brand new command that hasn't been documented yet. It was just released (IIRC) on their last minor release.

Adding path completion is on my todo list. Haven't gotten around to it just yet because it'll take about an hour to get finished up. Hopefully gonna sit down and work on that and also the workspace(s) subcommands one of these next coming days.

dsifford commented 6 years ago

@kachkaev Give 0.7.0 a shot and let me know how that works for you

kachkaev commented 6 years ago

Thank you for working on this update 0.7 – I'm really looking forward to using the new features!

It's probably something to do with macOS again, but after grabbing the new version and trying yarn [tab] in a new Terminal, I'm getting:

yarn -bash: compopt: command not found
-bash: compopt: command not found

bash --version
GNU bash, version 4.4.19(1)-release (x86_64-apple-darwin17.3.0)
dsifford commented 6 years ago

Urggggggh. I hate mac. :rage:

What version of bash-completion do you have installed? I've found recently that the regular one in Homebrew is not the one people with updated versions of bash should be using..

Can you try installing instead bash-completion@2 (the name might not be exactly that, but it'll turn up when you search for it)

The regular bash-completion is a waaaaaaaay outdated branch that assumes bash 3 is installed.

kachkaev commented 6 years ago

You're right, I had bash-completion instead of bash-completion@2!

screen shot 2018-03-13 at 19 47 28

After brew uninstall bash-completion, brew install bash-completion@2 and updating ~/.bash_profile, I started getting this in every new terminal:

screen shot 2018-03-13 at 20 00 53

So looks like one of my installed completions is not working with @2:

screen shot 2018-03-13 at 20 01 13

yarn-completion still does not work:

screen shot 2018-03-13 at 20 03 10

Switched back to bash-completion for now.

dsifford commented 6 years ago

@kachkaev declare -A is a bash 4 option. All versions of bash 4 should support it.

So it looks like your login shell might not be bash 4?

kachkaev commented 6 years ago

You are right, it was not. I had to open Terminal settings and set Command to what I got in which bash (for Homebrew bash):

screen shot 2018-03-13 at 20 17 02

All works now! 🎉 ❤️

dsifford commented 6 years ago

Awesome, glad to hear.

For others who might also read this, you can also change your login shell by typing chsh in the terminal itself.

kachkaev commented 6 years ago

Strangely, chsh did not work for me. After changing Shell: /bin/bash with Shell: /usr/local/bin/bash (i.e. Homebrew's bash) and saving the file with :wq, I got the following error:

chsh: /usr/local/bin/bash: non-standard shell
re-edit the password file? 

🤔


UPD: solution