Skip to content

Instantly share code, notes, and snippets.

@CodeByKwakes
Last active October 6, 2023 12:01
Show Gist options
  • Save CodeByKwakes/93f3b1104e9279660a7c54f5c8681ae1 to your computer and use it in GitHub Desktop.
Save CodeByKwakes/93f3b1104e9279660a7c54f5c8681ae1 to your computer and use it in GitHub Desktop.
Git Hook setup

Husky

# install Husky
npx husky-init && npm install

# create empty hooks
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"' # or npx husky add .husky/commit-msg ''
npx husky add .husky/prepare-commit-msg '(exec < /dev/tty && node_modules/.bin/cz --hook) || true < /dev/null' # npx husky add .husky/prepare-commit-msg ''
# install lint-staged
npm i -D lint-staged

# install commitlint and helper
npm install -D @commitlint/config-nx-scopes @commitlint/cli @commitlint/config-conventional

# install commitizen
npm i -D commitizen @commitlint/cz-commitlint
# install lint-staged, commitizen, commitlint and helper
npm i -D lint-staged commitizen @commitlint/{cli,config-conventional,config-nx-scopes,cz-commitlint} 

pre-commit hook

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged --relative

We use --relative so lint-staged will apply the paths of the staged files as relative paths which we need for nx format:write

commit-msg

. "$(dirname "$0")/_/husky.sh"

npx --no -- commitlint --edit "$1"

prepare-commit-msg

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

(exec < /dev/tty && node_modules/.bin/cz --hook) || true < /dev/null

pre-push

npx husky add .husky/pre-push 'npx nx affected -t test && npx nx affected -t component-test'

.commitlintrc.js

echo "module.exports = {extends: ['@commitlint/config-conventional', '@commitlint/config-nx-scopes'],rules: {'scope-enum': [2, 'always', ['nx', 'deps', 'release', 'feature', 'data-access', 'ui', 'util', 'routes', 'model', 'api']]}}" >.commitlintrc.js
module.exports = {
  extends: ['@commitlint/config-conventional', '@commitlint/config-nx-scopes'],
  rules: {
    'scope-enum': [
      2,
      'always',
      [
        'feature',
        'data-access',
        'ui',
        'util',
        'routes',
        'model',
        'api',
      ],
    ],
  },
};

.cz-config.js

echo "module.exports = {path: '@commitlint/cz-commitlint'}" > .cz-config.js

lintstagedrc

echo "module.exports = {
  \"{apps,libs,src}/**/*.{ts,js,html,json,scss,css,md}\": [
    \"nx affected:lint --uncommitted --fix true\"
  ],
  \"{apps,libs,src}/**/*.{ts,js,html,json,scss,css,md,yaml,yml}\": [
    \"nx format:write --uncommitted\"
  ]
}" >.lintstagedrc.js
module.exports = {
  '{apps,libs,src}/**/*.{ts,js,html,json,scss,css,md}': [
    'nx affected:lint --uncommitted --fix true',
  ],
  '{apps,libs,src}/**/*.{ts,js,html,json,scss,css,md,yaml,yml}': [
    'nx format:write --uncommitted',
  ],
};

Reference:

https://thefullstack.engineer/full-stack-development-series-part-6-application-deployment-and-ci-cd/

https://medium.com/@guysenpai89/nx-monorepo-publish-libraries-to-github-packages-with-github-actions-semantic-release-970883f2786a

https://christianlydemann.com/how-to-set-up-git-hooks-in-an-nx-repo/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment