carloscuesta/gitmoji-cli

within hooks use command `npx gitmoji` instead of single `gitmoji`

AndreasAugustin opened this issue · 3 comments

Discussion

Hello,

sometimes it is useful not installing a npm tool globally.
Instead e.g. install it locally in a repo.

To be able to e.g. execute binaries you can use npx which is shipped per default with npm (i think since npm v7).

With the current version of gitmoji-cli the git hook looks like

'#!/bin/sh\n# gitmoji as a commit hook\n' +
    'exec < /dev/tty\ngitmoji --hook $1 $2\n'

which needs to have gitmoji-cli globally installed.

A change to

'#!/bin/sh\n# gitmoji as a commit hook\n' +
    'exec < /dev/tty\nnpx gitmoji --hook $1 $2\n'

would use npx to call gitmoji.

Related to docs

By default, npx will check whether exists in $PATH, or in the local project binaries, and execute that.

This would be a nice way to make the solution more portable for multi user projects because the dependency to gitmoji-cli can be pinned with a special version in the package.json.

Possible issues:

  • user does not have npx installed
    • npx comes with npm per default, I think starting with v7
    • not sure about the supported brew install.
    • not sure how it is working for yarn users

Advantages

  • no need to install gitmoji-cli global on the system which helps to keep the system clean
  • global installation still supported (if npx is installed)
  • portable -> will be installed on each dev system locally because added as dev dependency in the right version because it is part of package.json
    • personally I am part of many different projects using many different versions of tools. Often it is needed that the members have the right versions installed.

Validations

Hey!

Thanks for opening the issue, I'm aware of npx and its benefits. I think it's definitely something we can explore:

not sure about the supported brew install.

The gitmoji-cli package depends on node as you can see here on the formulae page: https://formulae.brew.sh/formula/gitmoji

So in case node it's not installed it should be installed as part of the installation process of the formula.

not sure how it is working for yarn users

Yarn classic has no equivalent to npx but yarn berry has a similar command named dlx.

Though if set npx as part of the hook we will be defining a dependency with npm inside of the hook, which is not ideal.

no need to install gitmoji-cli global on the system which helps to keep the system clean

I'm wondering if this is really an advantage. How are you initialising the hook? I guess you're using the gitmoji -i hook command to create it right?

Which means that you already have the binary installed globally at the system level, so in that case it makes no much difference to call the binary with npx.

The only thing where it would make sense, is in the scenario where you "manually" create the pre-commit hook in your project.

In that specific case you can always define the hook as you want, (even using npx) as you can modify and maintain the hook manually.

global installation still supported (if npx is installed)

Yes, but npx add some potential issues with people with no npm installed on their system a dependency that now we don't have as we rely on the binary.

portable -> will be installed on each dev system locally because added as devDependency in the right version because it is part of package.json

Also on top of that, there's no such need to install the cli globally, you can always add the tool as a devDependency to your project so you won't have to rely at the global level and you will be able to use it locally from the project dependencies.


For these specific reasons I'm not sure if it makes sense to use npx inside of the hook 🤔

Hi @carloscuesta and thanks for your suggestions.

not sure how it is working for yarn users

Yarn classic has no equivalent to npx but yarn berry has a similar command named dlx.

Though if set npx as part of the hook we will be defining a dependency with npm inside of the hook, which is not ideal.

no need to install gitmoji-cli global on the system which helps to keep the system clean

I'm wondering if this is really an advantage. How are you initialising the hook? I guess you're using the gitmoji -i hook command to create it right?

# install local
npm i -D gitmoji-cli
npx gitmoji -i

Which means that you already have the binary installed globally at the system level, so in that case it makes no much difference to call the binary with npx.

So the commit hook is created with the gitmoji-cli and npx without global installation (not manually)

The only thing where it would make sense, is in the scenario where you "manually" create the pre-commit hook in your project.

In that specific case you can always define the hook as you want, (even using npx) as you can modify and maintain the hook manually.

sure 😊 I can put that on my own. But easier with your cli. Useful e.g. with onboarding new team members, that way less documentation and no custom scripting needed (ok, would be an easy script ofc :p )

global installation still supported (if npx is installed)

Yes, but npx add some potential issues with people with no npm installed on their system a dependency that now we don't have as we rely on the binary.

Yes, e.g. as mentioned the case when yarn is used.

portable -> will be installed on each dev system locally because added as devDependency in the right version because it is part of package.json

Also on top of that, there's no such need to install the cli globally, you can always add the tool as a devDependency to your project so you won't have to rely at the global level and you will be able to use it locally from the project dependencies.

In that case AFAIK you have some options:

  • add the local binary within the node_modules to the path -> possible to be done with e.g. .env files but again I need to document a lot for different teams ;)
  • add a npm/yarn script -> hook does not work
  • use npx -> issues with yarn or other package managers

For these specific reasons I'm not sure if it makes sense to use npx inside of the hook 🤔

Maybe 🤔 add a check first in the hook if npx is installed and use it in that case? Can be extended with your mentioned dlx? But it makes the sh script a bit more complicated. Or let the user decide how to install the hook? Like

#-D chosen similar to npm
npx gitmoji -i -D npx

Hey!

Let's give it a try, reviewing the PR