LoginSignup
6
1

More than 1 year has passed since last update.

Python + TypeScriptのmonorepoでpre-commitの設定

Last updated at Posted at 2021-12-29

はじめに

やろうとした時に他にやっている人がいないくて、
色々調べてもなかったので備忘録として。

よくあるやつ

  • TypeScript

Run a TypeScript type check in your pre-commit hook using lint-staged + husky にあるように huskylint-staged の組み合わせを使う

{
  "scripts": {
    "prepare": "husky install"
  },
  "lint-staged": {
    "**/*.{ts,tsx}": [
      "prettier --config .prettierrc --parser typescript --write",
      "git add"
    ],
  },
  "devDependencies": {
    "husky": "^7.0.4",
    "lint-staged": "^12.1.4",
    "prettier": "^2.5.1"
  }
}

  • Python

Python製のツールpre-commitでGitのpre-commit hookを楽々管理!! にあるように pre-commit を使う

repos:
-   repo: https://github.com/psf/black
    rev: 21.12b0
    hooks:
      -   id: black
-   repo: https://gitlab.com/pycqa/flake8
    rev: 4.0.1
    hooks:
      -   id: flake8
          args: ['--extend-ignore=E203', '--max-line-length=88']
-   repo: https://github.com/pycqa/isort
    rev: 5.10.1
    hooks:
      -   id: isort
          args: ['--profile', 'black']

問題点

これらのツールは、 gitpre-commit フックを使っていて、且つこのフックには一つしか設定できないので、どちらかしか使えない。

最初に pre-commit を設定して「動いた!」ってなった後に、 husky を設定すると husky は動くけど pre-commit は動かなくなる。

解決策

Pythonpre-commitTypeScript の設定を追加する。

repos:
-   repo: https://github.com/psf/black
    rev: 21.12b0
    hooks:
      -   id: black
          files: '^backend/.*\.py'
-   repo: https://gitlab.com/pycqa/flake8
    rev: 4.0.1
    hooks:
      -   id: flake8
          files: '^backend/.*\.py'
          args: ['--extend-ignore=E203', '--max-line-length=88']
-   repo: https://github.com/pycqa/isort
    rev: 5.10.1
    hooks:
      -   id: isort
          files: '^backend/.*\.py'
          args: ['--profile', 'black']
-   repo: https://github.com/pre-commit/mirrors-prettier
    rev: v2.5.1
    hooks:
      -   id: prettier
          files: '^frontend/.*\.(ts|tsx)'
          args: ['--parser', 'typescript', '--write']

Typescripthuskylint-stagedPython を追加することもできるはずなのだけど、
なぜか上手く動かない。。。

{
  "scripts": {
    "prepare": "husky install"
  },
  "lint-staged": {
    "**/*.{ts,tsx}": [
      "prettier --config .prettierrc --parser typescript --write",
      "git add"
    ],
    "**/*.py": [
      "pre-commit",
      "flake8 --extend-ignore=E203 --max-line-length=88",
      "isort --profile black",
      "git add"
    ]
  },
  "devDependencies": {
    "husky": "^7.0.4",
    "lint-staged": "^12.1.4",
    "prettier": "^2.5.1"
  }
}

あと、 pre-commit は設計思想として multi-language をあげているので
pre-commit に対応を追加するのがよさそう。

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1