lint-staged特定パス対象する方法

1 min read
hiroweb developer

概要

lint-staged を利用して、Git の pre-commit 時にファイルを検証している。

検証対象のファイルは、*.jsなどのように指定できるが、その中で除外したいファイルがあり、それを除外したい。

方法

仕様

  • lint-staged.config.js で定義
  • .config.jsが含まれるパスを除外する
// lint-staged.config.js
const path = require("path");

module.exports = {
  "*.js": (files) => {
    const cwd = process.cwd();
    const relativePaths = files
      .map((file) => path.relative(cwd, file))
      .filter((relativePaths) => !relativePaths.includes(".config.js"));

    return `eslint --fix ${relativePaths.join(" ")}`;
  },
};