git 使用lint-staged来检查这个提交是否会构建好?

hwazgwia  于 2024-01-04  发布在  Git
关注(0)|答案(1)|浏览(143)

当使用husky和lint-staged时,是否可以在预提交中检查什么是staged构建?
就像这样?

"lint-staged": {
    "**/*.{js,jsx,ts,tsx,css,md}": [
      "prettier --write",
      "yarn build"
    ]
  }

字符串
如果我尝试这样做,我会得到一个构建错误,即使正在提交的内容应该构建,它说它找不到'pages'目录。这是因为它试图构建的只是暂存文件吗?
也许我正在尝试做一些不可能的事情。我只是想防止代码被签入,这将在CI构建管道上失败。
Thanks:)

1tu0hz3e

1tu0hz3e1#

是的,这是我的lint-staged.js文件

const fs = require("fs");
const path = require("path");

const prettierIgnorePath = path.resolve(__dirname, ".prettierignore");
const ignoredPatterns = fs
  .readFileSync(prettierIgnorePath, "utf-8")
  .split("\n");

function filterIgnoredFiles(filenames) {
  return filenames.filter((filename) => {
    return !ignoredPatterns.some((pattern) => filename.includes(pattern));
  });
}
module.exports = {
  // this will check Typescript files
  "**/*.(ts|tsx)": () => "npm run tsc -- --noEmit",

  "**/*.(ts|tsx|js)": (filenames) => {
    const filesToProcess = filterIgnoredFiles(filenames);
    const tasks = [`npm run eslint -- --fix ${filenames.join(" ")}`]; // eslint will always run on all files

    if (filesToProcess.length > 0) {
      tasks.push(`npm run prettier -- --write ${filesToProcess.join(" ")}`);
    }

    return tasks;
  },

  // this will Format MarkDown and JSON
  "**/*.(md|json)": (filenames) =>
    `npm run prettier -- --write ${filenames.join(" ")}`,
  //  build check here
  "**/*": () => "npm run build",
};

字符串
我正在做一个Nextjs项目,我通过Husky Pre-commit钩子运行这个项目。

相关问题