commitizen / cz-cli

The commitizen command line utility. #BlackLivesMatter
http://commitizen.github.io/cz-cli/
MIT License
16.77k stars 551 forks source link

Use git cz globally, in non-npm / non-javascript projects #83

Closed sebastienbarre closed 8 years ago

sebastienbarre commented 8 years ago

Big fan of commitizen here. In so much that I'm trying to use it for projects that are not part of the NPM/node/javascript ecosystem. Any git repository, that is.

I can't get that scenario to work. I've installed commitizen, conventional-changelog, cz-conventional-changelog globally with -g, but running git cz fails (and falls back to regular git) because the adapter is either not found, or not loaded. No surprise here I guess, since there is no package.json file, and there won't be one in that repo. Would it be possible to provide a global configuration file, say ~/.czconfig, that the CLI would use when all else fails?

Thanks

jimthedev commented 8 years ago

@sebastienbarre Sorry I was a bit slow to reply. Good question, though!

Our config loader actually allows for this. You can use either .cz.json or .czrc. Just throw your config in there and we'll pick up the config. :+1:

Hopefully that works!

sebastienbarre commented 8 years ago

Thanks. Interesting! Failing for me:

$ git cz -a
Error: Could not locate node_modules in your project's root directory. Did you forget to npm init or npm install?
path.js:481
      throw new TypeError('Arguments to path.join must be strings');
            ^
TypeError: Arguments to path.join must be strings
    at Object.posix.join (path.js:481:13)
    at getNearestProjectRootDirectory (/Volumes/Users/barre/.npm-packages/lib/node_modules/commitizen/dist/commitizen/adapter.js:138:28)
    at gitCz (/Volumes/Users/barre/.npm-packages/lib/node_modules/commitizen/dist/cli/strategies/git-cz.js:74:58)
    at Object.bootstrap (/Volumes/Users/barre/.npm-packages/lib/node_modules/commitizen/dist/cli/git-cz.js:29:27)

Maybe I'm not using it properly, here is my ~/.czrc:

{
  "config": {
    "commitizen": {
      "path": node_modules/cz-conventional-changelog"
    }
  }
}

I also tried

      "path": "~/.npm-packages/lib/node_modules/cz-conventional-changelog"
jimthedev commented 8 years ago

Ah yes, can you try:

{
  "path": "~/.npm-packages/lib/node_modules/cz-conventional-changelog"
}

I've also not actually tried using the ~ in the path but hopefully it would work. If not we could probably implement some kind of way to resolve ~ to a homedir.

sebastienbarre commented 8 years ago

I'm afraid neither the above, nor a resolved path did the trick...

{
  "path": "/Volumes/Users/barre/.npm-packages/lib/node_modules/cz-conventional-changelog"
}

still getting

$ git cz -a
Error: Could not locate node_modules in your project's root directory. Did you forget to npm init or npm install?
path.js:481
      throw new TypeError('Arguments to path.join must be strings');
            ^
TypeError: Arguments to path.join must be strings
    at Object.posix.join (path.js:481:13)
    at getNearestProjectRootDirectory (/Volumes/Users/barre/.npm-packages/lib/node_modules/commitizen/dist/commitizen/adapter.js:138:28)
jimthedev commented 8 years ago

Huh, ok I'll have to dig in more. Perhaps it is a bug. Thanks for working on it w/me. On Wed, Nov 25, 2015 at 6:32 PM Sébastien Barré notifications@github.com wrote:

I'm afraid neither the above, nor a resolved path did the trick...

{ "path": "/Volumes/Users/barre/.npm-packages/lib/node_modules/cz-conventional-changelog" }

still getting

$ git cz -a Error: Could not locate node_modules in your project's root directory. Did you forget to npm init or npm install? path.js:481 throw new TypeError('Arguments to path.join must be strings'); ^ TypeError: Arguments to path.join must be strings at Object.posix.join (path.js:481:13) at getNearestProjectRootDirectory (/Volumes/Users/barre/.npm-packages/lib/node_modules/commitizen/dist/commitizen/adapter.js:138:28)

— Reply to this email directly or view it on GitHub https://github.com/commitizen/cz-cli/issues/83#issuecomment-159765628.

jimthedev commented 8 years ago

I have this resolved, just working on fixing some tests it broke as a result. Will be in the next release.

sebastienbarre commented 8 years ago

Sweet, thanks

jimthedev commented 8 years ago

Ok so this was supposed to be simple but my tests alerted me to the fact that it isn't as simple as I'd hoped. This is due to two special places that the config loader is used in:

  1. Our test suite
  2. the commitizen init command, which attempts to make sure that it doesn't overwrite any existing adapter configs that might be up the tree.

So why are these two cases special? Well, simply put, the config loader needs two different traversal modes to handle them:

  1. Unbounded mode (the current default) - In this mode the config loader can traverse up the tree all the way to the drive root.
  2. Upper bounded mode - In this mode the config loader can only traverse up the tree to a certain directory. Once it gets to that directory it will stop the traversal. We need this mode specifically for tests because commitizen actually uses commitizen itself and we don't want the tests to detect our own config as valid. For commitizen init we need to allow people to choose if they want unbounded traversal (the default) or if they want to add an upper boundary during init.

Just wanted to give an update. :+1:

sebastienbarre commented 8 years ago

Thanks @jimthedev ; can you point me to the traversal code, out of curiosity?

yu-yuxuan commented 8 years ago

I create symbolic link to solve this problem. node_modules is in the /Users/yuyuxuan. Suppose my git repository is /Volumes/Transcend/Study/English/New_concept/New_concept_dictation I use ln -sf ~/node_modules . at the location /Volumes/Transcend/

sarbbottam commented 8 years ago

I could get commitizen and cz-conventional-changelog work for non-node-git-repo with global installation.

Here are the steps that I have followed.

Hope it will be helpful.

sebastienbarre commented 8 years ago

@sarbbottam the scenario you described didn't work when I created the issue, but it seems to have been fixed by @jimthedev since then. Which is great. Thanks to all.

sarbbottam commented 8 years ago

@jimthedev let me know if you would prefer a PR to update the README with

  • $ npm i -g commitizen cz-conventional-changelog
  • $ echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc; create .czrc
  • $ cd non-node-git-repo
  • $ touch foo
  • git cz -a
jimthedev commented 8 years ago

@sarbbottam That would be great if you could put in a README PR! 👍

@sebastienbarre Yes this was a nice PR from someone that replace how I was using require and instead using .resolve which is much more consistent behavior with how other node modules typically let you provide input. Cheers to the contributors. :)