snipt

Ctrl+h for KB shortcuts

Bash

Remove stray console.log() statements in a git pre-commit hook.

1
2
3
4
5
6
7
8
9
#!/bin/sh
 
logs="grep -iR 'console\.log' --exclude-dir=node_modules --exclude-dir=client/vendor --exclude-dir=server/views --exclude-dir=tests --exclude-dir=script --exclude='server/server.js' --exclude='*.un~' --exclude='api/server.js' *"
count=`eval "$logs | wc -l | awk '{print $1}'"`
if [[ "$count" -ge 1 ]]; then 
  echo "\033[0;31mRemove stray console.log() statements:\033[0m"
  eval $logs
  exit 1
fi

Description

Stick this in `.git/hooks/pre-commit`, and then `chmod u+x .git/hooks/pre-commit`.
https://snipt.net/embed/c7d4c074b14e0e79898110efb445647b/
/raw/c7d4c074b14e0e79898110efb445647b/
c7d4c074b14e0e79898110efb445647b
bash
Bash
9
2019-06-18T23:53:28
True
False
False
/api/public/snipt/136148/
remove-stray-console-log-statements-in-git-pre-commit-hook
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a> <a href="#L-9">9</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="ch">#!/bin/sh</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="nv">logs</span><span class="o">=</span><span class="s2">&quot;grep -iR &#39;console\.log&#39; --exclude-dir=node_modules --exclude-dir=client/vendor --exclude-dir=server/views --exclude-dir=tests --exclude-dir=script --exclude=&#39;server/server.js&#39; --exclude=&#39;*.un~&#39; --exclude=&#39;api/server.js&#39; *&quot;</span> </span><span id="L-4"><a name="L-4"></a><span class="nv">count</span><span class="o">=</span><span class="sb">`</span><span class="nb">eval</span> <span class="s2">&quot;</span><span class="nv">$logs</span><span class="s2"> | wc -l | awk &#39;{print </span><span class="nv">$1</span><span class="s2">}&#39;&quot;</span><span class="sb">`</span> </span><span id="L-5"><a name="L-5"></a><span class="k">if</span> <span class="o">[[</span> <span class="s2">&quot;</span><span class="nv">$count</span><span class="s2">&quot;</span> -ge <span class="m">1</span> <span class="o">]]</span><span class="p">;</span> <span class="k">then</span> </span><span id="L-6"><a name="L-6"></a> <span class="nb">echo</span> <span class="s2">&quot;\033[0;31mRemove stray console.log() statements:\033[0m&quot;</span> </span><span id="L-7"><a name="L-7"></a> <span class="nb">eval</span> <span class="nv">$logs</span> </span><span id="L-8"><a name="L-8"></a> <span class="nb">exit</span> 1 </span><span id="L-9"><a name="L-9"></a><span class="k">fi</span> </span></pre></div> </td></tr></table>
bash, git