0

I have lint-staged + husky configured in my app and I need to run a custom lint-staged script on precommit. Here is my package.json

//package.json
"lint-staged": {
  "app/**/*.js": "node ./lint-staged-scripts/customScript"
}

In the customScript.js, how will I be able to access the staged files ?

I tried running node --inspect-brk ./lint-staged-scripts/customScript, could not get staged files in args.

But on running npx lint-staged -d, i could see the files getting listed in the fileList.

How to access staged files and debug custom lint-staged scripts?

TIA and i'm new to writing lint-staged scripts !

codermonk
  • 84
  • 6
  • The only way to get staged files is through git commands. If you are debugging the functionality of the script, you can just run git diff command to list down the staged files and pass the list as input to your customScript. Once your customScript is ready, you can test it with the real code commits. – mathu mitha Aug 14 '22 at 17:22

1 Answers1

0

I still could not figure how to debug it. But looks like we get the staged files as arguments to the customScript file.

process.argv.slice(2) // will give the list of staged files

It is mentioned in the docs as well https://github.com/okonet/lint-staged#configuration

This config will execute your-cmd with the list of currently staged files passed as arguments. So, considering you did git add file1.ext file2.ext, lint-staged will run the following command: your-cmd file1.ext file2.ext

codermonk
  • 84
  • 6