commitizen / commitizen/cz-cli

commitizen init fails when commitizen is installed with -g

Open
#418 3 comments 7 reactions 0 assignees View on GitHub
bug cli hacktoberfest help / pr wanted
Dominant language
JavaScript
Stars
17.5k
Forks
566
Avg merge
8h 16m
Merged PRs (30d)
1

Description

Installing commitizen globally using the steps in the documentation as shown here

```
npm install -g commitizen
npm install -g cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc
```

Prevents you from being able to run `commitizen init --save-dev cz-conventional-changelog` in a brand new npm project.

```
Attempting to initialize using the npm package cz-conventional-changelog
Error: A previous adapter is already configured. Use --force to override
```

Actually running `commitizen init --save-exact --save-dev cz-conventional-changelog --force` results in:

```
Attempting to initialize using the npm package cz-conventional-changelog
npm WARN using --force I sure hope you know what you are doing.
npm WARN saveError ENOENT: no such file or directory, open '/Users/jim.cummins/package.json'
/Users/jim.cummins
└─┬ cz-conventional-changelog@1.2.0
├── conventional-commit-types@2.1.0
├── lodash.map@4.6.0
├── longest@1.0.1
├─┬ pad-right@0.2.2
│ └── repeat-string@1.6.1
├── right-pad@1.0.1
└── word-wrap@1.2.0

npm WARN enoent ENOENT: no such file or directory, open '/Users/jim.cummins/package.json'
npm WARN jim.cummins No description
npm WARN jim.cummins No repository field.
npm WARN jim.cummins No README data
npm WARN jim.cummins No license field.
{ Error: ENOENT: no such file or directory, open '/Users/jim.cummins/package.json'
at Error (native)
at Object.fs.openSync (fs.js:640:18)
at Object.fs.readFileSync (fs.js:508:33)
at addPathToAdapterConfig (/Users/jim.cummins/.nvm/versions/node/v6.9.1/lib/node_modules/commitizen/dist/commitizen/adapter.js:71:40)
at init (/Users/jim.cummins/.nvm/versions/node/v6.9.1/lib/node_modules/commitizen/dist/commitizen/init.js:104:9)
at Object.bootstrap (/Users/jim.cummins/.nvm/versions/node/v6.9.1/lib/node_modules/commitizen/dist/cli/commitizen.js:43:30)
at Object. (/Users/jim.cummins/.nvm/versions/node/v6.9.1/lib/node_modules/commitizen/bin/commitizen.js:2:38)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/Users/jim.cummins/package.json' }
```

I think this is because when running init we need to assume that the user wants to only init in the current directory, as such we should not be using findup to get the highest npm path like we do when looking for an adapter. Instead we need to be stricter in the case of init. We should explicitly be looking in the current directory for a package.json or we should fail.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.