【问题标题】:lint-staged for elixir/phoenix projectslint-staged 用于 elixir/phoenix 项目
【发布时间】:2022-01-21 05:47:25
【问题描述】:

如何使用mix credomix format 等将一个 lint 文件分阶段提交,而不是项目中的所有文件?

在 JavaScript 生态系统中,这是通过 lint-stagedhusky 完成的。 Elixir 有其名为 git_hooks 的 husky 包版本,但我没有发现任何类似于 lint-staged 的​​东西。

是否存在一个 elixir 包来实现我在提交 elixir 文件时仅运行 lint 命令的目标?

我在 config/dev.ex 中使用 git_hook 运行的示例配置。

config :git_hooks,
  auto_install: true,
  verbose: true,
  mix_path: "docker exec --tty $(docker-compose ps -q web) mix",
  hooks: [
    pre_commit: [
      tasks: [
        {:mix_task, :format, ["--check-formatted"]},
        {:mix_task, :credo, ["--strict"]}
      ]
    ]
  ]

【问题讨论】:

  • 嗨!我不知道是否有这个包,但你可以使用git diff --name-only | xargs mix credo 来完成它(因为 credo 接受文件列表作为参数)。 mix format 也可以这样做。希望对您有所帮助!
  • @sabiwara diff --name-only --cached | xargs mix credo 需要缺少的 --cached 参数。我会尝试使用它。

标签: elixir githooks phoenix lint-staged


【解决方案1】:

我使用 git_hooks 包和以下内容得到了这个:

config/dev.ex

config :git_hooks,
  auto_install: true,
  verbose: true,
  mix_path: "docker exec --tty $(docker-compose ps -q web) mix",
  hooks: [
    pre_commit: [
      tasks: [{:file, "./priv/githooks/pre_commit.sh"}]
   ]
 ]

priv/githooks/pre_commit.sh

#!/bin/ash

# notice the hash bang is for alpine linux's ash

# run mix format
git diff --name-only --cached | grep -E ".*\.(ex|exs)$" | xargs mix format --check-formatted

# run mix credo
git diff --name-only --cached | xargs mix credo --strict

使用文件任务类型,我使用了 git diff 命令并将暂存的文件传送到不同的混合命令以进行 linting。

【讨论】:

    猜你喜欢
    • 2022-01-26
    • 1970-01-01
    • 2017-10-21
    • 2020-09-20
    • 1970-01-01
    • 2018-08-04
    • 2019-06-21
    • 2019-09-12
    • 2020-12-27
    相关资源
    最近更新 更多