【问题标题】:How to run pre-commit on separate folders?如何在单独的文件夹上运行预提交?
【发布时间】:2020-08-03 15:15:18
【问题描述】:

我使用pre-commit,并且我有一个包含两个文件夹的存储库:

.
├── backend
│   └── (backend files)
├── frontend
│   └── (frontend files)
└── .pre-commit-config.yaml

每个文件夹都有一个 Dockerfile 并且相互独立。

这是我的.pre-commit-config.yaml

repos:
  - repo: local
    hooks:
      - id: go-docker
        name: go
        language: docker_image
        entry: backend:latest go fmt
        files: backend/

      - id: prettier-docker
        name: prettier
        language: docker_image
        entry: frontend:latest npm run format
        files: frontend/

我想使用 Docker 在两个文件夹上运行预提交,但我得到:

npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /src/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/src/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

根据预提交文档:pre-commit will automatically mount the repository source as a volume using -v $PWD:/src:rw,Z and set the working directory using --workdir /src.

所以问题似乎是frontend 文件夹位于src/frontend 但预提交使用的路径只是src

我在 Docker 命令之前尝试了cd frontend,但没有成功。有什么想法吗?

提前致谢!

【问题讨论】:

    标签: pre-commit pre-commit.com


    【解决方案1】:

    您是正确的,您需要 cd,但您需要在 docker 内部执行此操作

    例如:

          - id: prettier-docker
            name: prettier
            language: docker_image
            entry: frontend:latest bash -c 'cd frontend && npm run format'
            files: frontend/
    

    如果我没记错的话,你也会想要pass_filenames: false

    【讨论】:

    • 谢谢,就是这样!是的,我也会传递pass_filenames: false,为了简单起见,我删除了它。
    猜你喜欢
    • 2018-08-17
    • 2014-02-16
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    相关资源
    最近更新 更多