cristinafsanz
7/25/2019 - 3:59 PM

Pre-commit with Husky

  • eslint: initialize con --init and add airbnb linter
  • stylelint: add .stylelintrc.json and add: "extends": "stylelint-config-airbnb"
  • package.json:
{
    "scripts": {
     "start-dev": "yarn run webpack --mode development --watch",
      "start-prod": "yarn run webpack --mode production",
      "lint:js": "eslint hydration_for_health/webpack_source/js/*",
      "lint:css": "stylelint hydration_for_health/webpack_source/sass/**/*",
      "lint:js:fix": "eslint hydration_for_health/webpack_source/js/* --fix",
      "lint:css:fix": "stylelint hydration_for_health/webpack_source/sass/**/* --fix",
      "lint": "npm run lint:js && npm run lint:css"
    },
    "husky": {
     "hooks": {
         "pre-commit": "npm run lint"
      }
    },
    "dependencies": {
      ...
    },
    "devDependencies": {
      "eslint": "^5.15.3",
      "eslint-config-airbnb-base": "^13.1.0",
      "eslint-plugin-import": "^2.16.0",
      "husky": "^1.3.1",
      "stylelint": "^9.10.1",
      "stylelint-config-airbnb": "0.0.0",
      "stylelint-order": "^2.1.0",
      "stylelint-scss": "^3.5.4"
    }
}
  • When git commit, the precommit is launched and if there are errors, the proccess stop and you have the errors in the terminal.
  • You can launch npm run lint:css:fix and git add . and try git commit again.