how do i install husky to use commitlint with nested folders?

1.5k Views Asked by At

I've already installed husky and commitlint, but when I make a commit that commitlint should consider invalid, it normally passes as if it wasn't installed.

That's my directory tree

i have one script that installs husky on package.json:

{
  "name": "frontend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "prepare": "cd ../ && husky install ./frontend/.husky"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@commitlint/cli": "^16.2.4",
    "@commitlint/config-conventional": "^16.2.4",
    "husky": "^8.0.1"
  }
}

pre-commit file inside .husky

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
cd ./frontend && npx commitlint --edit

What should I do? My husky is installed so it should be working fine

2

There are 2 best solutions below

1
On

alexandreafa,

I got the same issue with yours, package.json file and .git directory are not at the same level. But I found a way to fix it, Step by step.

# Install commitlint cli and conventional config
npm install --save-dev @commitlint/{config-conventional,cli}
# For Windows:
npm install --save-dev @commitlint/config-conventional @commitlint/cli

# Configure commitlint to use conventional config
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js  

# Add hook
cat <<EEE > .husky/commit-msg
#!/bin/sh
. "\$(dirname "\$0")/_/husky.sh"

npx --no -- commitlint --edit "\${1}"
EEE   

chmod a+x .husky/commit-msg

  • update the commit-msg
CHANGE
npx --no -- commitlint --edit "\${1} 

TO
cd nestedFolderRoot npx --no -- commitlint --edit "\${1} 

fixed.

Now you could use git commit -m "type: xxxx" to commit it.

1
On

I had the same issue, got it fixed by replacing

cd ./frontend && npx commitlint --edit with "cd frontend && npm run commitlint ${1}"

and running command - npm set-script commitlint "commitlint --color --edit"

incase the package.json doesnt get updated ensure you have [email protected] else

execute - npm install -g [email protected]

also saw what you did with prepare config:

Incase if you have package.json and .git at different level of directory:

( If - when git directory at same level as package.json repository) ----------npx husky-init && npm install husky

(else if single sub_level) ----------npm set-script prepare "cd .. && husky install ${parent_repository_name}/.husky" then npm i

(else) ----------npm set-script prepare "cd ../.. && husky install ${parent_repository_name}/${sub_repositories_name}/.husky" then npm i