Home > Blockchain >  How to prevent files with "console.log" to be committed?
How to prevent files with "console.log" to be committed?

Time:04-08

I'm currently trying to enforce that me or my colleague cannot commit files that contains console.log in my angular application.

I currently already have husky on pre-commit, that execute a ng lint --fix.

Is there a way to either add something to my linting to prevent console log, or to add something in the husky script?

People should still be able to use console.log, just not commit it.

Thanks

CodePudding user response:

You can go to your project's tslint.json file and make sure this option is present in your file:

{
    "rules": {
        "no-console": true
    }
}

If you prefer something that won't "block" you but just warn you, you also can set this option as follows:

{
   "rules": {
      "no-console": {
         "severity": "warning",
      } 
   }
}

And finally, if you want to target some console functions more precisely, you can specify them like this:

{
    "rules": {
        "no-console": {
            "severity": "warning",
            "options": [
                "log",
                "error",
                "debug",
                "info",
                "time",
                "timeEnd",
                "trace"
            ]
        }
    }
}

CodePudding user response:

You can also create a .gitignorefile contaninig this:

console.log

With this your log file will be generated, but not commited.

EDIT

You can't ignore file by content with gitignore.

Instead, you can exlude files with shell script, like this:

for file in $(git grep -l --cached 'console.log') ; do
    git rm --cached $file
done
  • Related